|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.Text; |
| 8 | +using Scriban; |
| 9 | + |
| 10 | +namespace ThisAssembly |
| 11 | +{ |
| 12 | + [Generator(LanguageNames.CSharp)] |
| 13 | + public class ResourcesGenerator : IIncrementalGenerator |
| 14 | + { |
| 15 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 16 | + { |
| 17 | + context.RegisterPostInitializationOutput( |
| 18 | + spc => spc.AddSource( |
| 19 | + "ThisAssembly.Resources.EmbeddedResource.cs", |
| 20 | + SourceText.From(EmbeddedResource.GetContent("EmbeddedResource.cs"), Encoding.UTF8))); |
| 21 | + |
| 22 | + var files = context.AdditionalTextsProvider |
| 23 | + .Combine(context.AnalyzerConfigOptionsProvider) |
| 24 | + .Where(x => |
| 25 | + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.AdditionalFiles.SourceItemType", out var itemType) |
| 26 | + && itemType == "EmbeddedResource") |
| 27 | + .Where(x => x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Value", out var value) && value != null) |
| 28 | + .Select((x, ct) => |
| 29 | + { |
| 30 | + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Value", out var resourceName); |
| 31 | + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Kind", out var kind); |
| 32 | + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Comment", out var comment); |
| 33 | + return (resourceName!, kind, comment: string.IsNullOrWhiteSpace(comment) ? null : comment); |
| 34 | + }) |
| 35 | + .Combine(context.AnalyzerConfigOptionsProvider |
| 36 | + .Select((p, _) => |
| 37 | + { |
| 38 | + p.GlobalOptions.TryGetValue("build_property.EmbeddedResourceStringExtensions", out var extensions); |
| 39 | + return extensions!; |
| 40 | + })); |
| 41 | + |
| 42 | + context.RegisterSourceOutput( |
| 43 | + files, |
| 44 | + GenerateSource); |
| 45 | + } |
| 46 | + |
| 47 | + static void GenerateSource(SourceProductionContext spc, ((string resourceName, string? kind, string? comment), string extensions) arg2) |
| 48 | + { |
| 49 | + var ((resourceName, kind, comment), extensions) = arg2; |
| 50 | + |
| 51 | + var file = "CSharp.sbntxt"; |
| 52 | + var template = Template.Parse(EmbeddedResource.GetContent(file), file); |
| 53 | + |
| 54 | + var isText = kind != null && kind.Equals("text", StringComparison.OrdinalIgnoreCase) |
| 55 | + || extensions.Split(';').Contains(Path.GetFileName(resourceName)); |
| 56 | + var root = Area.Load(new Resource(resourceName, comment, isText)); |
| 57 | + var model = new Model(root); |
| 58 | + |
| 59 | + var output = template.Render(model, member => member.Name); |
| 60 | + |
| 61 | + // Apply formatting since indenting isn't that nice in Scriban when rendering nested |
| 62 | + // structures via functions. |
| 63 | + output = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit(output) |
| 64 | + .NormalizeWhitespace() |
| 65 | + .GetText() |
| 66 | + .ToString(); |
| 67 | + |
| 68 | + spc.AddSource( |
| 69 | + $"{resourceName.Replace('\\', '.').Replace('/', '.')}.g.cs", |
| 70 | + SourceText.From(output, Encoding.UTF8)); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments