From 80709335e5ad54cbcb78ce1b287dc23e56785489 Mon Sep 17 00:00:00 2001 From: Alexander Selishchev Date: Mon, 1 Mar 2021 23:13:29 +0300 Subject: [PATCH 1/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e79ea99..2d611f1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RazorEngineCore .NET5 Razor Template Engine. No legacy code. -* .NET Standard 2.0 * .NET 5.0 +* .NET Standard 2.0 * .NET Framework 4.7.2 * Windows / Linux From 3a8ad7fd94c7cc603abcb823075726fe4b5c610e Mon Sep 17 00:00:00 2001 From: Alexander Selishchev Date: Mon, 29 Mar 2021 00:24:17 +0300 Subject: [PATCH 2/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2d611f1..df96282 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ * .NET Standard 2.0 * .NET Framework 4.7.2 * Windows / Linux +* Publish as single file supported [![NuGet](https://img.shields.io/nuget/dt/RazorEngineCore.svg?style=flat-square)](https://www.nuget.org/packages/RazorEngineCore) [![NuGet](https://img.shields.io/nuget/v/RazorEngineCore.svg?style=flat-square)](https://www.nuget.org/packages/RazorEngineCore) From fbcb7bdd38c1a959666ebe979982d4de44a7ef91 Mon Sep 17 00:00:00 2001 From: William Cossey Date: Mon, 19 Apr 2021 11:24:19 +0100 Subject: [PATCH 3/5] Added some `async` overloads to `IRazorEngineTemplate` --- RazorEngineCore/IRazorEngineTemplate.cs | 21 ++++++++ .../RazorEngineCompiledTemplate.cs | 2 +- .../RazorEngineCompiledTemplateT.cs | 2 +- RazorEngineCore/RazorEngineTemplateBase.cs | 54 ++++++++++++++++--- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/RazorEngineCore/IRazorEngineTemplate.cs b/RazorEngineCore/IRazorEngineTemplate.cs index 9afa415..f4d0985 100644 --- a/RazorEngineCore/IRazorEngineTemplate.cs +++ b/RazorEngineCore/IRazorEngineTemplate.cs @@ -5,12 +5,33 @@ namespace RazorEngineCore public interface IRazorEngineTemplate { dynamic Model { get; set; } + void WriteLiteral(string literal = null); + + Task WriteLiteralAsync(string literal = null); + void Write(object obj = null); + + Task WriteAsync(object obj = null); + void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount); + + Task BeginWriteAttributeAsync(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount); + void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral); + + Task WriteAttributeValueAsync(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral); + void EndWriteAttribute(); + + Task EndWriteAttributeAsync(); + + void Execute(); + Task ExecuteAsync(); + string Result(); + + Task ResultAsync(); } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngineCompiledTemplate.cs b/RazorEngineCore/RazorEngineCompiledTemplate.cs index 071b70a..48461fe 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplate.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplate.cs @@ -101,7 +101,7 @@ public async Task RunAsync(object model = null) await instance.ExecuteAsync(); - return instance.Result(); + return await instance.ResultAsync(); } } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngineCompiledTemplateT.cs b/RazorEngineCore/RazorEngineCompiledTemplateT.cs index 56dd41b..3e2d392 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplateT.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplateT.cs @@ -96,7 +96,7 @@ public async Task RunAsync(Action initializer) await instance.ExecuteAsync(); - return instance.Result(); + return await instance.ResultAsync(); } } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngineTemplateBase.cs b/RazorEngineCore/RazorEngineTemplateBase.cs index f42ae46..b54e570 100644 --- a/RazorEngineCore/RazorEngineTemplateBase.cs +++ b/RazorEngineCore/RazorEngineTemplateBase.cs @@ -11,32 +11,69 @@ public abstract class RazorEngineTemplateBase : IRazorEngineTemplate public dynamic Model { get; set; } - public virtual void WriteLiteral(string literal = null) + public void WriteLiteral(string literal = null) + { + WriteLiteralAsync(literal).GetAwaiter().GetResult(); + } + + public virtual Task WriteLiteralAsync(string literal = null) { this.stringBuilder.Append(literal); + return Task.CompletedTask; + } + + public void Write(object obj = null) + { + WriteAsync(obj).GetAwaiter().GetResult(); } - public virtual void Write(object obj = null) + public virtual Task WriteAsync(object obj = null) { this.stringBuilder.Append(obj); + return Task.CompletedTask; + } + + public void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, + int attributeValuesCount) + { + BeginWriteAttributeAsync(name, prefix, prefixOffset, suffix, suffixOffset, attributeValuesCount).GetAwaiter().GetResult(); } - public virtual void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount) + public virtual Task BeginWriteAttributeAsync(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount) { this.attributeSuffix = suffix; this.stringBuilder.Append(prefix); + return Task.CompletedTask; + } + + public void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, + bool isLiteral) + { + WriteAttributeValueAsync(prefix, prefixOffset, value, valueOffset, valueLength, isLiteral).GetAwaiter().GetResult(); } - public virtual void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral) + public virtual Task WriteAttributeValueAsync(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral) { this.stringBuilder.Append(prefix); this.stringBuilder.Append(value); + return Task.CompletedTask; } - public virtual void EndWriteAttribute() + public void EndWriteAttribute() + { + EndWriteAttributeAsync().GetAwaiter().GetResult(); + } + + public virtual Task EndWriteAttributeAsync() { this.stringBuilder.Append(this.attributeSuffix); this.attributeSuffix = null; + return Task.CompletedTask; + } + + public void Execute() + { + ExecuteAsync().GetAwaiter().GetResult(); } public virtual Task ExecuteAsync() @@ -46,7 +83,12 @@ public virtual Task ExecuteAsync() public virtual string Result() { - return this.stringBuilder.ToString(); + return ResultAsync().GetAwaiter().GetResult(); + } + + public virtual Task ResultAsync() + { + return Task.FromResult(this.stringBuilder.ToString()); } } } \ No newline at end of file From 07eea7bf6386fdb264172017ceffccab99357378 Mon Sep 17 00:00:00 2001 From: William D Cossey Date: Mon, 19 Apr 2021 18:03:26 +0100 Subject: [PATCH 4/5] A more descriptive error message. --- RazorEngineCore/RazorEngine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RazorEngineCore/RazorEngine.cs b/RazorEngineCore/RazorEngine.cs index b25d1e5..0bdcf3d 100644 --- a/RazorEngineCore/RazorEngine.cs +++ b/RazorEngineCore/RazorEngine.cs @@ -110,7 +110,7 @@ private MemoryStream CreateAndCompileToStream(string templateSource, RazorEngine { List errors = emitResult.Diagnostics.ToList(); - RazorEngineCompilationException exception = new RazorEngineCompilationException($"Unable to compile template: {errors.FirstOrDefault()}") + RazorEngineCompilationException exception = new RazorEngineCompilationException($"Unable to compile template: \n{string.Join("\n", errors)}") { Errors = errors, GeneratedCode = razorCSharpDocument.GeneratedCode From ef39f886b6f34d8051db1de49686ad662d0212d2 Mon Sep 17 00:00:00 2001 From: William D Cossey Date: Tue, 20 Apr 2021 12:48:20 +0100 Subject: [PATCH 5/5] Only include `Errors` in exception message. --- RazorEngineCore/RazorEngine.cs | 6 ++---- RazorEngineCore/RazorEngineCompilationException.cs | 10 +++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/RazorEngineCore/RazorEngine.cs b/RazorEngineCore/RazorEngine.cs index 0bdcf3d..6d83cc6 100644 --- a/RazorEngineCore/RazorEngine.cs +++ b/RazorEngineCore/RazorEngine.cs @@ -108,11 +108,9 @@ private MemoryStream CreateAndCompileToStream(string templateSource, RazorEngine if (!emitResult.Success) { - List errors = emitResult.Diagnostics.ToList(); - - RazorEngineCompilationException exception = new RazorEngineCompilationException($"Unable to compile template: \n{string.Join("\n", errors)}") + RazorEngineCompilationException exception = new RazorEngineCompilationException() { - Errors = errors, + Errors = emitResult.Diagnostics.ToList(), GeneratedCode = razorCSharpDocument.GeneratedCode }; diff --git a/RazorEngineCore/RazorEngineCompilationException.cs b/RazorEngineCore/RazorEngineCompilationException.cs index 4254f03..b53f9be 100644 --- a/RazorEngineCore/RazorEngineCompilationException.cs +++ b/RazorEngineCore/RazorEngineCompilationException.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.Serialization; using Microsoft.CodeAnalysis; @@ -15,15 +16,14 @@ protected RazorEngineCompilationException(SerializationInfo info, StreamingConte { } - public RazorEngineCompilationException(string message) : base(message) - { - } - - public RazorEngineCompilationException(string message, Exception innerException) : base(message, innerException) + public RazorEngineCompilationException(Exception innerException) : base(null, innerException) { } public List Errors { get; set; } + public string GeneratedCode { get; set; } + + public override string Message => $"Unable to compile template: {string.Join("\n", Errors.Where(w => w.IsWarningAsError || w.Severity == DiagnosticSeverity.Error))}"; } } \ No newline at end of file