|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace AspNetCore.SassCompiler; |
| 11 | + |
| 12 | +public interface ISassCompiler |
| 13 | +{ |
| 14 | + Task CompileAsync(string[] args); |
| 15 | + |
| 16 | + Task<Stream> CompileAsync(Stream input, string[] args); |
| 17 | + |
| 18 | + Task CompileAsync(Stream input, Stream output, string[] args); |
| 19 | + |
| 20 | + Task<string> CompileToStringAsync(Stream input, string[] args); |
| 21 | +} |
| 22 | + |
| 23 | +internal class SassCompiler : ISassCompiler |
| 24 | +{ |
| 25 | + public async Task CompileAsync(string[] args) |
| 26 | + { |
| 27 | + var escapedArgs = args.Length > 0 ? '"' + string.Join("\" \"", args) + '"' : ""; |
| 28 | + ValidateArgs(escapedArgs); |
| 29 | + var process = CreateSassProcess(escapedArgs); |
| 30 | + if (process == null) |
| 31 | + throw new SassCompilerException("Sass executable not found"); |
| 32 | + |
| 33 | + var errorOutput = new MemoryStream(); |
| 34 | + |
| 35 | + process.Start(); |
| 36 | + ChildProcessTracker.AddProcess(process); |
| 37 | + await process.StandardOutput.BaseStream.CopyToAsync(Stream.Null); |
| 38 | + await process.StandardError.BaseStream.CopyToAsync(errorOutput); |
| 39 | + await process.WaitForExitAsync(); |
| 40 | + |
| 41 | + if (process.ExitCode != 0) |
| 42 | + { |
| 43 | + var errorOutputText = Encoding.UTF8.GetString(errorOutput.ToArray()); |
| 44 | + throw new SassCompilerException($"Sass process exited with non-zero exit code: {process.ExitCode}.", errorOutputText); |
| 45 | + } |
| 46 | + |
| 47 | + process.Dispose(); |
| 48 | + } |
| 49 | + |
| 50 | + public async Task<Stream> CompileAsync(Stream input, string[] args) |
| 51 | + { |
| 52 | + var output = new MemoryStream(); |
| 53 | + await CompileAsync(input, output, args); |
| 54 | + |
| 55 | + output.Position = 0; |
| 56 | + return output; |
| 57 | + } |
| 58 | + |
| 59 | + public async Task CompileAsync(Stream input, Stream output, string[] args) |
| 60 | + { |
| 61 | + var escapedArgs = args.Length > 0 ? '"' + string.Join("\" \"", args) + '"' : ""; |
| 62 | + ValidateArgs(escapedArgs); |
| 63 | + var process = CreateSassProcess(escapedArgs + " --stdin"); |
| 64 | + if (process == null) |
| 65 | + throw new SassCompilerException("Sass executable not found"); |
| 66 | + |
| 67 | + process.StartInfo.RedirectStandardInput = true; |
| 68 | + |
| 69 | + var errorOutput = new MemoryStream(); |
| 70 | + |
| 71 | + process.Start(); |
| 72 | + ChildProcessTracker.AddProcess(process); |
| 73 | + await input.CopyToAsync(process.StandardInput.BaseStream); |
| 74 | + await process.StandardInput.DisposeAsync(); |
| 75 | + await process.StandardOutput.BaseStream.CopyToAsync(output); |
| 76 | + await process.StandardError.BaseStream.CopyToAsync(errorOutput); |
| 77 | + await process.WaitForExitAsync(); |
| 78 | + |
| 79 | + if (process.ExitCode != 0) |
| 80 | + { |
| 81 | + var errorOutputText = Encoding.UTF8.GetString(errorOutput.ToArray()); |
| 82 | + throw new SassCompilerException($"Sass process exited with non-zero exit code: {process.ExitCode}.", errorOutputText); |
| 83 | + } |
| 84 | + |
| 85 | + process.Dispose(); |
| 86 | + } |
| 87 | + |
| 88 | + public async Task<string> CompileToStringAsync(Stream input, string[] args) |
| 89 | + { |
| 90 | + using var output = new MemoryStream(); |
| 91 | + await CompileAsync(input, output, args); |
| 92 | + return Encoding.UTF8.GetString(output.ToArray()); |
| 93 | + } |
| 94 | + |
| 95 | + private static void ValidateArgs(string args) |
| 96 | + { |
| 97 | + if (args.Contains("--watch")) |
| 98 | + throw new SassCompilerException("The sass --watch option is not supported."); |
| 99 | + |
| 100 | + if (args.Contains("--interactive")) |
| 101 | + throw new SassCompilerException("The sass --interactive option is not supported."); |
| 102 | + } |
| 103 | + |
| 104 | + internal static Process CreateSassProcess(string arguments) |
| 105 | + { |
| 106 | + var command = GetSassCommand(); |
| 107 | + if (command.Filename == null) |
| 108 | + return null; |
| 109 | + |
| 110 | + if (!string.IsNullOrEmpty(command.Snapshot)) |
| 111 | + arguments = $"{command.Snapshot} {arguments}"; |
| 112 | + |
| 113 | + var process = new Process(); |
| 114 | + process.StartInfo.FileName = command.Filename; |
| 115 | + process.StartInfo.Arguments = arguments; |
| 116 | + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
| 117 | + process.StartInfo.CreateNoWindow = true; |
| 118 | + process.StartInfo.UseShellExecute = false; |
| 119 | + process.StartInfo.RedirectStandardOutput = true; |
| 120 | + process.StartInfo.RedirectStandardError = true; |
| 121 | + process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); |
| 122 | + |
| 123 | + return process; |
| 124 | + } |
| 125 | + |
| 126 | + internal static (string Filename, string Snapshot) GetSassCommand() |
| 127 | + { |
| 128 | + var attribute = Assembly.GetEntryAssembly()?.GetCustomAttributes<SassCompilerAttribute>().FirstOrDefault(); |
| 129 | + |
| 130 | + if (attribute != null) |
| 131 | + return (attribute.SassBinary, string.IsNullOrWhiteSpace(attribute.SassSnapshot) ? "" : $"\"{attribute.SassSnapshot}\""); |
| 132 | + |
| 133 | + var assemblyLocation = typeof(SassCompilerHostedService).Assembly.Location; |
| 134 | + |
| 135 | + var (exePath, snapshotPath) = GetExeAndSnapshotPath(); |
| 136 | + if (exePath == null) |
| 137 | + return (null, null); |
| 138 | + |
| 139 | + var directory = Path.GetDirectoryName(assemblyLocation); |
| 140 | + while (!string.IsNullOrEmpty(directory) && directory != "/") |
| 141 | + { |
| 142 | + if (File.Exists(Path.Join(directory, exePath))) |
| 143 | + return (Path.Join(directory, exePath), snapshotPath == null ? null : "\"" + Path.Join(directory, snapshotPath) + "\""); |
| 144 | + |
| 145 | + directory = Path.GetDirectoryName(directory); |
| 146 | + } |
| 147 | + |
| 148 | + return (null, null); |
| 149 | + } |
| 150 | + |
| 151 | + internal static (string ExePath, string SnapshotPath) GetExeAndSnapshotPath() |
| 152 | + { |
| 153 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 154 | + { |
| 155 | + return RuntimeInformation.OSArchitecture switch |
| 156 | + { |
| 157 | + Architecture.X64 => ("runtimes\\win-x64\\src\\dart.exe", "runtimes\\win-x64\\src\\sass.snapshot"), |
| 158 | + Architecture.Arm64 => ("runtimes\\win-x64\\src\\dart.exe", "runtimes\\win-x64\\src\\sass.snapshot"), |
| 159 | + _ => (null, null), |
| 160 | + }; |
| 161 | + } |
| 162 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 163 | + { |
| 164 | + return RuntimeInformation.OSArchitecture switch |
| 165 | + { |
| 166 | + Architecture.X64 => ("runtimes/linux-x64/src/dart", "runtimes/linux-x64/src/sass.snapshot"), |
| 167 | + Architecture.Arm64 => ("runtimes/linux-arm64/src/dart", "runtimes/linux-x64/src/sass.snapshot"), |
| 168 | + _ => (null, null), |
| 169 | + }; |
| 170 | + } |
| 171 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 172 | + { |
| 173 | + return RuntimeInformation.OSArchitecture switch |
| 174 | + { |
| 175 | + Architecture.X64 => ("runtimes/osx-x64/src/dart", "runtimes/osx-x64/src/sass.snapshot"), |
| 176 | + Architecture.Arm64 => ("runtimes/osx-arm64/src/dart", "runtimes/osx-arm64/src/sass.snapshot"), |
| 177 | + _ => (null, null), |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + return (null, null); |
| 182 | + } |
| 183 | +} |
0 commit comments