Skip to content

Commit

Permalink
Refactored the properties for the source generator
Browse files Browse the repository at this point in the history
They were not building correctly in gitlab.
  • Loading branch information
ByronMayne committed Jan 21, 2024
1 parent d7cd221 commit 0890c8f
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 84 deletions.
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<!-- Paths | SGF Plugin Window -->
<SGFWindowsPluginProjectDir>$(SGFSourceDir)Plugins\SourceGenerator.Foundations.Windows\</SGFWindowsPluginProjectDir>
<SGFWindowsPluginProjectPath>$(SGFWindowsPluginProjectDir)SourceGenerator.Foundations.Windows.csproj</SGFWindowsPluginProjectPath>

<!-- Local Path Overrides | Both a local build and the nuget package use the same logic. This is used to override the paths when building locally -->
<SGFMsBuildPath>$(SGFMSBuildProjectDir)bin\$(Configuration)\netstandard2.0\SourceGenerator.Foundations.MSBuild.dll</SGFMsBuildPath>
<SgfInjectorPath>$(SGFInjectorProjectDir)bin\$(Configuration)\net6.0\SourceGenerator.Foundations.Injector.exe</SgfInjectorPath>
</PropertyGroup>
<!-- Debug Targets -->
<Target Name="PrintSGFPaths">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
using System;
using System.Collections.Generic;
using System.Text;
using SGF.Diagnostics;
using System;

