Skip to content
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
1 change: 0 additions & 1 deletion src/ThisAssembly.Constants/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
{{ obsolete }}
{{~ if RawStrings && value.IsText ~}}
public {{ Modifier }} string {{ value.Name | string.replace "-" "_" | string.replace " " "_" }} ={{ Lambda }}

"""
{{ value.Value }}
""";
Expand Down
41 changes: 30 additions & 11 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
using Devlooped.Sponsors;
Expand Down Expand Up @@ -94,12 +95,12 @@ void GenerateConstant(SourceProductionContext spc,
}

if (comment != null)
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(comment).ToString().Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(comment).ToString().Trim().Replace("`n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
else
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(value).ToString().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(value).ToString().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, type ?? "string"),], root, rootComment);
var rootArea = Area.Load([new(name, value.Replace("`n", Environment.NewLine).Trim(['\r', '\n']), comment, type ?? "string"),], root, rootComment);
// For now, we only support C# though
var file = parse.Language.Replace("#", "Sharp") + ".sbntxt";
var template = Template.Parse(EmbeddedResource.GetContent(file), file);
Expand All @@ -123,17 +124,35 @@ void GenerateConstant(SourceProductionContext spc,

var output = template.Render(model, member => member.Name);

// Apply formatting since indenting isn't that nice in Scriban when rendering nested
// structures via functions.
if (parse.Language == LanguageNames.CSharp)
{
output = SyntaxFactory
.ParseCompilationUnit(output, options: cs)
.NormalizeWhitespace()
.GetText()
.ToString();
// Apply formatting since indenting isn't that nice in Scriban when rendering nested
// structures via functions.
// We alos rewrite to prepend a newline leading trivia before the raw string literals if any
var node = new RawStringLiteralRewriter().Visit(
SyntaxFactory.ParseCompilationUnit(output, options: cs).NormalizeWhitespace(eol: Environment.NewLine));

output = node.GetText().ToString();
}

spc.AddSource($"{root}.{name}.g.cs", SourceText.From(output, Encoding.UTF8));
}
}

class RawStringLiteralRewriter : CSharpSyntaxRewriter
{
public override SyntaxToken VisitToken(SyntaxToken token)
{
// See https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.csharp.syntaxkind?view=roslyn-dotnet-4.13.0
// MultiLineRawStringLiteralToken = 8519
// Utf8MultiLineRawStringLiteralToken = 8522
if (token.RawKind == 8519 || token.RawKind == 8522)
return token.WithLeadingTrivia(
token.LeadingTrivia.Add(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
SyntaxFactory.CarriageReturnLineFeed :
SyntaxFactory.LineFeed));

return base.VisitToken(token);
}
}
}
4 changes: 2 additions & 2 deletions src/ThisAssembly.Constants/ThisAssembly.Constants.targets
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<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>
<Value>$([MSBuild]::ValueOrDefault('%(Constant.Value)', '').Replace('&#xD;&#xA;', '`n').Replace('&#xA;', '`n'))</Value>
<Comment>$([MSBuild]::ValueOrDefault('%(Constant.Comment)', '').Replace('&#xD;&#xA;', '`n').Replace('&#xA;', '`n'))</Comment>
</Constant>
</ItemGroup>
</Target>
Expand Down
5 changes: 5 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.IdentityModel.Tokens;
using Xunit;
using Xunit.Abstractions;
//using ThisAssembly = ThisAssemblyTests
Expand Down Expand Up @@ -191,4 +192,8 @@ public void CanUseGitBranchConstants()
[Fact]
public void CanUseSemicolonsInConstant()
=> Assert.Equal("A;B;C", ThisAssembly.Constants.WithSemiColon);

/// <summary />
[Fact]
public void CanReadJsonConstant() => JsonWebKey.Create(ThisAssembly.Metadata.Funding.GitHub.devlooped);
}
14 changes: 14 additions & 0 deletions src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,18 @@
<CompilerVisibleProperty Include="ThisAssembly" />
</ItemGroup>

<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
<Exec Command="curl --silent --output $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk https://sponsorlink.devlooped.com/jwk" />
</Target>

<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
<PropertyGroup>
<!-- Read public key we validate manifests against -->
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk'))</DevloopedJwk>
</PropertyGroup>
<ItemGroup>
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk.Trim())" />
</ItemGroup>
</Target>

</Project>