Skip to content

Commit 6043b8b

Browse files
committed
fix work Zx on linux/mac env
1 parent da3ad69 commit 6043b8b

File tree

3 files changed

+98
-40
lines changed

3 files changed

+98
-40
lines changed

sandbox/ConsoleApp/Program.cs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,52 @@
88
using Zx;
99
using static Zx.Env;
1010

11-
var version = await "dotnet --version";
11+
await "dotnet add --help";
1212

13+
//// `await string` execute process like shell
14+
////await "cat package.json | grep name";
1315

14-
// `await string` execute process like shell
15-
//await "cat package.json | grep name";
16+
//var lst = await runl($"dotnet --list-sdks");
1617

17-
var lst = await runl($"dotnet --list-sdks");
18+
//// receive result msg of stdout
19+
//var branch = await "git branch --show-current";
20+
////await $"dep deploy --branch={branch}";
1821

19-
// receive result msg of stdout
20-
var branch = await "git branch --show-current";
21-
//await $"dep deploy --branch={branch}";
22+
//// parallel request (similar as Task.WhenAll)
23+
//await new[]
24+
//{
25+
// "echo 1",
26+
// "echo 2",
27+
// "echo 3",
28+
//};
2229

23-
// parallel request (similar as Task.WhenAll)
24-
await new[]
25-
{
26-
"echo 1",
27-
"echo 2",
28-
"echo 3",
29-
};
30+
//// you can also use cd(chdir)
31+
//await "cd ../../";
3032

31-
// you can also use cd(chdir)
32-
await "cd ../../";
33+
//// run with $"" automatically escaped and quoted
34+
//var dir = "foo/foo bar";
35+
//await run($"mkdir {dir}"); // mkdir "/foo/foo bar"
3336

34-
// run with $"" automatically escaped and quoted
35-
var dir = "foo/foo bar";
36-
await run($"mkdir {dir}"); // mkdir "/foo/foo bar"
37+
//// helper for Console.WriteLine and colorize
38+
//log("red log.", System.ConsoleColor.Red);
39+
//using (color(System.ConsoleColor.Blue))
40+
//{
41+
// log("blue log");
42+
// System.Console.WriteLine("also blue");
43+
// await run($"echo {"blue blue blue"}");
44+
//}
3745

38-
// helper for Console.WriteLine and colorize
39-
log("red log.", System.ConsoleColor.Red);
40-
using (color(System.ConsoleColor.Blue))
41-
{
42-
log("blue log");
43-
System.Console.WriteLine("also blue");
44-
await run($"echo {"blue blue blue"}");
45-
}
46+
//// helper for web request
47+
//var text = await fetchText("http://wttr.in");
48+
//log(text);
4649

47-
// helper for web request
48-
var text = await fetchText("http://wttr.in");
49-
log(text);
50+
//// helper for ReadLine(stdin)
51+
//var bear = await question("What kind of bear is best?");
52+
//log($"You answered: {bear}");
5053

51-
// helper for ReadLine(stdin)
52-
var bear = await question("What kind of bear is best?");
53-
log($"You answered: {bear}");
5454

5555

56-
57-
await ignore(run($"dotnet noinfo"));
56+
//await ignore(run($"dotnet noinfo"));
5857

5958

6059

src/ProcessX/Zx/Env.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ public static string shell
2626
}
2727
else
2828
{
29-
_shell = ProcessStartAsync("which bash", CancellationToken.None, forceSilcent: true).Result + " -c";
29+
if (Which.TryGetPath("bash", out var bashPath))
30+
{
31+
_shell = bashPath + " -c";
32+
}
33+
else
34+
{
35+
throw new InvalidOperationException("shell is not found in PATH, set Env.shell manually.");
36+
}
3037
}
3138
}
3239
return _shell;
@@ -210,11 +217,11 @@ public static IDisposable color(ConsoleColor color)
210217

211218
static async Task<(string StdOut, string StdError)> ProcessStartAsync(string command, CancellationToken cancellationToken, bool forceSilcent = false)
212219
{
213-
var cmd = shell + " " + command;
220+
var cmd = shell + " \"" + command + "\"";
214221
var sbOut = new StringBuilder();
215222
var sbError = new StringBuilder();
216223

217-
var (_, stdout, stderror) = ProcessX.GetDualAsyncEnumerable(cmd, workingDirectory, envVars);
224+
var (_, stdout, stderror) = Cysharp.Diagnostics.ProcessX.GetDualAsyncEnumerable(cmd, workingDirectory, envVars);
218225

219226
var runStdout = Task.Run(async () =>
220227
{
@@ -268,11 +275,11 @@ public static IDisposable color(ConsoleColor color)
268275

269276
static async Task<(string[] StdOut, string[] StdError)> ProcessStartListAsync(string command, CancellationToken cancellationToken, bool forceSilcent = false)
270277
{
271-
var cmd = shell + " " + command;
278+
var cmd = shell + " \"" + command + "\"";
272279
var sbOut = new List<string>();
273280
var sbError = new List<string>();
274281

275-
var (_, stdout, stderror) = ProcessX.GetDualAsyncEnumerable(cmd, workingDirectory, envVars);
282+
var (_, stdout, stderror) = Cysharp.Diagnostics.ProcessX.GetDualAsyncEnumerable(cmd, workingDirectory, envVars);
276283

277284
var runStdout = Task.Run(async () =>
278285
{

src/ProcessX/Zx/Which.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This class is borrowd from https://github.com/mayuki/Chell
2+
3+
using System;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
8+
namespace Zx
9+
{
10+
internal static class Which
11+
{
12+
public static bool TryGetPath(string commandName, out string matchedPath)
13+
{
14+
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
15+
var paths = (Environment.GetEnvironmentVariable("PATH") ?? string.Empty).Split(isWindows ? ';' : ':');
16+
var pathExts = Array.Empty<string>();
17+
18+
if (isWindows)
19+
{
20+
paths = paths.Prepend(Environment.CurrentDirectory).ToArray();
21+
pathExts = (Environment.GetEnvironmentVariable("PATHEXT") ?? ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC").Split(';');
22+
}
23+
24+
foreach (var path in paths)
25+
{
26+
// /path/to/foo.ext
27+
foreach (var ext in pathExts)
28+
{
29+
var fullPath = Path.Combine(path, $"{commandName}{ext}");
30+
if (File.Exists(fullPath))
31+
{
32+
matchedPath = fullPath;
33+
return true;
34+
}
35+
}
36+
37+
// /path/to/foo
38+
{
39+
var fullPath = Path.Combine(path, commandName);
40+
if (File.Exists(fullPath))
41+
{
42+
matchedPath = fullPath;
43+
return true;
44+
}
45+
}
46+
}
47+
48+
matchedPath = string.Empty;
49+
return false;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)