Skip to content

Commit

Permalink
Remove unused source generator post initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Jan 30, 2023
1 parent b9bcd4c commit 1428fff
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions src/DocoptNet/CodeGeneration/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@ public sealed class SourceGenerator : ISourceGenerator
DiagnosticSeverity.Error,
isEnabledByDefault: true);

public void Initialize(GeneratorInitializationContext context)
{
public void Initialize(GeneratorInitializationContext context) =>
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
context.RegisterForPostInitialization(context =>
{
foreach (var (fn, source) in GetEmbeddedCSharpSources(fn => DoesFileNameEndIn(fn, "Attribute")))
context.AddSource(fn, source);
});
}

sealed class SyntaxReceiver : ISyntaxContextReceiver
{
Expand Down Expand Up @@ -200,24 +193,6 @@ public void Execute(GeneratorExecutionContext context)
static readonly SourceText EmptySourceText = SourceText.From(string.Empty);
static readonly Encoding Utf8BomlessEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);

IEnumerable<(string, SourceText)> GetEmbeddedCSharpSources(Func<string, bool> predicate)
{
const string resourceNamespace = nameof(DocoptNet) + "." + nameof(CodeGeneration);
var assembly = GetType().Assembly;
foreach (var (rn, fn) in from rn in assembly.GetManifestResourceNames()
where rn.StartsWith(resourceNamespace) && rn.EndsWith(".cs")
select (ResourceName: rn, FileName: rn.Substring(resourceNamespace.Length)) into e
where predicate(e.FileName)
select e)
{
using var stream = assembly.GetManifestResourceStream(rn)!;
yield return (fn, SourceText.From(stream, canBeEmbedded: true));
}
}

static bool DoesFileNameEndIn(string fileName, string ending) =>
Path.GetFileNameWithoutExtension(fileName).EndsWith(ending, StringComparison.OrdinalIgnoreCase);

static readonly string[] Vars = "abcdefghijklmnopqrstuvwxyz".Select(ch => ch.ToString()).ToArray();

[Flags]
Expand Down

0 comments on commit 1428fff

Please sign in to comment.