diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 769e3b4..5de535a 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@  - 1.2.0 + 1.2.1 \ No newline at end of file diff --git a/src/Local.Reference.props b/src/Local.Reference.props index 5121853..b9c3c4d 100644 --- a/src/Local.Reference.props +++ b/src/Local.Reference.props @@ -10,4 +10,18 @@ + + + + <__pluginProjectName Include="SourceGenerator.Foundations.Windows"/> + + + + \ No newline at end of file diff --git a/src/Sandbox/ConsoleApp.SourceGenerator/ConsoleAppSourceGenerator.cs b/src/Sandbox/ConsoleApp.SourceGenerator/ConsoleAppSourceGenerator.cs index cdf2667..ab63150 100644 --- a/src/Sandbox/ConsoleApp.SourceGenerator/ConsoleAppSourceGenerator.cs +++ b/src/Sandbox/ConsoleApp.SourceGenerator/ConsoleAppSourceGenerator.cs @@ -14,6 +14,11 @@ public class Payload public string? Version { get; set; } } + public ConsoleAppSourceGenerator() : base("ConsoleApp") + { + + } + protected override void OnInitialize(IncrementalGeneratorInitializationContext context) { Payload payload = new Payload() diff --git a/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs b/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs index 457288a..50467db 100644 --- a/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs +++ b/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs @@ -46,6 +46,12 @@ static DevelopmentEnviroment() .WriteTo.Sink(s_sinkAggregate) .CreateLogger(); + string assemblyVersion + = typeof(DevelopmentEnviroment) + .Assembly + .GetName() + .Version.ToString(); + Instance = new GenericDevelopmentEnviroment(); AppDomain.CurrentDomain.UnhandledException += OnExceptionThrown; @@ -56,7 +62,8 @@ static DevelopmentEnviroment() if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Assembly windowsEnvironmentAssembly = Assembly.Load(new AssemblyName("SourceGenerator.Foundations.Windows")); + AssemblyName windowsAssemblyName = new AssemblyName($"SourceGenerator.Foundations.Windows, Version={assemblyVersion}, Culture=neutral, PublicKeyToken=null"); + Assembly windowsEnvironmentAssembly = Assembly.Load(windowsAssemblyName); if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("VisualStudioVersion"))) { @@ -77,6 +84,7 @@ static DevelopmentEnviroment() } + /// /// Attaches the debugger to the given process Id /// diff --git a/src/SourceGenerator.Foundations.Contracts/SourceGenerator.Foundations.Contracts.csproj b/src/SourceGenerator.Foundations.Contracts/SourceGenerator.Foundations.Contracts.csproj index c7853c3..a490346 100644 --- a/src/SourceGenerator.Foundations.Contracts/SourceGenerator.Foundations.Contracts.csproj +++ b/src/SourceGenerator.Foundations.Contracts/SourceGenerator.Foundations.Contracts.csproj @@ -12,11 +12,11 @@ - + diff --git a/src/SourceGenerator.Foundations/Generators/ScriptInjectorGenerator.cs b/src/SourceGenerator.Foundations/Generators/ScriptInjectorGenerator.cs index c754235..c101639 100644 --- a/src/SourceGenerator.Foundations/Generators/ScriptInjectorGenerator.cs +++ b/src/SourceGenerator.Foundations/Generators/ScriptInjectorGenerator.cs @@ -13,6 +13,9 @@ namespace SGF.Generators [Generator] internal class ScriptInjectorGenerator : IncrementalGenerator { + public ScriptInjectorGenerator() : base("ScriptInjector") + { } + protected override void OnInitialize(IncrementalGeneratorInitializationContext context) { context.RegisterSourceOutput(context @@ -40,7 +43,6 @@ private void CompilerSource(SourceProductionContext context, (Compilation compil { continue; } - string hintName = resourceName.Replace($"{ResourceConfiguration.ScriptPrefix}", ""); hintName = Path.ChangeExtension(hintName, ".generated.cs"); using Stream stream = assembly.GetManifestResourceStream(resourceName); diff --git a/src/SourceGenerator.Foundations/IncrementalGenerator.cs b/src/SourceGenerator.Foundations/IncrementalGenerator.cs index bf62cf7..a66a182 100644 --- a/src/SourceGenerator.Foundations/IncrementalGenerator.cs +++ b/src/SourceGenerator.Foundations/IncrementalGenerator.cs @@ -1,6 +1,7 @@ #nullable enable using Microsoft.CodeAnalysis; using Serilog; +using SGF.Reflection; using System; using System.Diagnostics; @@ -18,14 +19,16 @@ internal abstract class IncrementalGenerator : IIncrementalGenerator /// public ILogger Logger { get; } + static IncrementalGenerator() + { + AssemblyResolver.Initialize(); + } + /// /// Initializes a new instance of the incremental generator with an optional name /// - protected IncrementalGenerator(string? name = null) + protected IncrementalGenerator(string? name) { - Type type = GetType(); - if (string.IsNullOrWhiteSpace(name)) name = type.FullName; - Logger = DevelopmentEnviroment.Logger.ForContext(GetType()); Logger.Information("Initalizing {GeneratorName}", name ?? GetType().Name); } diff --git a/src/SourceGenerator.Foundations/Reflection/AssemblyResolver.cs b/src/SourceGenerator.Foundations/Reflection/AssemblyResolver.cs index cfade10..7805d66 100644 --- a/src/SourceGenerator.Foundations/Reflection/AssemblyResolver.cs +++ b/src/SourceGenerator.Foundations/Reflection/AssemblyResolver.cs @@ -19,11 +19,17 @@ private enum LogLevel private static readonly IList s_assemblies; private static readonly AssemblyName s_contractsAssemblyName; + private static readonly string s_unpackDirectory; static AssemblyResolver() { s_assemblies = new List(); + s_unpackDirectory = Path.Combine(Path.GetTempPath(), "SourceGenerator.Foundations", "Assemblies"); s_contractsAssemblyName = new AssemblyName(); + if (!Directory.Exists(s_unpackDirectory)) + { + Directory.CreateDirectory(s_unpackDirectory); + } } [ModuleInitializer] @@ -98,40 +104,25 @@ private static void OnAssemblyLoaded(object sender, AssemblyLoadEventArgs args) ManifestResourceInfo resourceInfo = assembly.GetManifestResourceInfo(resourceName); if (resourceInfo != null) { - using Stream stream = assembly.GetManifestResourceStream(resourceName); - byte[] data = new byte[stream.Length]; - _ = stream.Read(data, 0, data.Length); - try - { - Assembly resolvedAssembly = AppDomain.CurrentDomain.Load(data); - - if (resolvedAssembly != null) - { - if (!s_assemblies.Contains(resolvedAssembly)) - { - s_assemblies.Add(resolvedAssembly); - } + string assemblyPath = Path.Combine(s_unpackDirectory, $"{assemblyName.Name}-{assemblyName.Version}.dll"); - return resolvedAssembly; - } - } - catch (Exception exception) + if (!File.Exists(assemblyPath)) { - if (assemblyName != s_contractsAssemblyName) + using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName)) + using (FileStream fileStream = new FileStream(assemblyPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { - // This is redirected to a metho so that it does not attempt to - // load the assembly if it has failed. - Log(exception, LogLevel.Error, "Failed to load assembly {Assembly} due to exception", assemblyName); + resourceStream.CopyTo(fileStream); + fileStream.Flush(); } - return null; } + Assembly resolvedAssembly = Assembly.LoadFile(assemblyPath); + s_assemblies.Add(resolvedAssembly); + return resolvedAssembly; } } return null; } - - /// /// Wrapper around the logging implemention to handle the case where loading the contracts library can actually fail /// diff --git a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj index d4d6bc0..7314f14 100644 --- a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj +++ b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj @@ -1,76 +1,75 @@  - - netstandard2.0 - enable - 9.0 - true - false + + netstandard2.0 + enable + 9.0 + true + false true - true - false - Source Generator Foundations - Byron Mayne - A Source Generator for Source Generators to smooth out the bumps in development. A foucs on removing the boilerplate and improving the debugging experince. - - README.md - https://github.com/ByronMayne/SourceGenerator.Foudations - git - source;generator;csharp - SGF - $(NoWarn);NU5128 - $(TargetsForTfmSpecificContentInPackage);Nuget_AppendContent - $(NoWarn);NU5100 - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + true + false + Source Generator Foundations + Byron Mayne + A Source Generator for Source Generators to smooth out the bumps in development. A foucs on removing the boilerplate and improving the debugging experince. + + README.md + https://github.com/ByronMayne/SourceGenerator.Foudations + git + source;generator;csharp + SGF + $(NoWarn);NU5128 + $(TargetsForTfmSpecificContentInPackage);Nuget_AppendContent + $(NoWarn);NU5100 + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - - - - - - - - - + - - True - / - - - false - True - - - - - - - - - build/$(AssemblyName).props - - + + + + + + + + + + True + / + + + + + + + + + build/$(AssemblyName).props + + - - - + + + \ No newline at end of file diff --git a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props index 72d32b3..f82d712 100644 --- a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props +++ b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props @@ -5,21 +5,30 @@ true false true - $(MSBuildThisFileDirectory)..\environments\ + $(MSBuildThisFileDirectory)..\sgf\embedded\assemblies\ + $(MSBuildThisFileDirectory)..\sgf\embedded\scripts\ $(MSBuildThisFileDirectory)..\lib\netstandard2.0\ false True - + + + + + + + + - + - + + @@ -38,7 +47,8 @@ SGF.Assembly::%(FileName)%(Extension) - + + SGF.Script::%(RecursiveDir)%(FileName)%(Extension)