Skip to content

Commit

Permalink
Merge pull request #218 from adiessl/master-mutex
Browse files Browse the repository at this point in the history
Use Mutex to prevent simultaneous SCSS compilation for multi target projects
  • Loading branch information
sleeuwen authored Jan 6, 2025
2 parents 1fdd305 + 98784d8 commit 9d0a566
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
53 changes: 51 additions & 2 deletions AspNetCore.SassCompiler.Tasks/CompileSass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand All @@ -30,6 +32,10 @@ public string Snapshot

public string Configuration { get; set; }

public string TargetFramework { get; set; }

public string TargetFrameworks { get; set; }

[Output]
public ITaskItem[] GeneratedFiles { get; set; } = Array.Empty<ITaskItem>();

Expand Down Expand Up @@ -271,6 +277,49 @@ private IEnumerable<ITaskItem> GenerateCss(SassCompilerOptions options)
}

private (bool Success, string Output, string Error) GenerateCss(string arguments)
{
if (string.IsNullOrWhiteSpace(TargetFrameworks))
{
return CompileCss(arguments);
}

var mutexName = GetMutexName();

Log.LogMessage(MessageImportance.Normal,
$"TargetFramework: '{TargetFramework}'; TargetFrameworks: '{TargetFrameworks}'; using mutex {mutexName}");

using var mutex = new Mutex(false, mutexName);

try
{
mutex.WaitOne();
}
catch (AbandonedMutexException)
{
return (false, string.Empty, $"Mutex {mutexName} was abandoned");
}

try
{
return CompileCss(arguments);
}
finally
{
mutex.ReleaseMutex();
}

string GetMutexName()
{
using var sha256 = SHA256.Create();

var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(arguments));
var hashString = BitConverter.ToString(hash).Replace("-", "");

return $"{nameof(AspNetCore)}.{nameof(SassCompiler)}_{hashString}";
}
}

private (bool Success, string Output, string Error) CompileCss(string arguments)
{
var compiler = new Process();
compiler.StartInfo = new ProcessStartInfo
Expand All @@ -290,11 +339,11 @@ private IEnumerable<ITaskItem> GenerateCss(SassCompilerOptions options)
{
error += e.Data + Environment.NewLine;
};

compiler.Start();

compiler.BeginErrorReadLine();

var output = compiler.StandardOutput.ReadToEnd();

compiler.WaitForExit();
Expand Down
12 changes: 8 additions & 4 deletions AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<Project>

<PropertyGroup Condition="'$(SassCompilerIncludeRuntime)' == 'true'">
<SassCompilerEnableWatcher>$(SassCompilerIncludeRuntime)</SassCompilerEnableWatcher>
<SassCompilerRuntimeCopyToPublishDirectory>PreserveNewest</SassCompilerRuntimeCopyToPublishDirectory>
Expand All @@ -12,12 +12,16 @@
<UsingTask TaskName="AspNetCore.SassCompiler.CompileSass"
AssemblyFile="$(SassCompilerTasksAssembly)" />

<Target Name="Compile Sass" BeforeTargets="Build;ResolveScopedCssInputs;BundleMinify;ResolveProjectStaticWebAssets" Condition="'$(DesignTimeBuild)' != 'true'">
<Target Name="Compile Sass"
BeforeTargets="Build;ResolveScopedCssInputs;BundleMinify;ResolveProjectStaticWebAssets"
Condition="'$(DesignTimeBuild)' != 'true'">
<CompileSass AppsettingsFile="$(SassCompilerAppsettingsJson)"
SassCompilerFile="$(SassCompilerSassCompilerJson)"
Command="$(SassCompilerBuildCommand)"
Snapshot="$(SassCompilerBuildSnapshot)"
Configuration="$(SassCompilerConfiguration)">
Configuration="$(SassCompilerConfiguration)"
TargetFramework="$(TargetFramework)"
TargetFrameworks="$(TargetFrameworks)">
<Output TaskParameter="GeneratedFiles"
ItemName="CompiledCssFiles" />
</CompileSass>
Expand Down

0 comments on commit 9d0a566

Please sign in to comment.