Skip to content

Commit

Permalink
Workaround for invoking csval on Linux/macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Mar 25, 2019
1 parent 8d276af commit 2c800a2
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/CSharpMinifierConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace CSharpMinifierConsole
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using CSharpMinifier;
using Microsoft.Extensions.FileSystemGlobbing;
using Mono.Options;
Expand Down Expand Up @@ -75,6 +76,14 @@ static int Wain(IEnumerable<string> args)

void DefaultCommand()
{
const string validatorExecutableName = "csval";

var validator = Lazy.Create(() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|| RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
? FindProgramPath(validatorExecutableName)
: validatorExecutableName);

foreach (var (_, source) in ReadSources(tail, globDir))
{
Minify(source, Console.Out);
Expand All @@ -99,7 +108,7 @@ void Minify(string source, TextWriter output)

bool Validate(Action<TextWriter> minificationAction)
{
var psi = new ProcessStartInfo("csval")
var psi = new ProcessStartInfo(validator.Value)
{
UseShellExecute = false,
CreateNoWindow = true,
Expand Down Expand Up @@ -134,6 +143,24 @@ bool Validate(Action<TextWriter> minificationAction)
}
}

static string FindProgramPath(string program)
{
var fileName = Path.GetFileName(program);

var paths =
from p in Environment.GetEnvironmentVariable("PATH")
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries)
select p.Length > 0 && p[0] == '~'
? Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.Personal), p.Substring(1))
: p
into p
select Path.Join(p, fileName) into p
where File.Exists(p)
select p;

return paths.FirstOrDefault() ?? program;
}

static void HelpCommand(IEnumerable<string> args)
{
switch (args.FirstOrDefault())
Expand Down

0 comments on commit 2c800a2

Please sign in to comment.