44using System . IO ;
55using System . Linq ;
66using System . Runtime . Serialization ;
7+ using System . Security . Cryptography ;
78using System . Text ;
89using System . Text . RegularExpressions ;
10+ using System . Threading ;
911using Microsoft . Build . Framework ;
1012using 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 ( ) ;
0 commit comments