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