This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated namespaces. * Updated readme. * Added cancellation tokens. * Added source generator options. * Removed options. * Added support for multiple source files. * Updated README.
- Loading branch information
Showing
27 changed files
with
369 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,99 @@ | ||
# AttributeSourceGenerator | ||
|
||
A simple attribute-based Roslyn incremental source generator base class for .NET. | ||
|
||
### Example generator | ||
|
||
```csharp | ||
using AttributeSourceGenerator; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace SourceGenerators; | ||
|
||
[Generator] | ||
public sealed class IdentifierSourceGenerator : AttributeIncrementalGeneratorBase | ||
{ | ||
public IdentifierSourceGenerator() | ||
: base(() => new AttributeIncrementalGeneratorConfiguration() | ||
{ | ||
MarkerAttributeName = MarkerAttributeName, | ||
MarkerAttributeSource = MarkerAttributeSource, | ||
SymbolFilter = FilterType.Struct, | ||
SourceGenerator = GenerateIdentifier | ||
}) | ||
{ | ||
} | ||
|
||
private const string MarkerAttributeNamespace = "Domain.Common.Attributes"; | ||
|
||
private const string MarkerAttributeName = $"{MarkerAttributeNamespace}.GeneratedIdentifierAttribute`1"; | ||
|
||
private static source MarkerAttributeSource = new Source("GeneratedIdentifierAttribute`1", $$""" | ||
namespace {{MarkerAttributeNamespace}}; | ||
[AttributeUsage(AttributeTargets.Struct)] | ||
public sealed class GeneratedIdentifierAttribute<TIdentifier> : Attribute; | ||
"""; | ||
|
||
private static IEnumerable<Source> GenerateIdentifier(Symbol symbol) | ||
{ | ||
return [new Source(symbol.Name, $$""" | ||
// <auto-generated/> | ||
#nullable enable | ||
namespace {{symbol.Namespace}}; | ||
partial struct {{symbol.Name}} : IEquatable<{{symbol.Name}}>, IComparable<{{symbol.Name}}>, IComparable | ||
{ | ||
// Implementation details | ||
} | ||
""")]; | ||
} | ||
} | ||
``` | ||
|
||
### Typical .csproj | ||
|
||
```xml | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
<LangVersion>latest</LangVersion> | ||
<AnalysisLevel>latest-All</AnalysisLevel> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" /> | ||
<PackageReference Include="AttributeSourceGenerator" Version="8.0.2" PrivateAssets="all" GeneratePathProperty="true" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
<IsPublishable>false</IsPublishable> | ||
<IsPackable>true</IsPackable> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<NoWarn>$(NoWarn);NU5128</NoWarn> | ||
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="$(PkgAttributeSourceGenerator)/lib/netstandard2.0/AttributeSourceGenerator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(OutputPath)/$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
</ItemGroup> | ||
|
||
<Target Name="GetDependencyTargetPaths"> | ||
<ItemGroup> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PkgAttributeSourceGenerator)/lib/netstandard2.0/AttributeSourceGenerator.dll" IncludeRuntimeDependency="false" /> | ||
<TargetPathWithTargetPlatformMoniker Include="$(MSBuildThisFileDirectory)bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).dll" IncludeRuntimeDependency="false" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 7 additions & 9 deletions
16
src/AttributeSourceGenerator/AttributeIncrementalGeneratorConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
// ReSharper disable CheckNamespace | ||
|
||
namespace AttributeSourceGenerator; | ||
namespace AttributeSourceGenerator; | ||
|
||
/// <summary>Defines the configuration for an incremental attribute generator.</summary> | ||
public sealed class AttributeIncrementalGeneratorConfiguration | ||
{ | ||
/// <summary>The fully qualified name of the attribute.</summary> | ||
public required string AttributeFullyQualifiedName { get; init; } | ||
/// <summary>The fully qualified name of the marker attribute.</summary> | ||
public required string MarkerAttributeName { get; init; } | ||
|
||
/// <summary>The source for the attribute.</summary> | ||
public string? AttributeSource { get; init; } | ||
/// <summary>The source for the marker attribute.</summary> | ||
public Source? MarkerAttributeSource { get; init; } | ||
|
||
/// <summary>The filter to apply to symbols.</summary> | ||
public FilterType SymbolFilter { get; init; } = FilterType.All; | ||
|
||
/// <summary>The function that generates the source code for the attribute.</summary> | ||
public required Func<Symbol, string> SourceGenerator { get; init; } | ||
public required Func<Symbol, IEnumerable<Source>> SourceGenerator { get; init; } | ||
|
||
/// <summary>Initializes a new instance of the <see cref="AttributeIncrementalGeneratorConfiguration" /> class</summary> | ||
public AttributeIncrementalGeneratorConfiguration() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/AttributeSourceGenerator/AttributeSourceGenerator.projitems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
<HasSharedItems>true</HasSharedItems> | ||
<SharedGUID>dfd4aa47-b465-4c4b-930c-ec98d65bedc3</SharedGUID> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Configuration"> | ||
<Import_RootNamespace>AttributeSourceGenerator</Import_RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\DeclarationExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\EquatableReadOnlyList.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\EquatableReadOnlyList`1.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\GeneratorAttributeSyntaxContextExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\MethodSymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\NamedSymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\SymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Common\TypeSymbolExtensions.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Compiler\CompilerServices.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\ConstructorArgument.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\Declaration.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\GenericTypeArgument.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\MarkerAttributeData.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\MethodParameter.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Models\NamedArgument.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)AttributeIncrementalGeneratorBase.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)AttributeIncrementalGeneratorConfiguration.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)DeclarationType.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)FilterType.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Source.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)Symbol.cs" /> | ||
<Compile Include="$(MSBuildThisFileDirectory)SymbolType.cs" /> | ||
</ItemGroup> | ||
</Project> |
13 changes: 13 additions & 0 deletions
13
src/AttributeSourceGenerator/AttributeSourceGenerator.shproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>dfd4aa47-b465-4c4b-930c-ec98d65bedc3</ProjectGuid> | ||
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion> | ||
</PropertyGroup> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props"/> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props"/> | ||
<PropertyGroup/> | ||
<Import Project="AttributeSourceGenerator.projitems" Label="Shared"/> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.