Parcourir la source

[cs] support calling Sys.command/Process with raw cmd string

Andy Li il y a 9 ans
Parent
commit
6ee8483802
3 fichiers modifiés avec 23 ajouts et 8 suppressions
  1. 1 1
      std/cs/_std/Sys.hx
  2. 21 6
      std/cs/_std/sys/io/Process.hx
  3. 1 1
      tests/sys/src/TestSys.hx

+ 1 - 1
std/cs/_std/Sys.hx

@@ -115,7 +115,7 @@ class Sys {
 
 	public static function command( cmd : String, ?args : Array<String> ) : Int
 	{
-		var proc:Process = new Process(cmd, args == null ? [] : args);
+		var proc:Process = new Process(cmd, args);
 		var ret = proc.exitCode();
 		proc.close();
 

+ 21 - 6
std/cs/_std/sys/io/Process.hx

@@ -39,14 +39,29 @@ class Process {
 	public function new( cmd : String, ?args : Array<String> ) : Void
 	{
 		this.native = new NativeProcess();
-		// mono 4.2.1 on Windows doesn't support relative path correctly
-		if (cmd.indexOf("/") != -1 || cmd.indexOf("\\") != -1)
-			cmd = sys.FileSystem.fullPath(cmd);
-		native.StartInfo.FileName = cmd;
 		native.StartInfo.CreateNoWindow = true;
-		native.StartInfo.Arguments = buildArgumentsString(args);
 		native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
-		native.StartInfo.UseShellExecute = false;
+		if (args != null) {
+			// mono 4.2.1 on Windows doesn't support relative path correctly
+			if (cmd.indexOf("/") != -1 || cmd.indexOf("\\") != -1)
+				cmd = sys.FileSystem.fullPath(cmd);
+			native.StartInfo.FileName = cmd;
+			native.StartInfo.UseShellExecute = false;
+			native.StartInfo.Arguments = buildArgumentsString(args);
+		} else {
+			switch (Sys.systemName()) {
+				case "Windows":
+					native.StartInfo.FileName = switch (Sys.getEnv("COMSPEC")) {
+						case null: "cmd.exe";
+						case comspec: comspec;
+					}
+					native.StartInfo.Arguments = '/C "$cmd"';
+				case _:
+					native.StartInfo.FileName = "/bin/sh";
+					native.StartInfo.Arguments = buildArgumentsString(["-c", cmd]);
+			}
+			native.StartInfo.UseShellExecute = false;
+		}
 
 		native.Start();
 

+ 1 - 1
tests/sys/src/TestSys.hx

@@ -29,7 +29,7 @@ class TestSys extends TestCommandBase {
 	}
 	#end
 
-	#if (neko || python || php || java)
+	#if (neko || python || php || java || cs)
 	function testRawCommand() {
 		var bin = sys.FileSystem.absolutePath(ExitCode.bin);
 		var native = sys.FileSystem.absolutePath(ExitCode.getNative());