From 874e813bbb9f55730748cd88ff081de7b0246c6d Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Fri, 13 Sep 2024 11:48:54 -0300 Subject: [PATCH] Make project constants a special case of Constants Just like metadata, git and assembly info. This completes the unification of all packages, so that the specific ones just make configuring things in MSBuild easier by providing automatic conversions to @(Constant) items. --- .../ConstantsGenerator.cs | 31 +++++++--- .../ThisAssembly.Metadata.targets | 2 +- src/ThisAssembly.Project/CSharp.sbntxt | 40 ------------- src/ThisAssembly.Project/Model.cs | 18 ------ .../ProjectPropertyGenerator.cs | 59 ------------------- .../ThisAssembly.Project.targets | 16 ++--- src/ThisAssembly.Project/Visual Basic.sbntxt | 28 --------- 7 files changed, 30 insertions(+), 164 deletions(-) delete mode 100644 src/ThisAssembly.Project/CSharp.sbntxt delete mode 100644 src/ThisAssembly.Project/Model.cs delete mode 100644 src/ThisAssembly.Project/ProjectPropertyGenerator.cs delete mode 100644 src/ThisAssembly.Project/Visual Basic.sbntxt diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index 9cd784ac..a8aa44ee 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -6,6 +6,8 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Text; using Scriban; +using static Devlooped.Sponsors.SponsorLink; +using Resources = Devlooped.Sponsors.Resources; namespace ThisAssembly; @@ -29,11 +31,26 @@ public void Initialize(IncrementalGeneratorInitializationContext context) if (value != null && value.StartsWith("|") && value.EndsWith("|")) value = value[1..^1].Replace('|', ';'); - return ( - name: Path.GetFileName(x.Left.Path), - value: value ?? "", - comment: string.IsNullOrWhiteSpace(comment) ? null : comment, - root: string.IsNullOrWhiteSpace(root) ? "Constants" : root!); + var name = Path.GetFileName(x.Left.Path); + if (string.IsNullOrEmpty(root)) + { + root = "Constants"; + } + else if (root == ".") + { + var parts = name.Split(['.'], 2); + if (parts.Length == 2) + { + // root should be the first part up to the first dot of name + // and name should be the rest + // note we only do this if there's an actual dot, otherwise, we + // just leave the root's default of Constants + root = parts[0]; + name = parts[1]; + } + } + + return (name, value: value ?? "", comment: string.IsNullOrWhiteSpace(comment) ? null : comment, root!); }); // Read the ThisAssemblyNamespace property or default to null @@ -62,7 +79,7 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value, return; } - var rootArea = Area.Load(new List { new Constant(name, value, comment), }, root); + var rootArea = Area.Load([new(name, value, comment),], root); // For now, we only support C# though var file = parse.Language.Replace("#", "Sharp") + ".sbntxt"; var template = Template.Parse(EmbeddedResource.GetContent(file), file); @@ -89,6 +106,6 @@ void GenerateConstant(SourceProductionContext spc, ((string name, string value, // .ToString(); //} - spc.AddSource($"{name}.g.cs", SourceText.From(output, Encoding.UTF8)); + spc.AddSource($"{root}.{name}.g.cs", SourceText.From(output, Encoding.UTF8)); } } diff --git a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets index 98cfd342..756d6b4f 100644 --- a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets +++ b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets @@ -10,7 +10,7 @@ BeforeTargets="PrepareConstants" DependsOnTargets="GetAssemblyAttributes"> - + diff --git a/src/ThisAssembly.Project/CSharp.sbntxt b/src/ThisAssembly.Project/CSharp.sbntxt deleted file mode 100644 index ed3ef12e..00000000 --- a/src/ThisAssembly.Project/CSharp.sbntxt +++ /dev/null @@ -1,40 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -using System.CodeDom.Compiler; -using System.Runtime.CompilerServices; -{{ if Namespace }} -namespace {{ Namespace }}; -{{~ end ~}} - -/// -/// Provides access to the current assembly information. -/// -partial class ThisAssembly -{ - /// - /// Gets the project properties. - /// - [GeneratedCode("ThisAssembly.Project", "{{ Version }}")] - [CompilerGenerated] - public static partial class Project - { - {{~ for prop in Properties ~}} - {{~ if RawStrings ~}} - public const string {{ prop.Key }} = -""" -{{ prop.Value }} -"""; - {{~ else ~}} - /// {{ prop.Key }} = {{ prop.Value }} - public const string {{ prop.Key }} = @"{{ prop.Value }}"; - {{~ end ~}} - {{~ end ~}} - } -} \ No newline at end of file diff --git a/src/ThisAssembly.Project/Model.cs b/src/ThisAssembly.Project/Model.cs deleted file mode 100644 index da0ab9c2..00000000 --- a/src/ThisAssembly.Project/Model.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection; - -namespace ThisAssembly; - -public class Model -{ - public Model(IEnumerable> properties, string? ns) - => (Properties, Namespace) - = (properties.ToList(), ns); - - public string? Namespace { get; } - public bool RawStrings { get; set; } = false; - public string Version => Assembly.GetExecutingAssembly().GetName().Version.ToString(3); - - public List> Properties { get; } -} diff --git a/src/ThisAssembly.Project/ProjectPropertyGenerator.cs b/src/ThisAssembly.Project/ProjectPropertyGenerator.cs deleted file mode 100644 index e7da9984..00000000 --- a/src/ThisAssembly.Project/ProjectPropertyGenerator.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using System.Text; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Text; -using Scriban; - -namespace ThisAssembly; - -[Generator] -public class ProjectPropertyGenerator : IIncrementalGenerator -{ - public void Initialize(IncrementalGeneratorInitializationContext context) - { - var properties = context.AnalyzerConfigOptionsProvider - .SelectMany((p, ct) => - { - var go = p.GlobalOptions; - if (!go.TryGetValue("build_property.ThisAssemblyProject", out var values)) - return Array.Empty>(); - - return values.Split('|') - .Select(prop => new KeyValuePair( - prop, - go.TryGetValue("build_property." + prop, out var value) ? value : null)) - .Where(pair => pair.Value != null); - }) - .Collect(); - - // Read the ThisAssemblyNamespace property or default to null - var right = context.AnalyzerConfigOptionsProvider - .Select((c, t) => c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var ns) && !string.IsNullOrEmpty(ns) ? ns : null) - .Combine(context.ParseOptionsProvider); - - context.RegisterSourceOutput( - properties.Combine(right), - GenerateSource); - } - - void GenerateSource(SourceProductionContext spc, (ImmutableArray> properties, (string? ns, ParseOptions parse)) arg) - { - var (properties, (ns, parse)) = arg; - - var model = new Model(properties, ns); - if (parse is CSharpParseOptions cs && (int)cs.LanguageVersion >= 1100) - model.RawStrings = true; - - var file = parse.Language.Replace("#", "Sharp") + ".sbntxt"; - var template = Template.Parse(EmbeddedResource.GetContent(file), file); - var output = template.Render(model, member => member.Name); - - spc.AddSource( - "ThisAssembly.Property.g.cs", - SourceText.From(output, Encoding.UTF8)); - } -} diff --git a/src/ThisAssembly.Project/ThisAssembly.Project.targets b/src/ThisAssembly.Project/ThisAssembly.Project.targets index bafda7ad..b8954be1 100644 --- a/src/ThisAssembly.Project/ThisAssembly.Project.targets +++ b/src/ThisAssembly.Project/ThisAssembly.Project.targets @@ -18,20 +18,14 @@ - - - + - - @(ProjectPropertyDistinct, '|') - - - - + + + - + diff --git a/src/ThisAssembly.Project/Visual Basic.sbntxt b/src/ThisAssembly.Project/Visual Basic.sbntxt deleted file mode 100644 index 6d3c4d52..00000000 --- a/src/ThisAssembly.Project/Visual Basic.sbntxt +++ /dev/null @@ -1,28 +0,0 @@ -'''------------------------------------------------------------------------------ -''' -''' This code was generated by a tool. -''' -''' Changes to this file may cause incorrect behavior And will be lost if -''' the code Is regenerated. -''' -'''------------------------------------------------------------------------------ - -{{ if Namespace }} -Namespace {{ Namespace }} -{{ else }} -Namespace Global -{{ end }} - ''' - ''' Provides access to the current assembly information. - ''' - Partial Class ThisAssembly - ''' - ''' Gets the project properties. - ''' - Partial Class Project - {{~ for prop in Properties ~}} - Public Const {{ prop.Key }} As String = @"{{ prop.Value }}" - - End Class - End Class -End Namespace \ No newline at end of file