Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiline values being truncated #392

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ThisAssembly.Constants/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{{- func summary -}}
/// <summary>
{{~ if $0.Comment ~}}
/// {{ $0.Comment }}
{{ $0.Comment }}
{{~ else ~}}
/// => @"{{ $0.Value }}"
{{~ end ~}}
Expand Down
19 changes: 8 additions & 11 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.Tracing;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -65,15 +65,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var options = context.GetStatusOptions();

context.RegisterSourceOutput(inputs.Combine(options), GenerateConstant);
//(spc, source) =>
//{
// var status = Diagnostics.GetOrSetStatus(source.Right);
// var warn = IsEditor &&
// (status == Devlooped.Sponsors.SponsorStatus.Unknown || status == Devlooped.Sponsors.SponsorStatus.Expired);

// GenerateConstant(spc, source.Left, warn ? string.Format(
// CultureInfo.CurrentCulture, Resources.Editor_Disabled, Funding.Product, Funding.HelpUrl) : null);
//});
}

void GenerateConstant(SourceProductionContext spc,
Expand All @@ -92,7 +83,13 @@ void GenerateConstant(SourceProductionContext spc,
return;
}

var rootArea = Area.Load([new(name, value, comment),], root);
if (comment != null)
comment = "/// " + string.Join(Environment.NewLine + "/// ", comment.Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
else
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);
// For now, we only support C# though
var file = parse.Language.Replace("#", "Sharp") + ".sbntxt";
var template = Template.Parse(EmbeddedResource.GetContent(file), file);
Expand Down
7 changes: 7 additions & 0 deletions src/ThisAssembly.Constants/ThisAssembly.Constants.targets
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
Value="%(FileConstant.Value)"
Comment="%(FileConstant.Comment)" />
</ItemGroup>
<ItemGroup>
<!-- Normalize newlines to avoid losing content -->
<Constant Update="@(Constant)">
<Value>$([MSBuild]::ValueOrDefault('%(Constant.Value)', '').Replace($([System.Environment]::NewLine), '\n'))</Value>
<Comment>$([MSBuild]::ValueOrDefault('%(Constant.Comment)', '').Replace($([System.Environment]::NewLine), '\n'))</Comment>
</Constant>
</ItemGroup>
</Target>

<Target Name="_InjectConstantAdditionalFiles"
Expand Down
10 changes: 10 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ with a newline and
// Some comments too.
""".ReplaceLineEndings(), ThisAssembly.Info.Description.ReplaceLineEndings());

[Fact]
public void CanUseMultilineProjectProperty()
=> Assert.Equal(
"""
A Description
with a newline and
* Some "things" with quotes
// Some comments too.
""".ReplaceLineEndings(), ThisAssembly.Project.Multiline.ReplaceLineEndings());

[Fact]
public void CanUseConstants()
=> Assert.Equal("Baz", ThisAssembly.Constants.Foo.Bar);
Expand Down
1 change: 0 additions & 1 deletion src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<ProjectProperty Include="Foo" />
<ProjectProperty Include="Foo" />
<ProjectProperty Include="Description" />
<!-- Multiline values that go through .editorconfig will get truncated, unfortunately -->
<ProjectProperty Include="Multiline" />
<Constant Include="Foo.Raw" Value="$(Multiline)" Comment="$(Multiline)" />
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
Expand Down