-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from amdavie/master
Add rendered C# templates to compilation source when using NTypewriter.SourceGenerator
- Loading branch information
Showing
2 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
36 changes: 32 additions & 4 deletions
36
NTypewriter.SourceGenerator/Adapters/GeneratedFileReaderWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters