-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use source generator instead of T4 template
Tweak source generator output Merge fixes
- Loading branch information
Showing
14 changed files
with
217 additions
and
1,362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Moniker.Generator; | ||
|
||
/// <inheritdoc /> | ||
[Generator] | ||
public class Utf8StringsGenerator : ISourceGenerator | ||
{ | ||
/// <inheritdoc/> | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
// No initialization required | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var additionalFiles = context.AdditionalFiles | ||
.Where(f => Path.GetExtension(f.Path).Equals(".txt", StringComparison.OrdinalIgnoreCase)); | ||
|
||
foreach (var file in additionalFiles) | ||
{ | ||
var className = Path.GetFileNameWithoutExtension(file.Path); | ||
var sourceText = GenerateSourceForFile(className, file); | ||
|
||
context.AddSource($"{className}.g.cs", SourceText.From(sourceText, Encoding.UTF8)); | ||
} | ||
} | ||
|
||
private static string GenerateSourceForFile(string className, AdditionalText file) | ||
{ | ||
var offsets = new List<int>(); | ||
var charCounts = new List<int>(); | ||
var offset = 0; | ||
var stringBuilder = new StringBuilder(); | ||
|
||
stringBuilder.AppendLine("//------------------------------------------------------------------------------"); | ||
stringBuilder.AppendLine("// <auto-generated>"); | ||
stringBuilder.AppendLine("// This code was generated by a tool."); | ||
stringBuilder.AppendLine("// Changes to this file may cause incorrect behavior and will be lost if"); | ||
stringBuilder.AppendLine("// the code is re-generated."); | ||
stringBuilder.AppendLine("// </auto-generated>"); | ||
stringBuilder.AppendLine("//------------------------------------------------------------------------------"); | ||
stringBuilder.AppendLine("#nullable enable"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine("using System;"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine("namespace Moniker;"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine($"internal static class {className}"); | ||
stringBuilder.AppendLine("{"); | ||
stringBuilder.AppendLine(" public static Utf8Strings Strings => new(Count, Data, Offsets, CharCounts);"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine(" private static ReadOnlySpan<byte> Data =>"); | ||
|
||
foreach (var textLine in file.GetText()?.Lines.ToArray() ?? []) | ||
{ | ||
var line = textLine.ToString().Trim(); | ||
if (line.Length == 0 || line[0] == '#') | ||
continue; | ||
|
||
var byteLength = Encoding.UTF8.GetByteCount(line); | ||
|
||
if (line.Length > 0) | ||
{ | ||
stringBuilder.AppendLine($" \"{line}\"u8 + // [{offset}..{offset + byteLength}]"); | ||
} | ||
|
||
offsets.Add(offset); | ||
charCounts.Add(line.Length); | ||
offset += byteLength; | ||
} | ||
|
||
offsets.Add(offset); | ||
stringBuilder.AppendLine(" \"\"u8;"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine($" private const int Size = {offsets.Last()};"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine($" public const int Count = {offsets.Count - 1};"); | ||
stringBuilder.AppendLine(); | ||
|
||
AppendReadOnlySpan(stringBuilder, "Offsets", "int", offsets); | ||
stringBuilder.AppendLine(); | ||
AppendReadOnlySpan(stringBuilder, "CharCounts", "byte", charCounts); | ||
|
||
stringBuilder.AppendLine("}"); | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
private static void AppendReadOnlySpan(StringBuilder stringBuilder, string name, string type, List<int> values) | ||
{ | ||
stringBuilder.AppendLine($" private static ReadOnlySpan<{type}> {name} =>"); | ||
stringBuilder.AppendLine(" ["); | ||
|
||
var groups = from e in values.Select((v, i) => (Index: i, Value: v)) | ||
group e.Value.ToString() by e.Index / 15; | ||
|
||
foreach (var group in groups) | ||
{ | ||
stringBuilder.AppendLine($" {string.Join(", ", group)},"); | ||
} | ||
|
||
stringBuilder.AppendLine(" ];"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.