|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 3 | +using Microsoft.CodeAnalysis.Text; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Gherkin.SourceGenerator; |
| 7 | + |
| 8 | +[Generator] |
| 9 | +public class LanguageDialectGenerator : IIncrementalGenerator |
| 10 | +{ |
| 11 | + const string GeneratorVersion = "1.0.0"; |
| 12 | + record ClassToAddLanguageDialects(string? Namespace, string ClassName); |
| 13 | + |
| 14 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 15 | + { |
| 16 | + //System.Diagnostics.Debugger.Launch(); |
| 17 | + |
| 18 | + context.RegisterPostInitializationOutput(context => context.AddSource( |
| 19 | + "LanguageDialectGeneratedAttribute.g.cs", |
| 20 | + SourceText.From(""" |
| 21 | + [System.AttributeUsage(System.AttributeTargets.Class)] |
| 22 | + internal sealed class LanguageDialectGeneratedAttribute : Attribute { } |
| 23 | + """, Encoding.UTF8))); |
| 24 | + |
| 25 | + var pipeline = context.SyntaxProvider.ForAttributeWithMetadataName( |
| 26 | + fullyQualifiedMetadataName: "LanguageDialectGeneratedAttribute", |
| 27 | + predicate: static (syntaxNode, cancelToken) => syntaxNode is ClassDeclarationSyntax, |
| 28 | + transform: static (context, cancelToken) => |
| 29 | + { |
| 30 | + var targetSymbol = (INamedTypeSymbol)context.TargetSymbol; |
| 31 | + var ns = targetSymbol.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)); |
| 32 | + var className = targetSymbol.Name; |
| 33 | + return new ClassToAddLanguageDialects(ns, className); |
| 34 | + }); |
| 35 | + |
| 36 | + context.RegisterSourceOutput(pipeline, static (context, classToAdd) => |
| 37 | + { |
| 38 | + var allLanguageSettings = LoadLanguageSettings(); |
| 39 | + |
| 40 | + var sb = new StringBuilder(); |
| 41 | + if (classToAdd.Namespace is not null) |
| 42 | + sb.AppendLine($"namespace {classToAdd.Namespace};"); |
| 43 | + sb.AppendLine($$""" |
| 44 | +public partial class {{classToAdd.ClassName}} |
| 45 | +{ |
| 46 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Gherkin.SourceGenerator", "{{GeneratorVersion}}")] |
| 47 | + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] |
| 48 | + private static GherkinDialect TryCreateGherkinDialect(string language) |
| 49 | + { |
| 50 | + switch (language) |
| 51 | + { |
| 52 | +"""); |
| 53 | + foreach (var (language, methodSuffix, _) in allLanguageSettings) |
| 54 | + { |
| 55 | + sb.AppendLine($$""" |
| 56 | + case {{FormatLiteral(language)}}: |
| 57 | + return CreateGherkinDialectFor_{{methodSuffix}}(); |
| 58 | +"""); |
| 59 | + } |
| 60 | + |
| 61 | + sb.AppendLine($$""" |
| 62 | + default: |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | +"""); |
| 68 | + foreach (var (language, methodSuffix, languageSettings) in allLanguageSettings) |
| 69 | + { |
| 70 | + sb.AppendLine($$""" |
| 71 | +
|
| 72 | + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Gherkin.SourceGenerator", "{{GeneratorVersion}}")] |
| 73 | + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] |
| 74 | + private static GherkinDialect CreateGherkinDialectFor_{{methodSuffix}}() |
| 75 | + { |
| 76 | + return new GherkinDialect( |
| 77 | + {{FormatLiteral(language)}}, |
| 78 | + {{FormatListLiteral(languageSettings.Feature)}}, |
| 79 | + {{FormatListLiteral(languageSettings.Rule)}}, |
| 80 | + {{FormatListLiteral(languageSettings.Background)}}, |
| 81 | + {{FormatListLiteral(languageSettings.Scenario)}}, |
| 82 | + {{FormatListLiteral(languageSettings.ScenarioOutline)}}, |
| 83 | + {{FormatListLiteral(languageSettings.Examples)}}, |
| 84 | + {{FormatListLiteral(languageSettings.Given)}}, |
| 85 | + {{FormatListLiteral(languageSettings.When)}}, |
| 86 | + {{FormatListLiteral(languageSettings.Then)}}, |
| 87 | + {{FormatListLiteral(languageSettings.And)}}, |
| 88 | + {{FormatListLiteral(languageSettings.But)}}); |
| 89 | + } |
| 90 | +"""); |
| 91 | + } |
| 92 | + |
| 93 | + sb.AppendLine(@" |
| 94 | +}" |
| 95 | + ); |
| 96 | + |
| 97 | + context.AddSource($"GherkinDialectProvider.LanguageDialect.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8)); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + static string FormatListLiteral(IEnumerable<string?>? items) |
| 102 | + { |
| 103 | + if (items is null) |
| 104 | + return "null"; |
| 105 | + bool first = true; |
| 106 | + var sb = new StringBuilder(); |
| 107 | + sb.Append("["); |
| 108 | + foreach (var item in items) |
| 109 | + { |
| 110 | + if (first) |
| 111 | + first = false; |
| 112 | + else |
| 113 | + sb.Append(", "); |
| 114 | + if (item is null) |
| 115 | + sb.Append("null"); |
| 116 | + else |
| 117 | + sb.Append(FormatLiteral(item)); |
| 118 | + } |
| 119 | + sb.Append("]"); |
| 120 | + return sb.ToString(); |
| 121 | + } |
| 122 | + |
| 123 | + static string FormatLiteral(string value) => Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(value, true); |
| 124 | + |
| 125 | + static List<(string Language, string MethodSuffix, GherkinLanguageSetting Settings)> LoadLanguageSettings() |
| 126 | + { |
| 127 | + const string languageFileName = "gherkin-languages.json"; |
| 128 | + |
| 129 | + var assembly = typeof(LanguageDialectGenerator).Assembly; |
| 130 | + var resourceStream = assembly.GetManifestResourceStream("Gherkin.SourceGenerator." + languageFileName); |
| 131 | + |
| 132 | + if (resourceStream == null) |
| 133 | + throw new InvalidOperationException("Gherkin language resource not found: " + languageFileName); |
| 134 | + var languagesFileContent = new StreamReader(resourceStream).ReadToEnd(); |
| 135 | + |
| 136 | + var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, GherkinLanguageSetting>>(languagesFileContent); |
| 137 | + if (result is null) |
| 138 | + throw new InvalidOperationException("Gherkin language resource is empty: " + languageFileName); |
| 139 | + return result.OrderBy(x => x.Key).Select(x => (x.Key, x.Key.Replace("-", "_"), x.Value)).ToList(); |
| 140 | + } |
| 141 | +} |
0 commit comments