From eae9ec15884b3f02ee86ec62a07fc69e1a0355fc Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Thu, 26 Sep 2024 23:27:51 -0300 Subject: [PATCH] Add support for typed constants Fixes #150 --- src/ThisAssembly.Constants/CSharp.sbntxt | 10 ++++- .../ConstantsGenerator.cs | 9 ++-- src/ThisAssembly.Constants/Model.cs | 5 ++- .../ThisAssembly.Constants.targets | 1 + src/ThisAssembly.Constants/readme.md | 41 ++++++++++++++++++- src/ThisAssembly.Tests/Tests.cs | 20 +++++++++ .../ThisAssembly.Tests.csproj | 5 +++ 7 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/ThisAssembly.Constants/CSharp.sbntxt b/src/ThisAssembly.Constants/CSharp.sbntxt index 98cd71d7..f9c0ab07 100644 --- a/src/ThisAssembly.Constants/CSharp.sbntxt +++ b/src/ThisAssembly.Constants/CSharp.sbntxt @@ -40,13 +40,19 @@ {{- summary value -}} {{- remarks -}} {{ obsolete }} - {{~ if RawStrings ~}} + {{~ if RawStrings && value.IsText ~}} public const string {{ value.Name | string.replace "-" "_" | string.replace " " "_" }} = + """ {{ value.Value }} """; {{~ else ~}} - public const string {{ value.Name | string.replace "-" "_" | string.replace " " "_" }} = @"{{ value.Value }}"; + public const {{ value.Type }} {{ value.Name | string.replace "-" "_" | string.replace " " "_" }} = + {{~ if value.IsText ~}} + @"{{ value.Value }}"; + {{~ else ~}} + {{ value.Value }}; + {{~ end ~}} {{~ end ~}} {{~ end ~}} diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index eb6e8cfc..0d040753 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -26,6 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .Select((x, ct) => { x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Value", out var value); + x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Type", out var type); x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Comment", out var comment); x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Root", out var root); @@ -52,7 +53,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) } } - return (name, value: value ?? "", comment: string.IsNullOrWhiteSpace(comment) ? null : comment, root!); + return (name, value: value ?? "", type: string.IsNullOrWhiteSpace(type) ? null : type, comment: string.IsNullOrWhiteSpace(comment) ? null : comment, root!); }); // Read the ThisAssemblyNamespace property or default to null @@ -68,9 +69,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context) } void GenerateConstant(SourceProductionContext spc, - (((string name, string value, string? comment, string root), (string? ns, ParseOptions parse)), StatusOptions options) args) + (((string name, string value, string? type, string? comment, string root), (string? ns, ParseOptions parse)), StatusOptions options) args) { - var (((name, value, comment, root), (ns, parse)), options) = args; + var (((name, value, type, comment, root), (ns, parse)), options) = args; var cs = (CSharpParseOptions)parse; if (!string.IsNullOrWhiteSpace(ns) && @@ -89,7 +90,7 @@ void GenerateConstant(SourceProductionContext spc, comment = "/// " + string.Join(Environment.NewLine + "/// ", value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None)); // Revert normalization of newlines performed in MSBuild to workaround the limitation in editorconfig. - var rootArea = Area.Load([new(name, value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']), comment),], root); + var rootArea = Area.Load([new(name, value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']), comment, type ?? "string"),], root); // For now, we only support C# though var file = parse.Language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); diff --git a/src/ThisAssembly.Constants/Model.cs b/src/ThisAssembly.Constants/Model.cs index 9e7f3cde..ed736678 100644 --- a/src/ThisAssembly.Constants/Model.cs +++ b/src/ThisAssembly.Constants/Model.cs @@ -105,4 +105,7 @@ static Area GetArea(Area area, IEnumerable areaPath) } [DebuggerDisplay("{Name} = {Value}")] -record Constant(string Name, string? Value, string? Comment); +record Constant(string Name, string? Value, string? Comment, string Type = "string") +{ + public bool IsText => Type.Equals("string", StringComparison.OrdinalIgnoreCase); +} diff --git a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets index 0ecb711f..5e0f167b 100644 --- a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets +++ b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets @@ -7,6 +7,7 @@ +