From 6d69b1ae7bb0391f13022e553441cbb2d58cfcbe Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 14:02:44 +0100 Subject: [PATCH 1/8] TemplateRazorProjectItem: add path The bogus path `/Template` did not help me to find the template causing compilation error. --- .../VS.Web.CG.Templating/TemplateRazorProjectItem.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Scaffolding/VS.Web.CG.Templating/TemplateRazorProjectItem.cs b/src/Scaffolding/VS.Web.CG.Templating/TemplateRazorProjectItem.cs index 3a5abd64e..0ade21f0c 100644 --- a/src/Scaffolding/VS.Web.CG.Templating/TemplateRazorProjectItem.cs +++ b/src/Scaffolding/VS.Web.CG.Templating/TemplateRazorProjectItem.cs @@ -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; From eba4300b774da914932c12ad46a27e7daca6b995 Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 14:05:50 +0100 Subject: [PATCH 2/8] RunTemplateAsync: add template path parameter --- src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs b/src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs index 8574e76f1..69d3c559f 100644 --- a/src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs +++ b/src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs @@ -29,7 +29,7 @@ public RazorTemplating(ICompilationService compilationService) _compilationService = compilationService; } - public async Task RunTemplateAsync(string content, + public async Task RunTemplateAsync(string path, string content, dynamic templateModel) { // Don't care about the RazorProject as we already have the content of the .cshtml file @@ -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(); From 90e9086ff1622a838150feb046fb709372888fe9 Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 14:08:24 +0100 Subject: [PATCH 3/8] AddNewContextItemsInternal: pass templatePath Pass `templatePath` to `RunTemplateAsync` in `AddNewContextItemsInternal` --- src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs b/src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs index 87e0c5912..9fc8e6466 100644 --- a/src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs +++ b/src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs @@ -80,7 +80,7 @@ private async Task 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) { From efa8cf33ed6ad3f76074deaeca6957a8746ab909 Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 14:23:47 +0100 Subject: [PATCH 4/8] ITemplating: add path parameter --- src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs b/src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs index fb3029cf5..99fcf5295 100644 --- a/src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs +++ b/src/Scaffolding/VS.Web.CG.Templating/ITemplating.cs @@ -7,6 +7,6 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating { public interface ITemplating { - Task RunTemplateAsync(string content, dynamic templateModel); + Task RunTemplateAsync(string path, string content, dynamic templateModel); } -} \ No newline at end of file +} From 384ec01ecf93a2e6d5079297174f0d5ebc2cba51 Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 17:43:41 +0100 Subject: [PATCH 5/8] RazorTemplatingTests: add mockTemplatePath --- .../VS.Web.CG.Templating.Test/RazorTemplatingTests.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs b/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs index 8433bbb98..ea898b747 100644 --- a/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs +++ b/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs @@ -9,6 +9,8 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test //This is more of an integration test. public class RazorTemplatingTests { + const mockTemplatePath = "/Template"; + [Fact (Skip = "Disabling test on CI")] public async void RunTemplateAsync_Generates_Text_For_Template_With_A_Model() { @@ -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); @@ -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); @@ -54,4 +56,4 @@ public class SimpleModel { public string Name { get; set; } } -} \ No newline at end of file +} From 07b64dbe32ac7cc99a9fd97218b0e1dab3da8ee0 Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 17:48:26 +0100 Subject: [PATCH 6/8] CodeGeneratorActionsServiceTests: add templatePath --- .../VS.Web.CG.Core.Test/CodeGeneratorActionsServiceTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Scaffolding/VS.Web.CG.Core.Test/CodeGeneratorActionsServiceTests.cs b/test/Scaffolding/VS.Web.CG.Core.Test/CodeGeneratorActionsServiceTests.cs index 49e5dae0c..5f9cdadf0 100644 --- a/test/Scaffolding/VS.Web.CG.Core.Test/CodeGeneratorActionsServiceTests.cs +++ b/test/Scaffolding/VS.Web.CG.Core.Test/CodeGeneratorActionsServiceTests.cs @@ -46,7 +46,7 @@ public async Task AddFileFromTemplateAsync_Throws_If_Template_Processing_Has_Exc mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny>())) .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 @@ -79,7 +79,7 @@ public async Task AddFileFromTemplateAsync_Writes_If_Template_Processing_Is_Succ mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny>())) .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, @@ -98,4 +98,4 @@ await codeGeneratorActionService.AddFileFromTemplateAsync(outputPath, Assert.Equal(generatedText, mockFileSystem.ReadAllText(outputPath)); } } -} \ No newline at end of file +} From 8411e893d5ec261eefd37800eb5b26984919896e Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 17:50:55 +0100 Subject: [PATCH 7/8] typo fix --- .../VS.Web.CG.Templating.Test/RazorTemplatingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs b/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs index ea898b747..53bbbedbd 100644 --- a/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs +++ b/test/Scaffolding/VS.Web.CG.Templating.Test/RazorTemplatingTests.cs @@ -9,7 +9,7 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Templating.Test //This is more of an integration test. public class RazorTemplatingTests { - const mockTemplatePath = "/Template"; + const string mockTemplatePath = "/Template"; [Fact (Skip = "Disabling test on CI")] public async void RunTemplateAsync_Generates_Text_For_Template_With_A_Model() From b8803fecef76e069b33181ea9d23f88b36fbaf2b Mon Sep 17 00:00:00 2001 From: Christopher Yeleighton Date: Mon, 10 Jan 2022 18:26:53 +0100 Subject: [PATCH 8/8] AddFileFromTemplateAsync: add templatePath --- src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs b/src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs index e1603ea55..d729f6c7b 100644 --- a/src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs +++ b/src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs @@ -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) {