GithubHelp home page GithubHelp logo

jscarle / attributesourcegenerator Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 39 KB

A simple attribute-based Roslyn incremental source generator base class for .NET.

License: MIT License

C# 100.00%
charp dotnet source-generator source-generators attribute attribute-based

attributesourcegenerator's Introduction

AttributeSourceGenerator

A simple attribute-based Roslyn incremental source generator base class for .NET.

main nuget downloads

Example generator

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

<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>

attributesourcegenerator's People

Contributors

jscarle avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.