namespace SGF.Diagnostics
/// <summary>
/// Contains extension methods for working with logging in Source Generator Foundations
/// </summary>
public static class LoggerExtensions
{
public static class LoggerExtensions
public static void AddSink<T>(this ILogger logger) where T : ILogSink, new()
{
public static void AddSink<T>(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);
}
/// <summary>
/// Adds a new info log entry
/// </summary>
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);
}
/// <summary>
/// Adds a new info log entry with an exception
/// </summary>
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);
}
/// <summary>
/// Adds a new warning log entry
/// </summary>
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);
}
/// <summary>
/// Adds a new warning entry with an exception
/// </summary>
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);
}
/// <summary>
/// Adds a new warning entry
/// </summary>
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);
}
/// <summary>
/// Adds a new error entry with an exception
/// </summary>
public static void Error(this ILogger logger, Exception exception, string message)
{
logger.Log(LogLevel.Error, exception, message);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext
{
try
{
SgfInitializationContext sgfContext = new(context, OnException);
SgfInitializationContext sgfContext = new(context, Logger, OnException);

OnInitialize(sgfContext);
}
Expand Down
61 changes: 36 additions & 25 deletions src/SourceGenerator.Foundations.Shared/SgfInitializationContext.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#nullable enable
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using SGF.Diagnostics;
using System;
using System.Reflection;

namespace SGF
{
/// <summary>
/// Middleware wrapper around a <see cref="IncrementalGeneratorInitializationContext"/> to allow for
/// wraping with exception handling and provide a better user expereince
/// </summary>
internal struct SgfInitializationContext
internal readonly struct SgfInitializationContext
{
private readonly ILogger m_logger;
private readonly Action<Exception> m_exceptionHandler;
private IncrementalGeneratorInitializationContext m_context;
private readonly IncrementalGeneratorInitializationContext m_context;

public SyntaxValueProvider SyntaxProvider => m_context.SyntaxProvider;
public IncrementalValueProvider<Compilation> CompilationProvider => m_context.CompilationProvider;
Expand All @@ -21,84 +23,93 @@ internal struct SgfInitializationContext
public IncrementalValueProvider<AnalyzerConfigOptionsProvider> AnalyzerConfigOptionsProvider => m_context.AnalyzerConfigOptionsProvider;
public IncrementalValuesProvider<MetadataReference> MetadataReferencesProvider => m_context.MetadataReferencesProvider;

public SgfInitializationContext(IncrementalGeneratorInitializationContext context, Action<Exception> exceptionHandler)
public SgfInitializationContext(
IncrementalGeneratorInitializationContext context,
ILogger logger,
Action<Exception> exceptionHandler)
{
m_logger = logger;
m_context = context;
m_exceptionHandler = exceptionHandler;
}

public void RegisterSourceOutput<TSource>(IncrementalValueProvider<TSource> source, Action<SourceProductionContext, TSource> action)
public void RegisterSourceOutput<TSource>(IncrementalValueProvider<TSource> source, Action<SgfSourceProductionContext, TSource> action)
{
ILogger logger = m_logger;
Action<Exception> exceptionHandler = m_exceptionHandler;
Action<SourceProductionContext, TSource> 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<TSource>(IncrementalValuesProvider<TSource> source, Action<SourceProductionContext, TSource> action)
public void RegisterSourceOutput<TSource>(IncrementalValuesProvider<TSource> source, Action<SgfSourceProductionContext, TSource> action)
{
ILogger logger = m_logger;
Action<Exception> exceptionHandler = m_exceptionHandler;
Action<SourceProductionContext, TSource> 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<TSource>(IncrementalValueProvider<TSource> source, Action<SourceProductionContext, TSource> action)
public void RegisterImplementationSourceOutput<TSource>(IncrementalValueProvider<TSource> source, Action<SgfSourceProductionContext, TSource> action)
{
ILogger logger = m_logger;
Action<Exception> exceptionHandler = m_exceptionHandler;
Action<SourceProductionContext, TSource> 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<TSource>(IncrementalValuesProvider<TSource> source, Action<SourceProductionContext, TSource> action)
public void RegisterImplementationSourceOutput<TSource>(IncrementalValuesProvider<TSource> source, Action<SgfSourceProductionContext, TSource> action)
{
ILogger logger = m_logger;
Action<Exception> exceptionHandler = m_exceptionHandler;
Action<SourceProductionContext, TSource> 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<IncrementalGeneratorPostInitializationContext> callback)
{
Action<Exception> exceptionHandler = m_exceptionHandler;
Action<IncrementalGeneratorPostInitializationContext> wrappedCallback = (context) =>
void wrappedCallback(IncrementalGeneratorPostInitializationContext context)
{
try
{
Expand All @@ -108,7 +119,7 @@ public void RegisterPostInitializationOutput(Action<IncrementalGeneratorPostInit
{
exceptionHandler(exception);
}
};
}
m_context.RegisterPostInitializationOutput(wrappedCallback);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis;
using SGF.Diagnostics;
using System.Text;
using System.Threading;
/// <summary>
/// Wrapper around a <see cref="SourceProductionContext"/> used to help capture errors and report logs
/// </summary>
internal struct SgfSourceProductionContext
{
private int m_sourceCount;
private readonly ILogger m_logger;
private readonly SourceProductionContext m_context;

/// <summary>
/// A token that will be cancelled when generation should stop
/// </summary>
public CancellationToken CancellationToken => m_context.CancellationToken;

internal SgfSourceProductionContext(SourceProductionContext context, ILogger logger)
{
m_sourceCount = 0;
m_logger = logger;
m_context = context;
}



/// <summary>
/// Adds source code in the form of a <see cref="string"/> to the compilation.
/// </summary>
/// <param name="hintName">An identifier that can be used to reference this source text, must be unique within this generator</param>
/// <param name="source">The source code to add to the compilation</param>
public void AddSource(string hintName, string source) => AddSource(hintName, SourceText.From(source, Encoding.UTF8));

/// <summary>
/// Adds a <see cref="SourceText"/> to the compilation
/// </summary>
/// <param name="hintName">An identifier that can be used to reference this source text, must be unique within this generator</param>
/// <param name="sourceText">The <see cref="SourceText"/> to add to the compilation</param>
public void AddSource(string hintName, SourceText sourceText)
{
m_sourceCount++;
m_logger.Information($" [AddedSource:{m_sourceCount:00}]: {hintName}");
m_context.AddSource(hintName, sourceText);
}

/// <summary>
/// Adds a <see cref="Diagnostic"/> to the users compilation
/// </summary>
/// <param name="diagnostic">The diagnostic that should be added to the compilation</param>
/// <remarks>
/// The severity of the diagnostic may cause the compilation to fail, depending on the <see cref="Compilation"/> settings.
/// </remarks>
public void ReportDiagnostic(Diagnostic diagnostic) => m_context.ReportDiagnostic(diagnostic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Reflection\AssemblyResolver.cs" Pack="True" PackagePath="sgf/src/Reflection\AssemblyResolver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IncrementalGenerator.cs" Pack="True" PackagePath="sgf/src/IncrementalGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SgfInitializationContext.cs" Pack="True" PackagePath="sgf/src/SgfInitializationContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SgfSourceProductionContext.cs" Pack="True" PackagePath="sgf/src/SgfSourceProductionContext.cs" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions src/SourceGenerator.Foundations.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0890c8f

Please sign in to comment.