diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 19668dd..120e862 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -26,6 +26,10 @@ $(SGFSourceDir)Plugins\SourceGenerator.Foundations.Windows\ $(SGFWindowsPluginProjectDir)SourceGenerator.Foundations.Windows.csproj + + + $(SGFMSBuildProjectDir)bin\$(Configuration)\netstandard2.0\SourceGenerator.Foundations.MSBuild.dll + $(SGFInjectorProjectDir)bin\$(Configuration)\net6.0\SourceGenerator.Foundations.Injector.exe diff --git a/src/SourceGenerator.Foundations.Contracts/Diagnostics/LoggerExtensions.cs b/src/SourceGenerator.Foundations.Contracts/Diagnostics/LoggerExtensions.cs index 075b34a..2d93448 100644 --- a/src/SourceGenerator.Foundations.Contracts/Diagnostics/LoggerExtensions.cs +++ b/src/SourceGenerator.Foundations.Contracts/Diagnostics/LoggerExtensions.cs @@ -1,44 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Text; +using SGF.Diagnostics; +using System; -namespace SGF.Diagnostics +/// +/// Contains extension methods for working with logging in Source Generator Foundations +/// +public static class LoggerExtensions { - public static class LoggerExtensions + public static void AddSink(this ILogger logger) where T : ILogSink, new() { - public static void AddSink(this ILogger logger) where T: ILogSink, new() - { - logger.AddSink(new T()); - } + logger.AddSink(new T()); + } - public static void Information(this ILogger logger, string message) - { - logger.Log(LogLevel.Information, null, message); - } + /// + /// Adds a new info log entry + /// + public static void Information(this ILogger logger, string message) + { + logger.Log(LogLevel.Information, null, message); + } - public static void Information(this ILogger logger, Exception exception, string message) - { - logger.Log(LogLevel.Information, exception, message); - } + /// + /// Adds a new info log entry with an exception + /// + public static void Information(this ILogger logger, Exception exception, string message) + { + logger.Log(LogLevel.Information, exception, message); + } - public static void Warning(this ILogger logger, string message) - { - logger.Log(LogLevel.Warning, null, message); - } + /// + /// Adds a new warning log entry + /// + public static void Warning(this ILogger logger, string message) + { + logger.Log(LogLevel.Warning, null, message); + } - public static void Warning(this ILogger logger, Exception exception, string message) - { - logger.Log(LogLevel.Warning, exception, message); - } + /// + /// Adds a new warning entry with an exception + /// + public static void Warning(this ILogger logger, Exception exception, string message) + { + logger.Log(LogLevel.Warning, exception, message); + } - public static void Error(this ILogger logger, string message) - { - logger.Log(LogLevel.Error, null, message); - } + /// + /// Adds a new warning entry + /// + public static void Error(this ILogger logger, string message) + { + logger.Log(LogLevel.Error, null, message); + } - public static void Error(this ILogger logger, Exception exception, string message) - { - logger.Log(LogLevel.Error, exception, message); - } + /// + /// Adds a new error entry with an exception + /// + public static void Error(this ILogger logger, Exception exception, string message) + { + logger.Log(LogLevel.Error, exception, message); } -} +} \ No newline at end of file diff --git a/src/SourceGenerator.Foundations.MSBuild/SourceGenerator.Foundations.MSBuild.targets b/src/SourceGenerator.Foundations.MSBuild/SourceGenerator.Foundations.MSBuild.targets deleted file mode 100644 index 749d1c6..0000000 --- a/src/SourceGenerator.Foundations.MSBuild/SourceGenerator.Foundations.MSBuild.targets +++ /dev/null @@ -1,11 +0,0 @@ - - - false - true - $(MSBuildThisFileDirectory)bin/$(Configuration)\netstandard2.0\$(MSBuildThisFileName).dll - $(MSBuildThisFileName).dll - - - \ No newline at end of file diff --git a/src/SourceGenerator.Foundations.Shared/IncrementalGenerator.cs b/src/SourceGenerator.Foundations.Shared/IncrementalGenerator.cs index 1085994..5df0206 100644 --- a/src/SourceGenerator.Foundations.Shared/IncrementalGenerator.cs +++ b/src/SourceGenerator.Foundations.Shared/IncrementalGenerator.cs @@ -184,7 +184,7 @@ void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext { try { - SgfInitializationContext sgfContext = new(context, OnException); + SgfInitializationContext sgfContext = new(context, Logger, OnException); OnInitialize(sgfContext); } diff --git a/src/SourceGenerator.Foundations.Shared/SgfInitializationContext.cs b/src/SourceGenerator.Foundations.Shared/SgfInitializationContext.cs index d585aea..83b93da 100644 --- a/src/SourceGenerator.Foundations.Shared/SgfInitializationContext.cs +++ b/src/SourceGenerator.Foundations.Shared/SgfInitializationContext.cs @@ -1,7 +1,8 @@ -#nullable enable -using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; +using SGF.Diagnostics; using System; +using System.Reflection; namespace SGF { @@ -9,10 +10,11 @@ namespace SGF /// Middleware wrapper around a to allow for /// wraping with exception handling and provide a better user expereince /// - internal struct SgfInitializationContext + internal readonly struct SgfInitializationContext { + private readonly ILogger m_logger; private readonly Action m_exceptionHandler; - private IncrementalGeneratorInitializationContext m_context; + private readonly IncrementalGeneratorInitializationContext m_context; public SyntaxValueProvider SyntaxProvider => m_context.SyntaxProvider; public IncrementalValueProvider CompilationProvider => m_context.CompilationProvider; @@ -21,84 +23,93 @@ internal struct SgfInitializationContext public IncrementalValueProvider AnalyzerConfigOptionsProvider => m_context.AnalyzerConfigOptionsProvider; public IncrementalValuesProvider MetadataReferencesProvider => m_context.MetadataReferencesProvider; - public SgfInitializationContext(IncrementalGeneratorInitializationContext context, Action exceptionHandler) + public SgfInitializationContext( + IncrementalGeneratorInitializationContext context, + ILogger logger, + Action exceptionHandler) { + m_logger = logger; m_context = context; m_exceptionHandler = exceptionHandler; } - public void RegisterSourceOutput(IncrementalValueProvider source, Action action) + public void RegisterSourceOutput(IncrementalValueProvider source, Action action) { + ILogger logger = m_logger; Action exceptionHandler = m_exceptionHandler; - Action wrappedAction = (context, source) => + + void wrappedAction(SourceProductionContext context, TSource source) { try { - action(context, source); + action(new (context, logger), source); } - catch(Exception exception) + catch (Exception exception) { exceptionHandler(exception); } - }; + } m_context.RegisterSourceOutput(source, wrappedAction); } - public void RegisterSourceOutput(IncrementalValuesProvider source, Action action) + public void RegisterSourceOutput(IncrementalValuesProvider source, Action action) { + ILogger logger = m_logger; Action exceptionHandler = m_exceptionHandler; - Action wrappedAction = (context, source) => + void wrappedAction(SourceProductionContext context, TSource source) { try { - action(context, source); + action(new (context, logger), source); } - catch(Exception exception) + catch (Exception exception) { exceptionHandler(exception); } - }; + } m_context.RegisterSourceOutput(source, wrappedAction); } - public void RegisterImplementationSourceOutput(IncrementalValueProvider source, Action action) + public void RegisterImplementationSourceOutput(IncrementalValueProvider source, Action action) { + ILogger logger = m_logger; Action exceptionHandler = m_exceptionHandler; - Action wrappedAction = (context, source) => + void wrappedAction(SourceProductionContext context, TSource source) { try { - action(context, source); + action(new(context, logger), source); } catch (Exception exception) { exceptionHandler(exception); } - }; + } m_context.RegisterImplementationSourceOutput(source, wrappedAction); } - public void RegisterImplementationSourceOutput(IncrementalValuesProvider source, Action action) + public void RegisterImplementationSourceOutput(IncrementalValuesProvider source, Action action) { + ILogger logger = m_logger; Action exceptionHandler = m_exceptionHandler; - Action wrappedAction = (context, source) => + void wrappedAction(SourceProductionContext context, TSource source) { try { - action(context, source); + action(new (context, logger), source); } catch (Exception exception) { exceptionHandler(exception); } - }; + } m_context.RegisterImplementationSourceOutput(source, wrappedAction); } public void RegisterPostInitializationOutput(Action callback) { Action exceptionHandler = m_exceptionHandler; - Action wrappedCallback = (context) => + void wrappedCallback(IncrementalGeneratorPostInitializationContext context) { try { @@ -108,7 +119,7 @@ public void RegisterPostInitializationOutput(Action +/// Wrapper around a used to help capture errors and report logs +/// +internal struct SgfSourceProductionContext +{ + private int m_sourceCount; + private readonly ILogger m_logger; + private readonly SourceProductionContext m_context; + + /// + /// A token that will be cancelled when generation should stop + /// + public CancellationToken CancellationToken => m_context.CancellationToken; + + internal SgfSourceProductionContext(SourceProductionContext context, ILogger logger) + { + m_sourceCount = 0; + m_logger = logger; + m_context = context; + } + + + + /// + /// Adds source code in the form of a to the compilation. + /// + /// An identifier that can be used to reference this source text, must be unique within this generator + /// The source code to add to the compilation + public void AddSource(string hintName, string source) => AddSource(hintName, SourceText.From(source, Encoding.UTF8)); + + /// + /// Adds a to the compilation + /// + /// An identifier that can be used to reference this source text, must be unique within this generator + /// The to add to the compilation + public void AddSource(string hintName, SourceText sourceText) + { + m_sourceCount++; + m_logger.Information($" [AddedSource:{m_sourceCount:00}]: {hintName}"); + m_context.AddSource(hintName, sourceText); + } + + /// + /// Adds a to the users compilation + /// + /// The diagnostic that should be added to the compilation + /// + /// The severity of the diagnostic may cause the compilation to fail, depending on the settings. + /// + public void ReportDiagnostic(Diagnostic diagnostic) => m_context.ReportDiagnostic(diagnostic); +} \ No newline at end of file diff --git a/src/SourceGenerator.Foundations.Shared/SourceGenerator.Foundations.Shared.projitems b/src/SourceGenerator.Foundations.Shared/SourceGenerator.Foundations.Shared.projitems index bc11776..e781bb5 100644 --- a/src/SourceGenerator.Foundations.Shared/SourceGenerator.Foundations.Shared.projitems +++ b/src/SourceGenerator.Foundations.Shared/SourceGenerator.Foundations.Shared.projitems @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/src/SourceGenerator.Foundations.sln b/src/SourceGenerator.Foundations.sln index 8e46947..19e58e4 100644 --- a/src/SourceGenerator.Foundations.sln +++ b/src/SourceGenerator.Foundations.sln @@ -29,6 +29,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp.SourceGenerator" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator.Foundations.Injector", "SourceGenerator.Foundations.Injector\SourceGenerator.Foundations.Injector.csproj", "{E9B785CC-B19E-499F-915D-7E37F727C50C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator.Foundations.MSBuild", "SourceGenerator.Foundations.MSBuild\SourceGenerator.Foundations.MSBuild.csproj", "{E95587D6-78E8-48FE-9F98-371800A77B69}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{F0124C8F-B8FB-4832-9D65-D0F13E4308AE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -85,6 +89,14 @@ Global {E9B785CC-B19E-499F-915D-7E37F727C50C}.Release|Any CPU.Build.0 = Release|Any CPU {E9B785CC-B19E-499F-915D-7E37F727C50C}.Release|x64.ActiveCfg = Release|Any CPU {E9B785CC-B19E-499F-915D-7E37F727C50C}.Release|x64.Build.0 = Release|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Debug|x64.ActiveCfg = Debug|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Debug|x64.Build.0 = Debug|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Release|Any CPU.Build.0 = Release|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Release|x64.ActiveCfg = Release|Any CPU + {E95587D6-78E8-48FE-9F98-371800A77B69}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj index 58105b6..e4b9de5 100644 --- a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj +++ b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.csproj @@ -1,10 +1,11 @@  - + netstandard2.0 true false true + $(TargetsForTfmSpecificContentInPackage);CustomNugetPack @@ -17,6 +18,7 @@ + TargetFramework=net6.0 @@ -24,4 +26,12 @@ + + + + + build/ + + + \ No newline at end of file diff --git a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props index 5d51f41..32e801d 100644 --- a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props +++ b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.props @@ -11,6 +11,8 @@ true false true + $(MSBuildThisFileDirectory)SourceGenerator.Foundations.MSBuild.dll + $(MSBuildThisFileDirectory)SourceGenerator.Foundations.Injector.exe diff --git a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.targets b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.targets index 455fccc..4be6795 100644 --- a/src/SourceGenerator.Foundations/SourceGenerator.Foundations.targets +++ b/src/SourceGenerator.Foundations/SourceGenerator.Foundations.targets @@ -5,15 +5,10 @@ from Directory.Build.[props|targets] as these files won't exist. ==================================================--> - - $(MSBuildThisFileDirectory)..\sgf\injector\SourceGenerator.Foundations.Injector.exe - $(SGFInjectorProjectDir)bin\$(Configuration)\net6.0\SourceGenerator.Foundations.Injector.exe - - - + - + @@ -28,14 +23,18 @@ - + - - + + - - + + \ No newline at end of file