Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplateRazorProjectItem: Add true template path #1790

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task AddFileFromTemplateAsync(string outputPath, string templateNam
Debug.Assert(_fileSystem.FileExists(templatePath));
var templateContent = _fileSystem.ReadAllText(templatePath);

var templateResult = await _templatingService.RunTemplateAsync(templateContent, templateModel);
var templateResult = await _templatingService.RunTemplateAsync(templatePath, templateContent, templateModel);

if (templateResult.ProcessingException != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private async Task<SyntaxTree> AddNewContextItemsInternal(string templateName, N
Contract.Assert(File.Exists(templatePath));

var templateContent = File.ReadAllText(templatePath);
var templateResult = await _templatingService.RunTemplateAsync(templateContent, dbContextTemplateModel);
var templateResult = await _templatingService.RunTemplateAsync(templatePath, templateContent, dbContextTemplateModel);

if (templateResult.ProcessingException != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating
{
public interface ITemplating
{
Task<TemplateResult> RunTemplateAsync(string content, dynamic templateModel);
Task<TemplateResult> RunTemplateAsync(string path, string content, dynamic templateModel);
}
}
}
4 changes: 2 additions & 2 deletions src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public RazorTemplating(ICompilationService compilationService)
_compilationService = compilationService;
}

public async Task<TemplateResult> RunTemplateAsync(string content,
public async Task<TemplateResult> RunTemplateAsync(string path, string content,
dynamic templateModel)
{
// Don't care about the RazorProject as we already have the content of the .cshtml file
Expand All @@ -52,7 +52,7 @@ @using System.Threading.Tasks
");
});

var templateItem = new TemplateRazorProjectItem(content);
var templateItem = new TemplateRazorProjectItem(path, content);
var codeDocument = projectEngine.Process(templateItem);
var generatorResults = codeDocument.GetCSharpDocument();

Expand Down
12 changes: 7 additions & 5 deletions src/Scaffolding/VS.Web.CG.Templating/TemplateRazorProjectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating
internal class TemplateRazorProjectItem : RazorProjectItem
{
private readonly byte[] _contentUTF8Bytes;
private readonly FileInfo _path;

public TemplateRazorProjectItem(string content)
public TemplateRazorProjectItem(string path, string content)
{
var preamble = Encoding.UTF8.GetPreamble();

var contentBytes = Encoding.UTF8.GetBytes(content);

_contentUTF8Bytes = new byte[preamble.Length + contentBytes.Length];
this._contentUTF8Bytes = new byte[preamble.Length + contentBytes.Length];
preamble.CopyTo(_contentUTF8Bytes, 0);
contentBytes.CopyTo(_contentUTF8Bytes, preamble.Length);
this._path = new FileInfo(path);
}

public override string BasePath => "/";
public override string BasePath => this._path.Directory.FullName;

public override string FilePath => "Template";
public override string FilePath => this._path.Name;

public override string PhysicalPath => "/Template";
public override string PhysicalPath => this._path.FullName;

public override bool Exists => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task AddFileFromTemplateAsync_Throws_If_Template_Processing_Has_Exc
mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny<IEnumerable<string>>()))
.Returns(templatePath);
mockFileSystem.WriteAllText(templatePath, templateContent);
mockTemplating.Setup(templating => templating.RunTemplateAsync(templateContent, null))
mockTemplating.Setup(templating => templating.RunTemplateAsync(templatePath, templateContent, null))
.Returns(Task.FromResult(new TemplateResult()
{
ProcessingException = processingException
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task AddFileFromTemplateAsync_Writes_If_Template_Processing_Is_Succ
mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny<IEnumerable<string>>()))
.Returns(templatePath);
mockFileSystem.WriteAllText(templatePath, templateContent);
mockTemplating.Setup(templating => templating.RunTemplateAsync(templateContent, null))
mockTemplating.Setup(templating => templating.RunTemplateAsync(templatePath, templateContent, null))
.Returns(Task.FromResult(new TemplateResult()
{
ProcessingException = null,
Expand All @@ -98,4 +98,4 @@ await codeGeneratorActionService.AddFileFromTemplateAsync(outputPath,
Assert.Equal(generatedText, mockFileSystem.ReadAllText(outputPath));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test
//This is more of an integration test.
public class RazorTemplatingTests
{
const string mockTemplatePath = "/Template";

[Fact (Skip = "Disabling test on CI")]
public async void RunTemplateAsync_Generates_Text_For_Template_With_A_Model()
{
Expand All @@ -19,7 +21,7 @@ public async void RunTemplateAsync_Generates_Text_For_Template_With_A_Model()
var templatingService = new RazorTemplating(compilationService);

//Act
var result = await templatingService.RunTemplateAsync(templateContent, model);
var result = await templatingService.RunTemplateAsync(mockTemplatePath, templateContent, model);

//Assert
Assert.Null(result.ProcessingException);
Expand All @@ -35,7 +37,7 @@ public async void RunTemplateAsync_Returns_Error_For_Invalid_Template()
var templatingService = new RazorTemplating(compilationService);

//Act
var result = await templatingService.RunTemplateAsync(templateContent, templateModel: "DoesNotMatter");
var result = await templatingService.RunTemplateAsync(mockTemplatePath, templateContent, templateModel: "DoesNotMatter");

//Assert
Assert.Equal("", result.GeneratedText);
Expand All @@ -54,4 +56,4 @@ public class SimpleModel
{
public string Name { get; set; }
}
}
}