diff --git a/content/srcgen-fba/.template.config/template.json b/content/srcgen-fba/.template.config/template.json new file mode 100644 index 0000000..05e303e --- /dev/null +++ b/content/srcgen-fba/.template.config/template.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/template", + "author": "naratteu", + "classifications": [ "SourceGenerator", "FBA" ], + "identity": "srcgen-fba", + "name": "SourceGenerator (File-based App)", + "shortName": "srcgen-fba", + "sourceName": "Sample", + "preferNameDirectory": true, + "tags": { + "language": "C#", + "type": "project" + } +} \ No newline at end of file diff --git a/content/srcgen-fba/Sample.Test.cs b/content/srcgen-fba/Sample.Test.cs new file mode 100644 index 0000000..9f661a7 --- /dev/null +++ b/content/srcgen-fba/Sample.Test.cs @@ -0,0 +1,12 @@ +#:package Sample@0.0.1 +using Sample; + +Hello.World(); + +[Friend] class Github; +[Friend] class DotnetDev; + +/* +dotnet pack Sample.cs -p:Version=0.0.1 +dotnet Sample.Test.cs +*/ \ No newline at end of file diff --git a/content/srcgen-fba/Sample.cs b/content/srcgen-fba/Sample.cs new file mode 100644 index 0000000..a749c13 --- /dev/null +++ b/content/srcgen-fba/Sample.cs @@ -0,0 +1,39 @@ +#:sdk Naratteu.SourceGenerator.Sdk@0.0.11 + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; +using System.Diagnostics.CodeAnalysis; +using System.Text; + +[Generator] +public class SampleGenerator : IIncrementalGenerator +{ + void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext context) + { + context.RegisterPostInitializationOutput(ctx => + { + ctx.AddEmbeddedAttributeDefinition(); + ctx.AddSource("FriendAttribute.g.cs", """ + namespace Sample + { + [Microsoft.CodeAnalysis.Embedded] + public class FriendAttribute : System.Attribute; + } + """); + }); + context.RegisterSourceOutput( + context.SyntaxProvider.ForAttributeWithMetadataName("Sample.FriendAttribute", (_, _) => true, (c, _) => c).Collect(), + (ctx, sources) => + { + ctx.AddSource($"Hello.g.cs", $$""" + namespace Sample + { + public static class Hello + { + public static void World() => System.Console.WriteLine("Hello {{string.Join(", ", sources.Select(s => s.TargetSymbol.Name))}} !"); + } + } + """); + }); + } +} \ No newline at end of file diff --git a/content/srcgen-fba/nuget.config b/content/srcgen-fba/nuget.config new file mode 100644 index 0000000..12dd92d --- /dev/null +++ b/content/srcgen-fba/nuget.config @@ -0,0 +1,6 @@ + + + + + +