diff --git a/NTypewriter.SourceGenerator/AssemblyResolver.cs b/NTypewriter.SourceGenerator/AssemblyResolver.cs index 1b00e3d..45451fb 100644 --- a/NTypewriter.SourceGenerator/AssemblyResolver.cs +++ b/NTypewriter.SourceGenerator/AssemblyResolver.cs @@ -70,13 +70,13 @@ private static Assembly Resolve(object sender, ResolveEventArgs args) var asmName = new AssemblyName(args.Name); if (asmName.Name.EndsWith(".resources")) return null; - return ResolveFromAssemblyName(asmName); + return ResolveFromAssemblyName(asmName, args.RequestingAssembly); } - private static Assembly ResolveFromAssemblyName(AssemblyName asmName) + private static Assembly ResolveFromAssemblyName(AssemblyName asmName, Assembly requestingAssembly = null) { DumpLodedAssemblies(); - Log($"requested {asmName.Name}, {asmName.Version}"); + Log($"requested {asmName.Name}, {asmName.Version} by {requestingAssembly?.GetName()?.Name}"); var lodedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); @@ -110,12 +110,12 @@ private static Assembly ResolveFromAssemblyName(AssemblyName asmName) var roslynAsmPath = Path.Combine(microsoftCodeAnalysisPath, asmName.Name + ".dll"); if (File.Exists(roslynAsmPath)) { - var roslynAsm = LoadAssembly(roslynAsmPath); - if (roslynAsm != null) - { - Log($"resolved from Roslyn - {roslynAsm.GetName().Version}"); - return roslynAsm; - } + //var roslynAsm = LoadAssembly(roslynAsmPath); + //if (roslynAsm != null) + //{ + //Log($"resolved from Roslyn - {roslynAsm.GetName().Version}"); + //return roslynAsm; + //} } } } @@ -172,9 +172,16 @@ private static Assembly ResolveFromAssemblyName(AssemblyName asmName) private static Assembly LoadAssembly(string filePath) { //var b = File.ReadAllBytes(filePath); - var assembly = Assembly.LoadFile(filePath); - - return assembly; + try + { + var assembly = Assembly.LoadFile(filePath); + return assembly; + } + catch (Exception ex) + { + Log(ex.ToString()); + } + return null; } } } \ No newline at end of file diff --git a/NTypewriter.SourceGenerator/Extensions/System/StringExtensions.cs b/NTypewriter.SourceGenerator/Extensions/System/StringExtensions.cs new file mode 100644 index 0000000..80dcb05 --- /dev/null +++ b/NTypewriter.SourceGenerator/Extensions/System/StringExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Text; + +namespace NTypewriter.SourceGenerator.Extensions.System +{ + internal static class StringExtensions + { + private static readonly string TempPath = Path.Combine(Path.GetTempPath(), "NTSG"); + + + public static void WriteItDownAndForget(this string text, string fileName) + { + try + { + var path = Path.Combine (TempPath, fileName); + File.WriteAllText(path, text); + } + catch (Exception ex) + { + Trace.TraceError(ex.ToString(), ex); + } + } + } +} \ No newline at end of file diff --git a/NTypewriter.SourceGenerator/NTypewriter.SourceGenerator.csproj b/NTypewriter.SourceGenerator/NTypewriter.SourceGenerator.csproj index 1d3f334..7fb303d 100644 --- a/NTypewriter.SourceGenerator/NTypewriter.SourceGenerator.csproj +++ b/NTypewriter.SourceGenerator/NTypewriter.SourceGenerator.csproj @@ -5,7 +5,7 @@ true true false - 0.4.1 + 0.4.3 nt.128.128.png NeVeSpl NeVeSpl diff --git a/NTypewriter.SourceGenerator/SourceGenerator.cs b/NTypewriter.SourceGenerator/SourceGenerator.cs index 329b6b8..55f9643 100644 --- a/NTypewriter.SourceGenerator/SourceGenerator.cs +++ b/NTypewriter.SourceGenerator/SourceGenerator.cs @@ -19,6 +19,7 @@ using NTypewriter.Editor.Config; using NTypewriter.Runtime; using NTypewriter.SourceGenerator.Adapters; +using NTypewriter.SourceGenerator.Extensions.System; using Scriban; namespace NTypewriter.SourceGenerator @@ -26,7 +27,7 @@ namespace NTypewriter.SourceGenerator [Generator] public class NTypewriterSourceGenerator : ISourceGenerator { - private static readonly ConcurrentDictionary ProjectContexts = new(); + private static readonly ConcurrentDictionary ProjectContexts = new(); private static readonly object Padlock = new(); @@ -62,9 +63,10 @@ private void PostInitialization(GeneratorPostInitializationContext context) ]; var assembliesInfoLines = markingTypes.Select(x => $"// {x.Assembly.GetLogEntry()}"); - var postInitializationRaport = String.Join("\r\n", assembliesInfoLines); + var postInitializationRaport = String.Join("\r\n", assembliesInfoLines) + $"\r\n// CurrentDomain: {AppDomain.CurrentDomain.FriendlyName}"; - context.AddSource("diagnostics-initialization.g.cs", postInitializationRaport); + context.AddSource("diagnostics-initialization.g.cs", postInitializationRaport); + postInitializationRaport.WriteItDownAndForget("diagnostics-initialization.g.cs"); } @@ -73,24 +75,25 @@ public void Execute(GeneratorExecutionContext context) var projectContext = GetProjectContext(context); Interlocked.Increment(ref projectContext.ExecuteCount); - projectContext.LastTouchTime = File.GetLastWriteTime(projectContext.TouchFilePath); + projectContext.LastTouchTime = GetLastBuildTime(projectContext.TouchFilePath); projectContext.LastNTFileEditTime = GetLastEditTime(context.AdditionalFiles); bool doRender = false; lock (Padlock) - { + { if ((projectContext.LastTouchTime > projectContext.LastRenderTime) || (projectContext.LastNTFileEditTime > projectContext.LastRenderTime)) { Interlocked.Increment(ref projectContext.RenderCount); - projectContext.LastRenderTime = new DateTime(Math.Max(projectContext.LastTouchTime.Ticks, projectContext.LastNTFileEditTime.Ticks)); - doRender = true; + projectContext.LastRenderTime = new DateTime(Math.Max(projectContext.LastTouchTime.Ticks, projectContext.LastNTFileEditTime.Ticks)); + doRender = true; } } var report = projectContext.PrepareRaport(); context.ReportDiagnostic(Diagnostic.Create(Diagnostics.ExecuteInfo, Location.None, report)); context.AddSource("diagnostics-sg-last-run.g.cs", report); + report.WriteItDownAndForget("diagnostics-sg-last-run.g.cs"); if (doRender == false) return; @@ -102,7 +105,7 @@ public void Execute(GeneratorExecutionContext context) { context.ReportDiagnostic(Diagnostic.Create(Diagnostics.Exception, Location.None, ex.ToString())); } - AssemblyResolver.DumpLodedAssemblies(); + AssemblyResolver.DumpLodedAssemblies(); } private void DoRender(GeneratorExecutionContext context, ProjectContext projectContext) { @@ -126,7 +129,7 @@ private static ProjectContext GetProjectContext(GeneratorExecutionContext contex { context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ProjectDir", out var projectDir); context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.OutputPath", out var buildOutputPath); - + var assemblyName = context.Compilation.AssemblyName; var outputDir = GetOutputDir(projectDir, buildOutputPath); @@ -140,7 +143,7 @@ private static ProjectContext GetProjectContext(GeneratorExecutionContext contex return ProjectContexts.GetOrAdd(id, _ => new ProjectContext(assemblyName, projectDir, outputDir)); } private static string GetOutputDir(string projectDir, string buildOutputPath) - { + { if (Path.IsPathRooted(buildOutputPath)) { return buildOutputPath; @@ -160,11 +163,19 @@ private static DateTime GetLastEditTime(ImmutableArray additiona var lastEdit = additionalFiles.Max(x => File.GetLastWriteTime(x.Path)); return lastEdit; } - catch + catch { } return DateTime.MinValue; } + private static DateTime GetLastBuildTime(string touchFilePath) + { + if (File.Exists(touchFilePath)) + { + return File.GetLastWriteTime(touchFilePath); + } + return DateTime.Now; + } private sealed class ProjectContext(string assemblyName, string projectDir, string buildOutputDir)