Skip to content

Commit

Permalink
Serialize file access
Browse files Browse the repository at this point in the history
  • Loading branch information
mikes-gh committed Dec 31, 2023
1 parent 42258c1 commit 11bcced
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
49 changes: 32 additions & 17 deletions CompileJS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand All @@ -17,40 +18,54 @@ public class CompileJS : Task

public override bool Execute()
{
var sourceDirectory = new DirectoryInfo(SourceDirectory);
var sourceFiles = sourceDirectory.GetFiles("*.js");
var sourceFiles = new DirectoryInfo(SourceDirectory).GetFiles("*.js");
var maxSourceWriteTime = new DateTime();
var combinedJS = new StringBuilder();

foreach (var sourceFile in sourceFiles)
{
maxSourceWriteTime = sourceFile.LastWriteTime > maxSourceWriteTime ? sourceFile.LastWriteTime : maxSourceWriteTime;
var fileText = File.ReadAllText(sourceFile.FullName);
combinedJS.Append(fileText);
}

var compressedJS = JavaScriptCompressor.Compress(combinedJS.ToString());

if (File.Exists(DestinationFile))
using (Mutex mutex = new Mutex(false, DestinationFile))
{
if (File.GetLastWriteTime(DestinationFile) < maxSourceWriteTime)
mutex.WaitOne(5000);

if (File.Exists(DestinationFile))
{
File.WriteAllText(DestinationFile, compressedJS);
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Updated");
if (File.GetLastWriteTime(DestinationFile) < maxSourceWriteTime)
{
WriteFile();
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Updated");
}
else
{
Log.LogMessage(MessageImportance.High, $"{DestinationFile} UpToDate");
}
}
else
{
Log.LogMessage(MessageImportance.High, $"{DestinationFile} UpToDate");
WriteFile();
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Created");
}
}
else
{
File.WriteAllText(DestinationFile, compressedJS);
Log.LogMessage(MessageImportance.High, $"{DestinationFile} Created");

mutex.ReleaseMutex();
}

GeneratedFile = new TaskItem(DestinationFile);
return true;
}

private void WriteFile()
{
var sourceFiles = new DirectoryInfo(SourceDirectory).GetFiles("*.js");
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);
}
}
}
2 changes: 1 addition & 1 deletion MudBlazor.JSCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MudBlazor.JSCompiler</PackageId>
<Version>1.0.10</Version>
<Version>1.0.11</Version>
<Authors>mikes-gh</Authors>
<Description>MudBlazor JS Compiler Library</Description>
<PackageDescription>MudBlazor JS Compiler Library</PackageDescription>
Expand Down

0 comments on commit 11bcced

Please sign in to comment.