Skip to content

Commit 9d0a566

Browse files
authored
Merge pull request #218 from adiessl/master-mutex
Use Mutex to prevent simultaneous SCSS compilation for multi target projects
2 parents 1fdd305 + 98784d8 commit 9d0a566

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

AspNetCore.SassCompiler.Tasks/CompileSass.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System.IO;
55
using System.Linq;
66
using System.Runtime.Serialization;
7+
using System.Security.Cryptography;
78
using System.Text;
89
using System.Text.RegularExpressions;
10+
using System.Threading;
911
using Microsoft.Build.Framework;
1012
using Microsoft.Build.Utilities;
1113

@@ -30,6 +32,10 @@ public string Snapshot
3032

3133
public string Configuration { get; set; }
3234

35+
public string TargetFramework { get; set; }
36+
37+
public string TargetFrameworks { get; set; }
38+
3339
[Output]
3440
public ITaskItem[] GeneratedFiles { get; set; } = Array.Empty<ITaskItem>();
3541

@@ -271,6 +277,49 @@ private IEnumerable<ITaskItem> GenerateCss(SassCompilerOptions options)
271277
}
272278

273279
private (bool Success, string Output, string Error) GenerateCss(string arguments)
280+
{
281+
if (string.IsNullOrWhiteSpace(TargetFrameworks))
282+
{
283+
return CompileCss(arguments);
284+
}
285+
286+
var mutexName = GetMutexName();
287+
288+
Log.LogMessage(MessageImportance.Normal,
289+
$"TargetFramework: '{TargetFramework}'; TargetFrameworks: '{TargetFrameworks}'; using mutex {mutexName}");
290+
291+
using var mutex = new Mutex(false, mutexName);
292+
293+
try
294+
{
295+
mutex.WaitOne();
296+
}
297+
catch (AbandonedMutexException)
298+
{
299+
return (false, string.Empty, $"Mutex {mutexName} was abandoned");
300+
}
301+
302+
try
303+
{
304+
return CompileCss(arguments);
305+
}
306+
finally
307+
{
308+
mutex.ReleaseMutex();
309+
}
310+
311+
string GetMutexName()
312+
{
313+
using var sha256 = SHA256.Create();
314+
315+
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(arguments));
316+
var hashString = BitConverter.ToString(hash).Replace("-", "");
317+
318+
return $"{nameof(AspNetCore)}.{nameof(SassCompiler)}_{hashString}";
319+
}
320+
}
321+
322+
private (bool Success, string Output, string Error) CompileCss(string arguments)
274323
{
275324
var compiler = new Process();
276325
compiler.StartInfo = new ProcessStartInfo
@@ -290,11 +339,11 @@ private IEnumerable<ITaskItem> GenerateCss(SassCompilerOptions options)
290339
{
291340
error += e.Data + Environment.NewLine;
292341
};
293-
342+
294343
compiler.Start();
295344

296345
compiler.BeginErrorReadLine();
297-
346+
298347
var output = compiler.StandardOutput.ReadToEnd();
299348

300349
compiler.WaitForExit();

AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<Project>
2-
1+
<Project>
2+
33
<PropertyGroup Condition="'$(SassCompilerIncludeRuntime)' == 'true'">
44
<SassCompilerEnableWatcher>$(SassCompilerIncludeRuntime)</SassCompilerEnableWatcher>
55
<SassCompilerRuntimeCopyToPublishDirectory>PreserveNewest</SassCompilerRuntimeCopyToPublishDirectory>
@@ -12,12 +12,16 @@
1212
<UsingTask TaskName="AspNetCore.SassCompiler.CompileSass"
1313
AssemblyFile="$(SassCompilerTasksAssembly)" />
1414

15-
<Target Name="Compile Sass" BeforeTargets="Build;ResolveScopedCssInputs;BundleMinify;ResolveProjectStaticWebAssets" Condition="'$(DesignTimeBuild)' != 'true'">
15+
<Target Name="Compile Sass"
16+
BeforeTargets="Build;ResolveScopedCssInputs;BundleMinify;ResolveProjectStaticWebAssets"
17+
Condition="'$(DesignTimeBuild)' != 'true'">
1618
<CompileSass AppsettingsFile="$(SassCompilerAppsettingsJson)"
1719
SassCompilerFile="$(SassCompilerSassCompilerJson)"
1820
Command="$(SassCompilerBuildCommand)"
1921
Snapshot="$(SassCompilerBuildSnapshot)"
20-
Configuration="$(SassCompilerConfiguration)">
22+
Configuration="$(SassCompilerConfiguration)"
23+
TargetFramework="$(TargetFramework)"
24+
TargetFrameworks="$(TargetFrameworks)">
2125
<Output TaskParameter="GeneratedFiles"
2226
ItemName="CompiledCssFiles" />
2327
</CompileSass>

0 commit comments

Comments
 (0)