Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now ...


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)">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now ...

<Output TaskParameter="GeneratedFiles"
ItemName="CompiledCssFiles" />
</CompileSass>
Expand Down
Loading