Skip to content

Commit

Permalink
Experimental: Change source code generation method to file writing
Browse files Browse the repository at this point in the history
Removed the source code generation from context and instead write the generated code directly to a file. This change allows us to save the refit generated code as standalone files. This approach could be beneficial for future debugging and preview purposes.
  • Loading branch information
christianhelle committed Aug 9, 2023
1 parent 8fa2774 commit dc2a61c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Refitter.SourceGenerator/RefitterSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Where(text => text.Path.EndsWith(".refitter", StringComparison.InvariantCultureIgnoreCase))
.Select(GenerateCode);

context.RegisterSourceOutput(
sourceFiles,
(c, file) => c.AddSource(
file.Filename,
source: file.Source.ToString()));
// context.RegisterSourceOutput(
// sourceFiles,
// (c, file) => c.AddSource(
// file.Filename,
// source: file.Source.ToString()));
}

public void Initialize(GeneratorInitializationContext context)
Expand Down Expand Up @@ -59,7 +59,7 @@ private static void TryGenerateCode(GeneratorExecutionContext context, Additiona
{
var (sourceText, filename) = GenerateCode(file, context.CancellationToken);

context.AddSource(filename, sourceText);
// context.AddSource(filename, sourceText);
context.ReportDiagnostic(
Diagnostic.Create(
new DiagnosticDescriptor(
Expand Down Expand Up @@ -129,6 +129,11 @@ private static (SourceText Source, string Filename) GenerateCode(

var generator = RefitGenerator.CreateAsync(settings).GetAwaiter().GetResult();
var refit = generator.Generate();

using var stream = File.CreateText(Path.Combine(Path.GetFullPath(file.Path), filename));
stream.Write(refit);
stream.Flush();

return (SourceText.From(refit, Encoding.UTF8), filename);
}
}

0 comments on commit dc2a61c

Please sign in to comment.