Skip to content

Commit

Permalink
NTypewriter.SourceGenerator v0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Mar 8, 2024
1 parent af6cc87 commit 1e0b2c5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
31 changes: 19 additions & 12 deletions NTypewriter.SourceGenerator/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
//}
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
28 changes: 28 additions & 0 deletions NTypewriter.SourceGenerator/Extensions/System/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<IsRoslynComponent>true</IsRoslynComponent>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
<Version>0.4.1</Version>
<Version>0.4.3</Version>
<PackageIcon>nt.128.128.png</PackageIcon>
<Authors>NeVeSpl</Authors>
<Company>NeVeSpl</Company>
Expand Down
33 changes: 22 additions & 11 deletions NTypewriter.SourceGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
using NTypewriter.Editor.Config;
using NTypewriter.Runtime;
using NTypewriter.SourceGenerator.Adapters;
using NTypewriter.SourceGenerator.Extensions.System;
using Scriban;

namespace NTypewriter.SourceGenerator
{
[Generator]
public class NTypewriterSourceGenerator : ISourceGenerator
{
private static readonly ConcurrentDictionary<string, ProjectContext> ProjectContexts = new();
private static readonly ConcurrentDictionary<string, ProjectContext> ProjectContexts = new();
private static readonly object Padlock = new();


Expand Down Expand Up @@ -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");
}


Expand All @@ -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;

Expand All @@ -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)
{
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -160,11 +163,19 @@ private static DateTime GetLastEditTime(ImmutableArray<AdditionalText> 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)
Expand Down

0 comments on commit 1e0b2c5

Please sign in to comment.