|
| 1 | +// --------------------------------------------------------------------------------------------------------------------- |
| 2 | +// Imports |
| 3 | +// --------------------------------------------------------------------------------------------------------------------- |
| 4 | +using CodeOfChaos.Ansi.Generators.Xml; |
| 5 | +using CodeOfChaos.GeneratorTools; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Collections.Immutable; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | +using System.Xml.Serialization; |
| 14 | + |
| 15 | +namespace CodeOfChaos.Ansi.Generators; |
| 16 | +// --------------------------------------------------------------------------------------------------------------------- |
| 17 | +// Code |
| 18 | +// --------------------------------------------------------------------------------------------------------------------- |
| 19 | +[Generator] |
| 20 | +public class AnsiStringBuilderGenerator : IIncrementalGenerator { |
| 21 | + private static Regex FindFirstColorName { get; } = new(@"ASB\.[^\\\/]*\.xml"); |
| 22 | + |
| 23 | + // ----------------------------------------------------------------------------------------------------------------- |
| 24 | + // Methods |
| 25 | + // ----------------------------------------------------------------------------------------------------------------- |
| 26 | + public void Initialize(IncrementalGeneratorInitializationContext context) { |
| 27 | + IncrementalValueProvider<ImmutableArray<AdditionalText>> provider = context.AdditionalTextsProvider |
| 28 | + .Where(IsColorFile) |
| 29 | + .Collect(); |
| 30 | + |
| 31 | + context.RegisterSourceOutput(provider, GenerateCode); |
| 32 | + } |
| 33 | + |
| 34 | + private static bool IsColorFile(AdditionalText f) => FindFirstColorName.IsMatch(f.Path); |
| 35 | + |
| 36 | + private static void GenerateCode(SourceProductionContext context, ImmutableArray<AdditionalText> files) { |
| 37 | + IEnumerable<ColorEntry> colors = files.SelectMany(file => ParseColorFile(context, file)).ToArray(); |
| 38 | + var builder = new GeneratorStringBuilder(); |
| 39 | + |
| 40 | + #region Fore & Background |
| 41 | + foreach (string section in new[] {"Foreground", "Background"}) { |
| 42 | + context.AddSource($"Ansi{section}Builder.g.cs", builder |
| 43 | + .AppendUsings("System") |
| 44 | + .AppendAutoGenerated() |
| 45 | + .AppendNamespace("CodeOfChaos.Ansi") |
| 46 | + .AppendLine($"public partial class Ansi{section}Builder {{") |
| 47 | + .ForEach(colors, (stringBuilder, entry) => stringBuilder |
| 48 | + .AppendBodyIndented($$""" |
| 49 | + #region {{entry.Name}} |
| 50 | + private static CodeOfChaos.Ansi.ByteVector3 _{{entry.Name}} = new({{entry.Colors}}); |
| 51 | + |
| 52 | + public string {{entry.Name}}(string text) => $"{CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})}{text}{CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes}"; |
| 53 | + |
| 54 | + public Ansi{{section}}Builder Append{{entry.Name}}(string text) => BuilderAction(() => { |
| 55 | + Builder |
| 56 | + .Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})) |
| 57 | + .Append(text) |
| 58 | + .Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 59 | + }); |
| 60 | +
|
| 61 | + public Ansi{{section}}Builder Append{{entry.Name}}(Func<string> action) => BuilderAction(() => { |
| 62 | + Builder |
| 63 | + .Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})) |
| 64 | + .Append(action()) |
| 65 | + .Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 66 | + }); |
| 67 | +
|
| 68 | + public Ansi{{section}}Builder Append{{entry.Name}}(Action<Ansi{{section}}Builder> action) => BuilderAction(() => { |
| 69 | + Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})); |
| 70 | + action(this); |
| 71 | + Builder.Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 72 | + }); |
| 73 | + |
| 74 | + public Ansi{{section}}Builder Append{{entry.Name}}Line(string text) => BuilderAction(() => { |
| 75 | + Builder |
| 76 | + .Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})) |
| 77 | + .Append(text) |
| 78 | + .AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 79 | + }); |
| 80 | + |
| 81 | + public Ansi{{section}}Builder Append{{entry.Name}}Line(Func<string> action) => BuilderAction(() => { |
| 82 | + Builder |
| 83 | + .Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})) |
| 84 | + .Append(action()) |
| 85 | + .AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 86 | + }); |
| 87 | + |
| 88 | + public Ansi{{section}}Builder Append{{entry.Name}}Line(Action<Ansi{{section}}Builder> action) => BuilderAction(() => { |
| 89 | + Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})); |
| 90 | + action(this); |
| 91 | + Builder.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes); |
| 92 | + }); |
| 93 | + #endregion |
| 94 | +
|
| 95 | + """) |
| 96 | + .AppendLine() |
| 97 | + ) |
| 98 | + .AppendLine("}") |
| 99 | + .ToStringAndClear() |
| 100 | + ); |
| 101 | + } |
| 102 | + #endregion |
| 103 | + } |
| 104 | + |
| 105 | + private static ColorEntry[] ParseColorFile(SourceProductionContext context, AdditionalText file) { |
| 106 | + try { |
| 107 | + string? fileContent = file.GetText(context.CancellationToken)?.ToString(); |
| 108 | + if (fileContent is null || string.IsNullOrWhiteSpace(fileContent)) return []; |
| 109 | + |
| 110 | + // Use XmlSerializer to deserialize XML |
| 111 | + var serializer = new XmlSerializer(typeof(ColorEntryContainer)); |
| 112 | + using var reader = new StringReader(fileContent); |
| 113 | + // Deserialize the XML into the container object |
| 114 | + var container = (ColorEntryContainer?)serializer.Deserialize(reader); |
| 115 | + |
| 116 | + // Map the Color attribute (comma-separated values) to the ColorCode int array |
| 117 | + return container?.Entries |
| 118 | + .Select(entry => entry.ToColorEntry()) |
| 119 | + .ToArray() ?? []; |
| 120 | + } |
| 121 | + catch (InvalidOperationException ex) { |
| 122 | + // Report a diagnostic if XML parsing fails |
| 123 | + context.ReportDiagnostic(Diagnostic.Create( |
| 124 | + new DiagnosticDescriptor( |
| 125 | + "ASB01", |
| 126 | + "Invalid XML Format", |
| 127 | + $"Failed to parse the XML file: {file.Path}. Error: {ex.Message}", |
| 128 | + "CodeGeneration", |
| 129 | + DiagnosticSeverity.Warning, |
| 130 | + true), |
| 131 | + Location.None)); |
| 132 | + return []; |
| 133 | + } |
| 134 | + catch (FormatException ex) { |
| 135 | + // Handle errors when parsing color values |
| 136 | + context.ReportDiagnostic(Diagnostic.Create( |
| 137 | + new DiagnosticDescriptor( |
| 138 | + "ASB02", |
| 139 | + "Invalid Color Formatting", |
| 140 | + $"Error while parsing RGB values in file: {file.Path}. Error: {ex.Message}", |
| 141 | + "CodeGeneration", |
| 142 | + DiagnosticSeverity.Warning, |
| 143 | + true), |
| 144 | + Location.None)); |
| 145 | + return []; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments