log messages changed
This commit is contained in:
@@ -10,6 +10,9 @@ cache_dir = "cache/CSharpScript"
|
|||||||
# comppiled file name
|
# comppiled file name
|
||||||
assembly_file_name = "Program.dll"
|
assembly_file_name = "Program.dll"
|
||||||
|
|
||||||
|
# set to true to recompile instead of using cached assemblies
|
||||||
|
no_cache = false
|
||||||
|
|
||||||
# paths of dll files that are referenced in your source code
|
# paths of dll files that are referenced in your source code
|
||||||
# Placeholders:
|
# Placeholders:
|
||||||
# %RUNTIME% - directory where System.* libraries are stored
|
# %RUNTIME% - directory where System.* libraries are stored
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -9,14 +10,15 @@ using Tomlyn;
|
|||||||
|
|
||||||
namespace CSharpScript;
|
namespace CSharpScript;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Global")]
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
public string src_dir { get; set; }
|
public required string src_dir { get; set; }
|
||||||
public string main_class { get; set; }
|
public required string main_class { get; set; }
|
||||||
public string cache_dir { get; set; } = "cache/CSharpScript";
|
public required string cache_dir { get; set; }
|
||||||
public string assembly_file_name { get; set; }
|
public required string assembly_file_name { get; set; }
|
||||||
|
public bool no_cache { get; set; }
|
||||||
public List<string> references { get; set; } = ["%RUNTIME%/netstandard.dll"];
|
public List<string> references { get; set; } = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Program
|
public static class Program
|
||||||
@@ -49,44 +51,45 @@ public static class Program
|
|||||||
var conf = TomlSerializer.Deserialize<Config>(File.ReadAllText(CONFIG_PATH), TomlSerializerOptions)
|
var conf = TomlSerializer.Deserialize<Config>(File.ReadAllText(CONFIG_PATH), TomlSerializerOptions)
|
||||||
?? throw new Exception("can't deserialize config " + CONFIG_PATH);
|
?? throw new Exception("can't deserialize config " + CONFIG_PATH);
|
||||||
|
|
||||||
|
var sw = new Stopwatch();
|
||||||
|
|
||||||
// find sources
|
// find sources
|
||||||
var csFiles = Directory.GetFiles(PathFixSeparators(conf.src_dir), "*.cs").ToList();
|
var csFiles = Directory.GetFiles(
|
||||||
Console.WriteLine("Source files:");
|
PathFixSeparators(conf.src_dir),
|
||||||
csFiles.ForEach(Console.WriteLine);
|
"*.cs",
|
||||||
Console.WriteLine("");
|
SearchOption.AllDirectories)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
// try load cached assembly
|
// try load cached assembly
|
||||||
string cachedAssemblyPath = Path.GetFullPath(
|
string cachedAssemblyPath = Path.GetFullPath(
|
||||||
Path.Combine(
|
Path.Combine(
|
||||||
PathFixSeparators(conf.cache_dir),
|
PathFixSeparators(conf.cache_dir),
|
||||||
conf.assembly_file_name));
|
conf.assembly_file_name));
|
||||||
if (File.Exists(cachedAssemblyPath))
|
if (!conf.no_cache && File.Exists(cachedAssemblyPath))
|
||||||
{
|
{
|
||||||
var sourceFileUpdateTime = csFiles.Select(File.GetLastWriteTime).Max();
|
var sourceFileUpdateTime = csFiles.Select(File.GetLastWriteTime).Max();
|
||||||
var cachedAssemblyUpdateTime = File.GetLastWriteTime(cachedAssemblyPath);
|
var cachedAssemblyUpdateTime = File.GetLastWriteTime(cachedAssemblyPath);
|
||||||
Console.WriteLine($"Sources in '{conf.src_dir}' were updated at {sourceFileUpdateTime:yyyy.MM.dd-HH:mm:ss}");
|
Console.WriteLine($"Sources were updated at {sourceFileUpdateTime:yyyy.MM.dd-HH:mm:ss} '{Path.GetFullPath(conf.src_dir)}'");
|
||||||
Console.WriteLine($"Assembly '{cachedAssemblyPath}' was cached at {cachedAssemblyUpdateTime:yyyy.MM.dd-HH:mm:ss}");
|
Console.WriteLine($"Assembly was cached at {cachedAssemblyUpdateTime:yyyy.MM.dd-HH:mm:ss} '{cachedAssemblyPath}'");
|
||||||
// sources weren't changed
|
// sources weren't changed
|
||||||
if (sourceFileUpdateTime <= cachedAssemblyUpdateTime)
|
if (sourceFileUpdateTime <= cachedAssemblyUpdateTime)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Loading cached assembly '{cachedAssemblyPath}'");
|
Console.WriteLine("Loading cached assembly");
|
||||||
var assembly = Assembly.LoadFile(cachedAssemblyPath);
|
var assembly = Assembly.LoadFile(cachedAssemblyPath);
|
||||||
return ExecAssemblyMain(assembly, conf, args);
|
return ExecAssemblyMain(assembly, conf, args);
|
||||||
}
|
}
|
||||||
Console.WriteLine("Cached assembly is obsolete");
|
Console.WriteLine("Cached assembly is obsolete\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile sources
|
// compile sources
|
||||||
var sw = Stopwatch.StartNew();
|
|
||||||
var compiler = InitCompiler(conf);
|
var compiler = InitCompiler(conf);
|
||||||
Console.WriteLine($"Initialized in {sw.ElapsedMilliseconds}ms");
|
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
Console.WriteLine("Compiling...");
|
Console.WriteLine("Compiling...");
|
||||||
var compResult = compiler.Compile(csFiles);
|
var compResult = compiler.Compile(csFiles);
|
||||||
Console.WriteLine($"Compiled in {sw.ElapsedMilliseconds}s");
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
|
Console.WriteLine($"Compiled in {sw.ElapsedMilliseconds}ms");
|
||||||
return compResult.Match(
|
return compResult.Match(
|
||||||
noChanges => throw new NotImplementedException(noChanges.ToString()),
|
noChanges => throw new ArgumentOutOfRangeException(noChanges.ToString()),
|
||||||
success =>
|
success =>
|
||||||
{
|
{
|
||||||
PrintDiagnostics(success.Diagnostics);
|
PrintDiagnostics(success.Diagnostics);
|
||||||
@@ -105,7 +108,8 @@ public static class Program
|
|||||||
|
|
||||||
private static RoslynWrapper InitCompiler(Config conf)
|
private static RoslynWrapper InitCompiler(Config conf)
|
||||||
{
|
{
|
||||||
Console.WriteLine("\nInitializing compiler...");
|
Console.WriteLine("Initializing compiler...");
|
||||||
|
var sw = Stopwatch.StartNew();
|
||||||
|
|
||||||
Console.WriteLine("Referenced assemblies:");
|
Console.WriteLine("Referenced assemblies:");
|
||||||
List<string> referenceAssemblyFilePaths = AppDomain.CurrentDomain
|
List<string> referenceAssemblyFilePaths = AppDomain.CurrentDomain
|
||||||
@@ -113,6 +117,7 @@ public static class Program
|
|||||||
.Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
|
.Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
|
||||||
.Select(a => a.Location)
|
.Select(a => a.Location)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
referenceAssemblyFilePaths.Sort();
|
||||||
referenceAssemblyFilePaths.ForEach(Console.WriteLine);
|
referenceAssemblyFilePaths.ForEach(Console.WriteLine);
|
||||||
|
|
||||||
var systemDllPath = referenceAssemblyFilePaths
|
var systemDllPath = referenceAssemblyFilePaths
|
||||||
@@ -130,12 +135,13 @@ public static class Program
|
|||||||
var referencedAssemblies = referenceAssemblyFilePaths
|
var referencedAssemblies = referenceAssemblyFilePaths
|
||||||
.Select(p => (MetadataReference)MetadataReference.CreateFromFile(p))
|
.Select(p => (MetadataReference)MetadataReference.CreateFromFile(p))
|
||||||
.ToList();
|
.ToList();
|
||||||
Console.WriteLine();
|
|
||||||
|
|
||||||
var compiler = new RoslynWrapper(conf.assembly_file_name, referencedAssemblies);
|
var compiler = new RoslynWrapper(conf.assembly_file_name, referencedAssemblies);
|
||||||
compiler.CompilationOptions = compiler.CompilationOptions
|
compiler.CompilationOptions = compiler.CompilationOptions
|
||||||
.WithOutputKind(OutputKind.ConsoleApplication)
|
.WithOutputKind(OutputKind.ConsoleApplication)
|
||||||
.WithMainTypeName(conf.main_class);
|
.WithMainTypeName(conf.main_class);
|
||||||
|
sw.Stop();
|
||||||
|
Console.WriteLine($"Initialized in {sw.ElapsedMilliseconds}ms\n");
|
||||||
return compiler;
|
return compiler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user