Skip to content

Commit

Permalink
Automatically escape and unescape constants with semicolon
Browse files Browse the repository at this point in the history
This is a workaround (fix?) for dotnet/roslyn#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.
  • Loading branch information
kzu committed Jun 19, 2024
1 parent b31849f commit 04431a4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
7 changes: 6 additions & 1 deletion src/ThisAssembly.Constants/ThisAssembly.Constants.targets
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
DependsOnTargets="PrepareResourceNames;PrepareConstants">
<ItemGroup>
<AdditionalFiles Include="@(Constant)" SourceItemType="Constant" />
<!-- Automatically handle escaping of ; due to https://github.com/dotnet/roslyn/issues/51692 -->
<Constant Update="@(Constant)" Condition="$([MSBuild]::ValueOrdefault('%(Constant.Value)', '').Contains(';'))">
<Value>|$([MSBuild]::ValueOrdefault('%(Constant.Value)', '').Replace(';', '|'))|</Value>
</Constant>

<AdditionalFiles Include="@(Constant)" SourceItemType="Constant" />
</ItemGroup>
</Target>

Expand Down
4 changes: 4 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Constant Include="Foo.Raw" Value="$(Multiline)" Comment="$(Multiline)" />
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
<Constant Include="Foo.Hello" Value="World" Comment="Comments make everything better 😍" />
<Constant Include="WithSemiColon" Value="A;B;C" />
<FileConstant Include="@(None)" />
<FileConstant Update="@(FileConstant -&gt; WithMetadataValue('Filename', 'Readme'))">
<Link>Included/%(Filename)%(Extension)</Link>
Expand Down

0 comments on commit 04431a4

Please sign in to comment.