diff --git a/docs-prep.cake b/docs-prep.cake index ae609ed..07b45c7 100644 --- a/docs-prep.cake +++ b/docs-prep.cake @@ -1,4 +1,4 @@ -#addin nuget:?package=Cake.FileHelpers&version=2.0.0 +#addin nuget:?package=Cake.FileHelpers&version=3.1.0 Task("Copy-Readme-For-Docs") .WithCriteria(() => FileExists("README.md")) diff --git a/nuspec/nuget/Cake.Graph.nuspec b/nuspec/nuget/Cake.Graph.nuspec index bcda5ce..6327d70 100644 --- a/nuspec/nuget/Cake.Graph.nuspec +++ b/nuspec/nuget/Cake.Graph.nuspec @@ -14,7 +14,7 @@ Cake Script Build Graph Nodes Dependencies Tasks - - + + \ No newline at end of file diff --git a/src/Cake.Graph.Tests/Cake.Graph.Tests.csproj b/src/Cake.Graph.Tests/Cake.Graph.Tests.csproj index 3cececf..d2a7667 100644 --- a/src/Cake.Graph.Tests/Cake.Graph.Tests.csproj +++ b/src/Cake.Graph.Tests/Cake.Graph.Tests.csproj @@ -1,11 +1,11 @@  - - net46 + netcoreapp2.0 false + true - + true full false @@ -13,48 +13,34 @@ prompt 4 - + pdbonly true TRACE prompt 4 - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/src/Cake.Graph.Tests/GraphGeneratorTests.cs b/src/Cake.Graph.Tests/GraphGeneratorTests.cs index ad0ceae..5fb1fcf 100644 --- a/src/Cake.Graph.Tests/GraphGeneratorTests.cs +++ b/src/Cake.Graph.Tests/GraphGeneratorTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Cake.Core; using Cake.Graph.Generators; using Shouldly; @@ -25,11 +26,11 @@ public GraphGeneratorTests() [InlineData(typeof(MermaidHtmlGenerator), TestHelpers.TaskCMermaidPattern)] [InlineData(typeof(CytoscapeHtmlGenerator), TestHelpers.TaskCCytoscapePattern)] [InlineData(typeof(CytoscapeWyamGenerator), TestHelpers.TaskCCytoscapePattern)] - public void Serializes_Tasks_With_Dependencies_Correctly(Type generatorType, string expectedResult) + public async Task Serializes_Tasks_With_Dependencies_Correctly(Type generatorType, string expectedResult) { var mockContext = TestHelpers.GetMockCakeContext(); var graphGenerator = (ITaskGraphGenerator)Activator.CreateInstance(generatorType); - var result = graphGenerator.Serialize(mockContext.Object, taskC, tasks); + var result = await graphGenerator.SerializeAsync(mockContext.Object, taskC, tasks); result.ShouldMatch(expectedResult); } diff --git a/src/Cake.Graph.Tests/GraphRunnerTests.cs b/src/Cake.Graph.Tests/GraphRunnerTests.cs index 6e61990..477f8a7 100644 --- a/src/Cake.Graph.Tests/GraphRunnerTests.cs +++ b/src/Cake.Graph.Tests/GraphRunnerTests.cs @@ -18,7 +18,7 @@ public void Deploy_Creates_Files_For_Each_Task() var tasks = TestHelpers.CreateTasksWithDependencies(); var emptyGenerator = TestHelpers.GetEmptyTaskGraphGenerator(); - cakeContext.Object.Graph(tasks).Deploy(s => s + cakeContext.Object.Graph(tasks).DeployAsync(s => s .WithCustomGenerator(emptyGenerator) .SetOutputPath(outputPath) ); diff --git a/src/Cake.Graph.Tests/GraphTemplateManagerTests.cs b/src/Cake.Graph.Tests/GraphTemplateManagerTests.cs index 8d4b6eb..ea0ae22 100644 --- a/src/Cake.Graph.Tests/GraphTemplateManagerTests.cs +++ b/src/Cake.Graph.Tests/GraphTemplateManagerTests.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Reflection; +using System.Threading.Tasks; using Cake.Graph.Templates; using Shouldly; using Xunit; @@ -16,12 +17,12 @@ public class GraphTemplateManagerTests }); [Fact] - public void Manager_Parses_Razor_Template() + public async Task Manager_Parses_Razor_Template() { const string test = "test"; var graphTemplateManager = new GraphTemplateManager(graphTemplateRepository); - var result = graphTemplateManager.ParseTemplate(TemplateTypes.Cytoscape, test); - result = graphTemplateManager.ParseTemplate(TemplateTypes.Cytoscape, test); + var result = await graphTemplateManager.ParseTemplateAsync(TemplateTypes.Cytoscape, test); + result = await graphTemplateManager.ParseTemplateAsync(TemplateTypes.Cytoscape, test); result.ShouldBe(GraphTemplateRepositoryTests.TestFileContent.Replace("@Model", test)); } diff --git a/src/Cake.Graph.Tests/TestHelpers.cs b/src/Cake.Graph.Tests/TestHelpers.cs index 9427f49..4978211 100644 --- a/src/Cake.Graph.Tests/TestHelpers.cs +++ b/src/Cake.Graph.Tests/TestHelpers.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Cake.Core; using Cake.Core.Diagnostics; using Cake.Graph.Generators; @@ -24,9 +25,9 @@ public static ITaskGraphGenerator GetEmptyTaskGraphGenerator() mockTaskGraphGenerator .Setup(x => x.Extension) .Returns("test"); - mockTaskGraphGenerator.Setup(x => x.Serialize(It.IsAny(), It.IsAny(), + mockTaskGraphGenerator.Setup(x => x.SerializeAsync(It.IsAny(), It.IsAny(), It.IsAny>())) - .Returns((ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) => ""); + .Returns((ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) => Task.FromResult("")); return mockTaskGraphGenerator.Object; } diff --git a/src/Cake.Graph/Cake.Graph.csproj b/src/Cake.Graph/Cake.Graph.csproj index 2c8de2f..b957836 100644 --- a/src/Cake.Graph/Cake.Graph.csproj +++ b/src/Cake.Graph/Cake.Graph.csproj @@ -1,10 +1,11 @@  - net46 + netstandard2.0 false + true - + true full false @@ -13,7 +14,7 @@ 4 bin\Cake.Graph.xml - + pdbonly true TRACE @@ -27,24 +28,12 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + diff --git a/src/Cake.Graph/Generators/CytoscapeGraphGenerator.cs b/src/Cake.Graph/Generators/CytoscapeGraphGenerator.cs index 4c685d5..001acec 100644 --- a/src/Cake.Graph/Generators/CytoscapeGraphGenerator.cs +++ b/src/Cake.Graph/Generators/CytoscapeGraphGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Cake.Core; using Cake.Core.Diagnostics; using Newtonsoft.Json; @@ -18,12 +19,12 @@ public class CytoscapeGraphGenerator : ITaskGraphGenerator /// /// /// - public string Serialize(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) + public Task SerializeAsync(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) { TaskGraphGeneratorHelpers.ValidateParameters(context, task, tasks); var nodes = GetTaskGraphNodes(context, task, tasks); - return JsonConvert.SerializeObject(nodes); + return Task.FromResult(JsonConvert.SerializeObject(nodes)); } /// diff --git a/src/Cake.Graph/Generators/CytoscapeHtmlGenerator.cs b/src/Cake.Graph/Generators/CytoscapeHtmlGenerator.cs index c16b0d9..6c4c7d4 100644 --- a/src/Cake.Graph/Generators/CytoscapeHtmlGenerator.cs +++ b/src/Cake.Graph/Generators/CytoscapeHtmlGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Cake.Core; using Cake.Graph.Models; using Cake.Graph.Templates; @@ -48,11 +49,11 @@ public CytoscapeHtmlGenerator(IGraphTemplateManager graphTemplateManager) protected TemplateTypes templateType = TemplateTypes.Cytoscape; /// - public string Serialize(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) + public async Task SerializeAsync(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) { - var graph = graphGenerator.Serialize(context, task, tasks); + var graph = await graphGenerator.SerializeAsync(context, task, tasks); var model = new GraphHtmlModel(task.Name, CytoscapeJsSource, graph); - var html = graphTemplateManager.ParseTemplate(templateType, model); + var html = await graphTemplateManager.ParseTemplateAsync(templateType, model); return html; } } diff --git a/src/Cake.Graph/Generators/ITaskGraphGenerator.cs b/src/Cake.Graph/Generators/ITaskGraphGenerator.cs index 44cc58a..72a31f2 100644 --- a/src/Cake.Graph/Generators/ITaskGraphGenerator.cs +++ b/src/Cake.Graph/Generators/ITaskGraphGenerator.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Cake.Core; namespace Cake.Graph.Generators @@ -15,7 +16,7 @@ public interface ITaskGraphGenerator /// /// /// - string Serialize(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks); + Task SerializeAsync(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks); /// /// The file extension to use for this type of graph file /// diff --git a/src/Cake.Graph/Generators/MermaidGraphGenerator.cs b/src/Cake.Graph/Generators/MermaidGraphGenerator.cs index 99ac37f..aaf8369 100644 --- a/src/Cake.Graph/Generators/MermaidGraphGenerator.cs +++ b/src/Cake.Graph/Generators/MermaidGraphGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; using Cake.Core; using Cake.Core.Diagnostics; @@ -24,7 +25,7 @@ public class MermaidGraphGenerator : ITaskGraphGenerator /// /// /// - public string Serialize(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) + public async Task SerializeAsync(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) { TaskGraphGeneratorHelpers.ValidateParameters(context, task, tasks); diff --git a/src/Cake.Graph/Generators/MermaidHtmlGenerator.cs b/src/Cake.Graph/Generators/MermaidHtmlGenerator.cs index eee94d9..1eab80b 100644 --- a/src/Cake.Graph/Generators/MermaidHtmlGenerator.cs +++ b/src/Cake.Graph/Generators/MermaidHtmlGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Cake.Core; using Cake.Graph.Models; using Cake.Graph.Templates; @@ -45,11 +46,11 @@ public MermaidHtmlGenerator(IGraphTemplateManager graphTemplateManager) /// /// /// - public string Serialize(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) + public async Task SerializeAsync(ICakeContext context, ICakeTaskInfo task, IReadOnlyList tasks) { - var graph = graphGenerator.Serialize(context, task, tasks); + var graph = await graphGenerator.SerializeAsync(context, task, tasks); var model = new GraphHtmlModel(task.Name, MermaidJsSource, graph); - var html = graphTemplateManager.ParseTemplate(TemplateTypes.Mermaid, model); + var html = await graphTemplateManager.ParseTemplateAsync(TemplateTypes.Mermaid, model); return html; } } diff --git a/src/Cake.Graph/GraphRunner.cs b/src/Cake.Graph/GraphRunner.cs index 0af8e40..bc7ddcb 100644 --- a/src/Cake.Graph/GraphRunner.cs +++ b/src/Cake.Graph/GraphRunner.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Threading.Tasks; using Cake.Core; using Cake.Core.Diagnostics; using LogLevel = Cake.Core.Diagnostics.LogLevel; @@ -30,19 +32,26 @@ public GraphRunner(ICakeContext context, IReadOnlyList tasks) /// Generate the node set files and deploy the web content files /// /// - public GraphRunner Deploy(Action configure = null) + public async Task DeployAsync(Action configure = null) { var settings = new GraphSettings(); configure?.Invoke(settings); - return Deploy(settings); + return await DeployAsync(settings); } + /// + /// Generate the node set files and deploy the web content files + /// + /// + public GraphRunner Deploy(Action configure = null) => + DeployAsync(configure).GetAwaiter().GetResult(); + /// /// Generate the node set files and deploy the web content files /// /// - public GraphRunner Deploy(GraphSettings settings) + public async Task DeployAsync(GraphSettings settings) { if (settings.Generator == null) { @@ -50,23 +59,28 @@ public GraphRunner Deploy(GraphSettings settings) settings.WithMermaidGenerator(); } - var output = - tasks.Select( - x => new KeyValuePair(x.Name, settings.Generator.Serialize(context, x, tasks))); - context.Log.Write(Verbosity.Normal, LogLevel.Information, "Writing files"); context.Log.Write(Verbosity.Diagnostic, LogLevel.Information, $"Ensuring node sets directory at {settings.OutputPath}"); + if (!string.IsNullOrWhiteSpace(settings.OutputPath)) Directory.CreateDirectory(settings.OutputPath); - - foreach (var task in output) + + foreach (var task in tasks) { - var filePath = Path.Combine(settings.OutputPath, $"{task.Key}.{settings.Generator.Extension}"); + var value = await settings.Generator.SerializeAsync(context, task, tasks); + + var filePath = Path.Combine(settings.OutputPath, $"{task.Name}.{settings.Generator.Extension}"); using (var file = File.CreateText(filePath)) - file.Write(task.Value); + file.Write(value); } return this; } + + /// + /// Generate the node set files and deploy the web content files + /// + /// + public GraphRunner Deploy(GraphSettings settings) => DeployAsync(settings).GetAwaiter().GetResult(); } } \ No newline at end of file diff --git a/src/Cake.Graph/GraphTemplate.cs b/src/Cake.Graph/GraphTemplate.cs index 761aef0..d6c7878 100644 --- a/src/Cake.Graph/GraphTemplate.cs +++ b/src/Cake.Graph/GraphTemplate.cs @@ -1,4 +1,7 @@ -using RazorEngine.Templating; +//using RazorEngine.Templating; + +using System.Threading.Tasks; +using RazorLight; namespace Cake.Graph { @@ -7,14 +10,16 @@ namespace Cake.Graph /// Template for processing web files before saving /// /// - public class GraphTemplate : TemplateBase + public class GraphTemplate : TemplatePage { /// /// Model to use in Template /// - public new T Model + public new T Model => base.Model; + + public override Task ExecuteAsync() { - get => base.Model; + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/Cake.Graph/Templates/GraphTemplateManager.cs b/src/Cake.Graph/Templates/GraphTemplateManager.cs index db6a79a..63c1d11 100644 --- a/src/Cake.Graph/Templates/GraphTemplateManager.cs +++ b/src/Cake.Graph/Templates/GraphTemplateManager.cs @@ -1,7 +1,6 @@ using System; -using RazorEngine; -using RazorEngine.Configuration; -using RazorEngine.Templating; +using System.Threading.Tasks; +using RazorLight; namespace Cake.Graph.Templates { @@ -10,17 +9,8 @@ namespace Cake.Graph.Templates /// public class GraphTemplateManager : IGraphTemplateManager { - private static readonly ITemplateServiceConfiguration razorTemplateServiceConfig = new TemplateServiceConfiguration - { - DisableTempFileLocking = true, // loads the files in-memory (gives the templates full-trust permissions) - CachingProvider = new DefaultCachingProvider(t => { }) //disables the warnings - }; - - static GraphTemplateManager() - { - Engine.Razor = RazorEngineService.Create(razorTemplateServiceConfig); - } - + private RazorLightEngine Engine { get; } + private readonly IGraphTemplateRepository graphTemplateRepository; /// @@ -35,6 +25,10 @@ static GraphTemplateManager() public GraphTemplateManager(IGraphTemplateRepository graphTemplateRepository) { this.graphTemplateRepository = graphTemplateRepository ?? throw new ArgumentNullException(nameof(graphTemplateRepository)); + + Engine = new RazorLightEngineBuilder() + .UseMemoryCachingProvider() + .Build(); } /// @@ -45,35 +39,32 @@ public GraphTemplateManager(IGraphTemplateRepository graphTemplateRepository) /// /// /// - public string ParseTemplate(TemplateTypes templateTypeKey, T model) + public async Task ParseTemplateAsync(TemplateTypes templateTypeKey, T model) { if (!graphTemplateRepository.TemplateResourcePaths.TryGetValue(templateTypeKey, out var templateResourcePath)) throw new ArgumentOutOfRangeException(nameof(templateTypeKey)); - var result = ParseRazorTemplateFromResource(templateResourcePath, model); + var result = await ParseRazorTemplateFromResourceAsync(templateResourcePath, model); return result; } - private string ParseRazorTemplateFromResource(string resourcePath, T model) + private async Task ParseRazorTemplateFromResourceAsync(string resourcePath, T model) { - var cached = Engine.Razor.IsTemplateCached(resourcePath, typeof(T)); - if (cached) - return ParseCachedRazorTemplate(resourcePath, model); + if (Engine.TemplateCache.Contains(resourcePath)) + { + var template = Engine.TemplateCache.RetrieveTemplate(resourcePath).Template.TemplatePageFactory.Invoke(); + return await Engine.RenderTemplateAsync(template, model); + } var razorTemplate = graphTemplateRepository.ReadResourceToString(resourcePath); - var htmlFileOutput = ParseAndCacheRazorTemplate(resourcePath, razorTemplate, model); + var htmlFileOutput = await ParseAndCacheRazorTemplateAsync(resourcePath, razorTemplate, model); return htmlFileOutput; } - private string ParseAndCacheRazorTemplate(string templateName, string razorTemplate, T model) + private async Task ParseAndCacheRazorTemplateAsync(string templateName, string razorTemplate, T model) { - var htmlFileOutput = Engine.Razor.RunCompile(razorTemplate, templateName, typeof(T), model); + var htmlFileOutput = await Engine.CompileRenderAsync(templateName, razorTemplate, model); return htmlFileOutput; } - - private string ParseCachedRazorTemplate(string resourcePath, T model) - { - return Engine.Razor.Run(resourcePath, typeof(T), model); - } } } \ No newline at end of file diff --git a/src/Cake.Graph/Templates/IGraphTemplateManager.cs b/src/Cake.Graph/Templates/IGraphTemplateManager.cs index 66c427e..9fec588 100644 --- a/src/Cake.Graph/Templates/IGraphTemplateManager.cs +++ b/src/Cake.Graph/Templates/IGraphTemplateManager.cs @@ -1,3 +1,5 @@ +using System.Threading.Tasks; + namespace Cake.Graph.Templates { /// @@ -12,6 +14,6 @@ public interface IGraphTemplateManager /// /// /// - string ParseTemplate(TemplateTypes templateTypeKey, T model); + Task ParseTemplateAsync(TemplateTypes templateTypeKey, T model); } } \ No newline at end of file diff --git a/tools/packages.config b/tools/packages.config index e52a2c7..3c65df8 100644 --- a/tools/packages.config +++ b/tools/packages.config @@ -1,4 +1,4 @@ - +