From b66a3f4cca4d43365aa8b0cb60b09c04d0e95596 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Wed, 19 Jun 2024 19:08:16 -0300 Subject: [PATCH] Automatically escape and unescape constants with semicolon This is a workaround (fix?) for https://github.com/dotnet/roslyn/issues/51692, basically. We lose all content after the first semicolon in constant and metadata values (presumably Git too). Project properties also suffer from this, but merits unifying it with ThisAssembly.Constants to continue to streamline the implementation. --- src/ThisAssembly.Constants/ConstantsGenerator.cs | 5 +++++ src/ThisAssembly.Constants/ThisAssembly.Constants.targets | 7 ++++++- src/ThisAssembly.Tests/Tests.cs | 4 ++++ src/ThisAssembly.Tests/ThisAssembly.Tests.csproj | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ThisAssembly.Constants/ConstantsGenerator.cs b/src/ThisAssembly.Constants/ConstantsGenerator.cs index c09da1c6..107f812b 100644 --- a/src/ThisAssembly.Constants/ConstantsGenerator.cs +++ b/src/ThisAssembly.Constants/ConstantsGenerator.cs @@ -25,6 +25,11 @@ 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); x.Right.GetOptions(x.Left).TryGetValue("build_metadata.Constant.Root", out var root); + + // Revert auto-escaping due to https://github.com/dotnet/roslyn/issues/51692 + if (value != null && value.StartsWith("|") && value.EndsWith("|")) + value = value[1..^1].Replace('|', ';'); + return ( name: Path.GetFileName(x.Left.Path), value: value!, diff --git a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets index 91958a41..329a601e 100644 --- a/src/ThisAssembly.Constants/ThisAssembly.Constants.targets +++ b/src/ThisAssembly.Constants/ThisAssembly.Constants.targets @@ -44,7 +44,12 @@ BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun" DependsOnTargets="PrepareResourceNames;PrepareConstants"> - + + + |$([MSBuild]::ValueOrdefault('%(Constant.Value)', '').Replace(';', '|'))| + + + diff --git a/src/ThisAssembly.Tests/Tests.cs b/src/ThisAssembly.Tests/Tests.cs index 91c31dc9..f2376293 100644 --- a/src/ThisAssembly.Tests/Tests.cs +++ b/src/ThisAssembly.Tests/Tests.cs @@ -102,4 +102,8 @@ public void CanUseGitBranchConstants() Assert.NotEmpty(ThisAssembly.Git.Branch); Output.WriteLine(ThisAssembly.Git.Branch); } + + [Fact] + public void CanUseSemicolonsInConstant() + => Assert.Equal("A;B;C", ThisAssembly.Constants.WithSemiColon); } diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index b51b9cab..ef198a21 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -66,6 +66,7 @@ + Included/%(Filename)%(Extension)