Skip to content

Commit

Permalink
Merge pull request #75 from amdavie/master
Browse files Browse the repository at this point in the history
Add rendered C# templates to compilation source when using NTypewriter.SourceGenerator
  • Loading branch information
NeVeSpl authored Apr 26, 2023
2 parents 8e3127f + c325b90 commit b2f43fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
36 changes: 32 additions & 4 deletions NTypewriter.SourceGenerator/Adapters/GeneratedFileReaderWriter.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
using System.IO;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using NTypewriter.Runtime;

namespace NTypewriter.SourceGenerator.Adapters
{
internal class GeneratedFileReaderWriter : IGeneratedFileReaderWriter
{
private readonly GeneratorExecutionContext generatorExecutionContext;
private readonly string projectDir;

public GeneratedFileReaderWriter(GeneratorExecutionContext generatorExecutionContext, string projectDir)
{
this.generatorExecutionContext = generatorExecutionContext;
this.projectDir = projectDir;
}

public bool Exists(string path)
{
return File.Exists(path);
return !ShouldAddToCompilation(path) && File.Exists(path);
}

public Task<string> Read(string path)
{
return Task.FromResult(File.ReadAllText(path));
return Task.FromResult(ShouldAddToCompilation(path) ? string.Empty : File.ReadAllText(path));
}

public Task Write(string path, string text)
{
if (ShouldAddToCompilation(path))
WriteCompilationSource(path, text);
else
WriteFile(path, text);

return Task.CompletedTask;
}

private static bool IsCSharpFile(string path) => Path.GetExtension(path).Equals(".cs", StringComparison.OrdinalIgnoreCase);
private bool IsSubPathOfProject(string path) => path.StartsWith(projectDir, StringComparison.OrdinalIgnoreCase);
private bool ShouldAddToCompilation(string path) => IsCSharpFile(path) && IsSubPathOfProject(path);

private void WriteCompilationSource(string path, string source) => generatorExecutionContext.AddSource(GenerateHintNameFromPath(path), source);

private void WriteFile(string path, string text)
{
var dir = Path.GetDirectoryName(path);
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(path, text);
return Task.CompletedTask;
}

private static string GenerateHintNameFromPath(string path) => Path.GetFileName(path);
}
}
3 changes: 2 additions & 1 deletion NTypewriter.SourceGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void Execute(GeneratorExecutionContext context)
var userCodeProvider = new UserCodeProvider(userCodePaths);
var userInterfaceOutputWriter = new UserInterfaceOutputWriter();

var cmd = new RenderTemplatesCommand(null, userCodeProvider, new GeneratedFileReaderWriter(), userInterfaceOutputWriter, null, null, null, null);
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ProjectDir", out var projectDir);
var cmd = new RenderTemplatesCommand(null, userCodeProvider, new GeneratedFileReaderWriter(context, projectDir), userInterfaceOutputWriter, null, null, null, null);
cmd.Execute(context.Compilation, templates).GetAwaiter().GetResult();

var log = userInterfaceOutputWriter.GetOutput();
Expand Down

0 comments on commit b2f43fc

Please sign in to comment.