From a3fe37ee217e5f9618e367f78667a2020b332c33 Mon Sep 17 00:00:00 2001 From: Alexander Diessl Date: Thu, 2 Jan 2025 12:33:08 +0100 Subject: [PATCH 1/6] Remove trailing whitespace --- AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets index b7dadd5..7ad7fc2 100644 --- a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets +++ b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets @@ -1,5 +1,5 @@  - + $(SassCompilerIncludeRuntime) PreserveNewest From a53cdf6bc6033aa8b7ac05858ba3a3acd5aba4fd Mon Sep 17 00:00:00 2001 From: Alexander Diessl Date: Sat, 4 Jan 2025 20:44:44 +0100 Subject: [PATCH 2/6] Format code --- AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets index 7ad7fc2..2b35076 100644 --- a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets +++ b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets @@ -12,7 +12,9 @@ - + Date: Sat, 4 Jan 2025 20:46:04 +0100 Subject: [PATCH 3/6] Pass "TargetFramework" and "TargetFrameworks" to MSBuild task --- AspNetCore.SassCompiler.Tasks/CompileSass.cs | 4 ++++ .../build/AspNetCore.SassCompiler.targets | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/AspNetCore.SassCompiler.Tasks/CompileSass.cs b/AspNetCore.SassCompiler.Tasks/CompileSass.cs index ca0be9a..012a2ba 100644 --- a/AspNetCore.SassCompiler.Tasks/CompileSass.cs +++ b/AspNetCore.SassCompiler.Tasks/CompileSass.cs @@ -30,6 +30,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(); diff --git a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets index 2b35076..86b04aa 100644 --- a/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets +++ b/AspNetCore.SassCompiler/build/AspNetCore.SassCompiler.targets @@ -1,4 +1,4 @@ - + $(SassCompilerIncludeRuntime) @@ -19,7 +19,9 @@ SassCompilerFile="$(SassCompilerSassCompilerJson)" Command="$(SassCompilerBuildCommand)" Snapshot="$(SassCompilerBuildSnapshot)" - Configuration="$(SassCompilerConfiguration)"> + Configuration="$(SassCompilerConfiguration)" + TargetFramework="$(TargetFramework)" + TargetFrameworks="$(TargetFrameworks)"> From e540840940d55d5e3883b1fc88ed671ade8577ac Mon Sep 17 00:00:00 2001 From: Alexander Diessl Date: Sat, 4 Jan 2025 20:46:23 +0100 Subject: [PATCH 4/6] Remove trailing whitespace --- AspNetCore.SassCompiler.Tasks/CompileSass.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AspNetCore.SassCompiler.Tasks/CompileSass.cs b/AspNetCore.SassCompiler.Tasks/CompileSass.cs index 012a2ba..d590afb 100644 --- a/AspNetCore.SassCompiler.Tasks/CompileSass.cs +++ b/AspNetCore.SassCompiler.Tasks/CompileSass.cs @@ -294,11 +294,11 @@ private IEnumerable GenerateCss(SassCompilerOptions options) { error += e.Data + Environment.NewLine; }; - + compiler.Start(); compiler.BeginErrorReadLine(); - + var output = compiler.StandardOutput.ReadToEnd(); compiler.WaitForExit(); From 6c56f8baaed834627c307d44fa99f6778abf9a4f Mon Sep 17 00:00:00 2001 From: Alexander Diessl Date: Sat, 4 Jan 2025 20:50:59 +0100 Subject: [PATCH 5/6] Use Mutex to make sure SCSS compilation does not happen simultaneously Using a Mutex is only necessary for projects targeting multiple frameworks, in all other cases the compilation happens directly like before. Including the hashed arguments in the Mutex name makes sure the compilation for different projects (using different arguments) can still happen simultaneously. --- AspNetCore.SassCompiler.Tasks/CompileSass.cs | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/AspNetCore.SassCompiler.Tasks/CompileSass.cs b/AspNetCore.SassCompiler.Tasks/CompileSass.cs index d590afb..7ef5b5e 100644 --- a/AspNetCore.SassCompiler.Tasks/CompileSass.cs +++ b/AspNetCore.SassCompiler.Tasks/CompileSass.cs @@ -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; @@ -275,6 +277,49 @@ private IEnumerable 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, + $"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 From 98784d87a796f58f66485326713d15fdf9efc489 Mon Sep 17 00:00:00 2001 From: Alexander Diessl Date: Mon, 6 Jan 2025 09:07:02 +0100 Subject: [PATCH 6/6] Log TargetFramework as well --- AspNetCore.SassCompiler.Tasks/CompileSass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AspNetCore.SassCompiler.Tasks/CompileSass.cs b/AspNetCore.SassCompiler.Tasks/CompileSass.cs index 7ef5b5e..3a7801b 100644 --- a/AspNetCore.SassCompiler.Tasks/CompileSass.cs +++ b/AspNetCore.SassCompiler.Tasks/CompileSass.cs @@ -286,7 +286,7 @@ private IEnumerable GenerateCss(SassCompilerOptions options) var mutexName = GetMutexName(); Log.LogMessage(MessageImportance.Normal, - $"TargetFrameworks: '{TargetFrameworks}'; using mutex {mutexName}"); + $"TargetFramework: '{TargetFramework}'; TargetFrameworks: '{TargetFrameworks}'; using mutex {mutexName}"); using var mutex = new Mutex(false, mutexName);