From 462d3abd3caef5b5fa15c98083278ed01cf94642 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Wed, 25 Jan 2023 02:38:21 -0300 Subject: [PATCH] Allow constants to request a root area different than Constants This opens up other scenarios for specific constants we don't want behind a Constants propery (i.e ThisAssembly.Git, see #69). A small refactoring of the targets with a new PrepareConstants will also make it easier for extenders to run before we do our conversion to AdditionalFiles, to keep the extended items clean even when they need to run before/after certain targets. --- .../ConstantsGenerator.cs | 16 ++++++++++------ .../ThisAssembly.Constants.targets | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index 871c2c08..19d51aea 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -24,7 +24,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Value", out var value); x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Comment", out var comment); - return (name: Path.GetFileName(x.Left.Path), value: value!, comment: string.IsNullOrWhiteSpace(comment) ? null : comment); + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Root", out var root); + return ( + name: Path.GetFileName(x.Left.Path), + value: value!, + comment: string.IsNullOrWhiteSpace(comment) ? null : comment, + root: string.IsNullOrWhiteSpace(root) ? "Constants" : root!); }) .Combine(context.CompilationProvider.Select((p, _) => p.Language)); @@ -34,14 +39,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context) } - void GenerateConstant(SourceProductionContext spc, ((string name, string value, string? comment), string language) arg2) + void GenerateConstant(SourceProductionContext spc, ((string name, string value, string? comment, string root), string language) args) { - var ((name, value, comment), language) = arg2; + var ((name, value, comment, root), language) = args; - var root = Area.Load(new List { new Constant(name, value, comment), }); + var rootArea = Area.Load(new List { new Constant(name, value, comment), }, root); var file = language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); - var output = template.Render(new Model(root), member => member.Name); + var output = template.Render(new Model(rootArea), member => member.Name); // Apply formatting since indenting isn't that nice in Scriban when rendering nested // structures via functions. @@ -61,7 +66,6 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value, //} spc.AddSource($"{name}.g.cs", SourceText.From(output, Encoding.UTF8)); - } } } diff --git a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets index 3aa71bbf..64e3478f 100644 --- a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets +++ b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets @@ -6,6 +6,7 @@ + @@ -17,9 +18,8 @@ - + + %(RelativeDir)%(Filename) @@ -31,10 +31,17 @@ $([MSBuild]::ValueOrDefault('%(AreaPath)', '').Replace('\', '.').Replace('/', '.')) %(AreaPath)%(FileExtension) - + + + + +