diff --git a/CompileJS.cs b/CompileJS.cs index 2eb5e0c..99b169c 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,43 @@ public class CompileJS : Task public override bool Execute() { - File.Delete(DestinationFile); - var sourceDirectory = new DirectoryInfo(SourceDirectory); - var files = sourceDirectory.GetFiles("*.js"); + var sourceFiles = new DirectoryInfo(SourceDirectory).GetFiles("*.js"); + var maxSourceWriteTime = new DateTime(); + bool destinationNeedsUpdate = false; - 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; } - using (var reader = new StreamReader(DestinationFile)) + if (File.Exists(DestinationFile)) { - var writer = new StringWriter(); - JavaScriptCompressor.Compress(reader, writer); - File.WriteAllText(DestinationFile, writer.GetStringBuilder().ToString(), reader.CurrentEncoding); + if (File.GetLastWriteTime(DestinationFile) < maxSourceWriteTime) + { + destinationNeedsUpdate = true; + Log.LogMessage(MessageImportance.High, $"{DestinationFile} Updated"); + } + else + { + Log.LogMessage(MessageImportance.High, $"{DestinationFile} UpToDate"); + } + } + else + { + destinationNeedsUpdate = true; + Log.LogMessage(MessageImportance.High, $"{DestinationFile} Created"); + } + + if (destinationNeedsUpdate) + { + var combinedJS = new StringBuilder(); + foreach (var sourceFile in sourceFiles) + { + var fileText = File.ReadAllText(sourceFile.FullName); + combinedJS.Append(fileText); + } + var compressedJS = JavaScriptCompressor.Compress(combinedJS.ToString()); + File.WriteAllText(DestinationFile, compressedJS); } GeneratedFile = new TaskItem(DestinationFile); diff --git a/MudBlazor.JSCompiler.csproj b/MudBlazor.JSCompiler.csproj index 9ab34da..2e08bae 100644 --- a/MudBlazor.JSCompiler.csproj +++ b/MudBlazor.JSCompiler.csproj @@ -3,7 +3,7 @@ netstandard2.0 MudBlazor.JSCompiler - 1.0.7 + 1.0.9 mikes-gh MudBlazor JS Compiler Library MudBlazor JS Compiler Library