From 61b31ae1b7e28f9089af5b4dd459991aff5a6226 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Mon, 22 Jan 2024 18:24:15 +0000 Subject: [PATCH] Update Mono.TextTemplating to new preview package Part of #32385 --- src/EFCore.Design/EFCore.Design.csproj | 2 +- .../Internal/TextTemplatingModelGenerator.cs | 8 +++-- .../Internal/TextTemplatingEngineHostTest.cs | 36 +++++++++---------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/EFCore.Design/EFCore.Design.csproj b/src/EFCore.Design/EFCore.Design.csproj index f1fd0f511d3..34064d4dad2 100644 --- a/src/EFCore.Design/EFCore.Design.csproj +++ b/src/EFCore.Design/EFCore.Design.csproj @@ -62,7 +62,7 @@ - + diff --git a/src/EFCore.Design/Scaffolding/Internal/TextTemplatingModelGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/TextTemplatingModelGenerator.cs index fba4c63fe0f..aa4f3a5828d 100644 --- a/src/EFCore.Design/Scaffolding/Internal/TextTemplatingModelGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/TextTemplatingModelGenerator.cs @@ -107,7 +107,7 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO { host.TemplateFile = contextTemplate; - generatedCode = Engine.ProcessTemplate(File.ReadAllText(contextTemplate), host); + generatedCode = Engine.ProcessTemplateAsync(File.ReadAllText(contextTemplate), host).GetAwaiter().GetResult();; CheckEncoding(host.OutputEncoding); HandleErrors(host); } @@ -165,7 +165,8 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO if (compiledEntityTypeTemplate is null) { - compiledEntityTypeTemplate = Engine.CompileTemplate(File.ReadAllText(entityTypeTemplate), host); + compiledEntityTypeTemplate = Engine.CompileTemplateAsync(File.ReadAllText(entityTypeTemplate), host, default) + .GetAwaiter().GetResult();; entityTypeExtension = host.Extension; CheckEncoding(host.OutputEncoding); } @@ -208,7 +209,8 @@ public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationO if (compiledConfigurationTemplate is null) { - compiledConfigurationTemplate = Engine.CompileTemplate(File.ReadAllText(configurationTemplate), host); + compiledConfigurationTemplate = Engine.CompileTemplateAsync(File.ReadAllText(configurationTemplate), host, default) + .GetAwaiter().GetResult();; configurationExtension = host.Extension; CheckEncoding(host.OutputEncoding); } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/TextTemplatingEngineHostTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/TextTemplatingEngineHostTest.cs index 1fd71e38ffc..87f1356af50 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/TextTemplatingEngineHostTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/TextTemplatingEngineHostTest.cs @@ -22,9 +22,9 @@ public void Service_works() .AddSingleton("Hello, Services!") .BuildServiceProvider()); - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#@ template hostSpecific=""true"" #><#= ((IServiceProvider)Host).GetService(typeof(string)) #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal("Hello, Services!", result); @@ -35,9 +35,9 @@ public void Session_works() { var host = new TextTemplatingEngineHost { Session = new TextTemplatingSession { ["Value"] = "Hello, Session!" } }; - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#= Session[""Value""] #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal("Hello, Session!", result); @@ -48,9 +48,9 @@ public void Session_works_with_parameter() { var host = new TextTemplatingEngineHost { Session = new TextTemplatingSession { ["Value"] = "Hello, Session!" } }; - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#@ parameter name=""Value"" type=""System.String"" #><#= Value #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal("Hello, Session!", result); @@ -66,9 +66,9 @@ public void Include_works() var host = new TextTemplatingEngineHost { TemplateFile = Path.Combine(dir, "test.tt") }; - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#@ include file=""test.ttinclude"" #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal("Hello, Include!", result); @@ -79,9 +79,9 @@ public void Error_works() { var host = new TextTemplatingEngineHost(); - _engine.ProcessTemplate( + _engine.ProcessTemplateAsync( @"<# Error(""Hello, Error!""); #>", - host); + host).GetAwaiter().GetResult(); var error = Assert.Single(host.Errors.Cast()); Assert.Equal("Hello, Error!", error.ErrorText); @@ -93,9 +93,9 @@ public void Directive_throws_when_processor_unknown() var host = new TextTemplatingEngineHost(); var ex = Assert.Throws( - () => _engine.ProcessTemplate( + () => _engine.ProcessTemplateAsync( @"<#@ test processor=""TestDirectiveProcessor"" #>", - host)); + host).GetAwaiter().GetResult()); Assert.Equal(DesignStrings.UnknownDirectiveProcessor("TestDirectiveProcessor"), ex.Message); } @@ -107,9 +107,9 @@ public void ResolvePath_work() var host = new TextTemplatingEngineHost { TemplateFile = Path.Combine(dir, "test.tt") }; - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#@ template hostSpecific=""true"" #><#= Host.ResolvePath(""data.json"") #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal(Path.Combine(dir, "data.json"), result); @@ -120,9 +120,9 @@ public void Output_works() { var host = new TextTemplatingEngineHost(); - _engine.ProcessTemplate( + _engine.ProcessTemplateAsync( @"<#@ output extension="".txt"" encoding=""us-ascii"" #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal(".txt", host.Extension); @@ -134,9 +134,9 @@ public void Assembly_works() { var host = new TextTemplatingEngineHost(); - var result = _engine.ProcessTemplate( + var result = _engine.ProcessTemplateAsync( @"<#@ assembly name=""Microsoft.EntityFrameworkCore"" #><#= nameof(Microsoft.EntityFrameworkCore.DbContext) #>", - host); + host).GetAwaiter().GetResult(); Assert.Empty(host.Errors); Assert.Equal("DbContext", result);