From b5ab098fb6ae7060527eb60e61412ca09aa04896 Mon Sep 17 00:00:00 2001 From: Mike Surcouf Date: Sat, 30 Dec 2023 23:42:47 +0000 Subject: [PATCH] Reduce contention on destination file --- CompileJS.cs | 35 ++++++++++++++++++++++++++--------- MudBlazor.JSCompiler.csproj | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CompileJS.cs b/CompileJS.cs index 2eb5e0c..af4b26f 100644 --- a/CompileJS.cs +++ b/CompileJS.cs @@ -1,4 +1,6 @@ +using System; using System.IO; +using System.Text; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; @@ -15,21 +17,36 @@ public class CompileJS : Task public override bool Execute() { - File.Delete(DestinationFile); var sourceDirectory = new DirectoryInfo(SourceDirectory); - var files = sourceDirectory.GetFiles("*.js"); + var sourceFiles = sourceDirectory.GetFiles("*.js"); + var maxSourceWriteTime = new DateTime(); + var combinedJS = new StringBuilder(); - foreach (var file in files) + foreach (var sourceFile in sourceFiles) { - var fileText = File.ReadAllText(file.FullName); - File.AppendAllText(DestinationFile, fileText); + maxSourceWriteTime = sourceFile.LastWriteTime > maxSourceWriteTime ? sourceFile.LastWriteTime : maxSourceWriteTime; + var fileText = File.ReadAllText(sourceFile.FullName); + combinedJS.Append(fileText); } - using (var reader = new StreamReader(DestinationFile)) + var compressedJS = JavaScriptCompressor.Compress(combinedJS.ToString()); + + if (File.Exists(DestinationFile)) + { + if (File.GetLastWriteTime(DestinationFile) < maxSourceWriteTime) + { + File.WriteAllText(DestinationFile, compressedJS); + Log.LogMessage(MessageImportance.High, $"{DestinationFile} Updated"); + } + else + { + Log.LogMessage(MessageImportance.High, $"{DestinationFile} UpToDate"); + } + } + else { - var writer = new StringWriter(); - JavaScriptCompressor.Compress(reader, writer); - File.WriteAllText(DestinationFile, writer.GetStringBuilder().ToString(), reader.CurrentEncoding); + File.WriteAllText(DestinationFile, compressedJS); + Log.LogMessage(MessageImportance.High, $"{DestinationFile} Created"); } GeneratedFile = new TaskItem(DestinationFile); diff --git a/MudBlazor.JSCompiler.csproj b/MudBlazor.JSCompiler.csproj index 9ab34da..4585c16 100644 --- a/MudBlazor.JSCompiler.csproj +++ b/MudBlazor.JSCompiler.csproj @@ -3,7 +3,7 @@ netstandard2.0 MudBlazor.JSCompiler - 1.0.7 + 1.0.8 mikes-gh MudBlazor JS Compiler Library MudBlazor JS Compiler Library