From b6bbcdca750046e2735deb68e8a7643290c3857d Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Tue, 14 Feb 2023 11:53:47 +0545 Subject: [PATCH 1/7] fixed failing of testEcho() test in windows --- cli/src/main/java/dev/starfix/Starfix.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index b4f7397..224a280 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -300,7 +300,17 @@ public static String runCommand(Path directory, String... command) throws IOExce String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); } - return presult.outputUTF8(); + String result = presult.outputUTF8(); + + // for windows + if (isWindows()){ + if (result.contains("\r\n")){ + result = result.replaceAll("\r\n",""); + result = result+System.lineSeparator(); + } + } + + return result; } public static class CloneUrl{ From 010ffd4b922fa1f0f4db0df9e2bfc9ab3a2ce49b Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Thu, 16 Feb 2023 23:32:58 -0800 Subject: [PATCH 2/7] fixed testEcho() test failure in windows --- cli/src/main/java/dev/starfix/Starfix.java | 51 +++++++++++++--------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index 224a280..415ed80 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -15,6 +15,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.ExitCode; import picocli.CommandLine.Parameters; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.io.BufferedReader; @@ -285,32 +286,40 @@ public static void gitClone(Path directory, String originUrl) throws IOException public static String runCommand(Path directory, String... command) throws IOException, InterruptedException { // Function to Run Commands using Process Builder - ProcessResult presult; - try { + if (isWindows()) { System.out.println("Running " + String.join(" ", command)); - presult = new ProcessExecutor().command(command).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) - .execute(); - } catch (TimeoutException e) { - throw new RuntimeException("Error running command", e); - } - int exit = presult.getExitValue(); - if (exit!=0) { - throw new AssertionError( - String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); - } - - String result = presult.outputUTF8(); + final Process exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); + InputStream inputStream = exec.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String output = reader.readLine().replaceAll("\"", ""); + System.out.print(output+System.lineSeparator()); + + int exit = exec.waitFor(); + if (exit!=0) { + throw new AssertionError( + String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); + } + return output+System.lineSeparator(); + + } else{ + ProcessResult presult; + try { + System.out.println("Running " + String.join(" ", command)); + presult = new ProcessExecutor().command(command).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) + .execute(); + } catch (TimeoutException e) { + throw new RuntimeException("Error running command", e); + } - // for windows - if (isWindows()){ - if (result.contains("\r\n")){ - result = result.replaceAll("\r\n",""); - result = result+System.lineSeparator(); + int exit = presult.getExitValue(); + if (exit!=0) { + throw new AssertionError( + String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); } - } - return result; + return presult.outputUTF8(); + } } public static class CloneUrl{ From 47be33daeba8534ad5887243a4b1d35a64191d91 Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Thu, 16 Feb 2023 23:37:39 -0800 Subject: [PATCH 3/7] added try catch block. --- cli/src/main/java/dev/starfix/Starfix.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index 415ed80..728d78c 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -287,13 +287,17 @@ public static void gitClone(Path directory, String originUrl) throws IOException public static String runCommand(Path directory, String... command) throws IOException, InterruptedException { // Function to Run Commands using Process Builder if (isWindows()) { - System.out.println("Running " + String.join(" ", command)); + try{ + System.out.println("Running " + String.join(" ", command)); - final Process exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); - InputStream inputStream = exec.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String output = reader.readLine().replaceAll("\"", ""); - System.out.print(output+System.lineSeparator()); + final Process exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); + InputStream inputStream = exec.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String output = reader.readLine().replaceAll("\"", ""); + System.out.print(output+System.lineSeparator()); + } catch (TimeoutException e) { + throw new RuntimeException("Error running command", e); + } int exit = exec.waitFor(); if (exit!=0) { From 1a0eaa98165e3eb7d396744e242155e3cc504040 Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Thu, 16 Feb 2023 23:42:55 -0800 Subject: [PATCH 4/7] support to exceptions --- cli/src/main/java/dev/starfix/Starfix.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index 728d78c..529ed36 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -287,18 +287,20 @@ public static void gitClone(Path directory, String originUrl) throws IOException public static String runCommand(Path directory, String... command) throws IOException, InterruptedException { // Function to Run Commands using Process Builder if (isWindows()) { + final Process exec; + String output; try{ System.out.println("Running " + String.join(" ", command)); - final Process exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); + exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); InputStream inputStream = exec.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String output = reader.readLine().replaceAll("\"", ""); + output = reader.readLine().replaceAll("\"", ""); System.out.print(output+System.lineSeparator()); - } catch (TimeoutException e) { + } catch (Exception e) { throw new RuntimeException("Error running command", e); } - + int exit = exec.waitFor(); if (exit!=0) { throw new AssertionError( From d33d67179c6d224424a56c291b7e83355a89daf5 Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Tue, 21 Feb 2023 20:00:42 -0800 Subject: [PATCH 5/7] fixed --- cli/src/main/java/dev/starfix/Starfix.java | 32 ++++++++++------------ 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index 529ed36..a29a07d 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -286,30 +286,27 @@ public static void gitClone(Path directory, String originUrl) throws IOException public static String runCommand(Path directory, String... command) throws IOException, InterruptedException { // Function to Run Commands using Process Builder + ProcessResult presult; + if (isWindows()) { - final Process exec; - String output; try{ System.out.println("Running " + String.join(" ", command)); + presult = new ProcessExecutor().command("CMD", "/C", command[0], command[1]).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) + .execute(); + } catch (Exception e) { + throw new RuntimeException("Error running command", e); + } - exec = new ProcessBuilder("CMD", "/C", command[0], command[1]).start(); - InputStream inputStream = exec.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - output = reader.readLine().replaceAll("\"", ""); - System.out.print(output+System.lineSeparator()); - } catch (Exception e) { - throw new RuntimeException("Error running command", e); - } + int exit = presult.getExitValue(); + if (exit!=0) { + throw new AssertionError( + String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); + } - int exit = exec.waitFor(); - if (exit!=0) { - throw new AssertionError( - String.format("runCommand %s in %s returned %d", Arrays.toString(command), directory, exit)); - } - return output+System.lineSeparator(); + return presult.outputUTF8().replaceAll("\"",""); } else{ - ProcessResult presult; + try { System.out.println("Running " + String.join(" ", command)); presult = new ProcessExecutor().command(command).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) @@ -325,6 +322,7 @@ public static String runCommand(Path directory, String... command) throws IOExce } return presult.outputUTF8(); + } } From d8ba5fb48f11639d10f236d3109b56c210c688ae Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Tue, 21 Feb 2023 20:02:39 -0800 Subject: [PATCH 6/7] removed unused library --- cli/src/main/java/dev/starfix/Starfix.java | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index a29a07d..03c0910 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -15,7 +15,6 @@ import picocli.CommandLine.Command; import picocli.CommandLine.ExitCode; import picocli.CommandLine.Parameters; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.io.BufferedReader; From a4594bba211312c9a3ad566bdadf3ad406c21566 Mon Sep 17 00:00:00 2001 From: Kshitiz-Mhto Date: Wed, 22 Feb 2023 21:34:00 -0800 Subject: [PATCH 7/7] fix handling multiple arguments --- cli/src/main/java/dev/starfix/Starfix.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java index 03c0910..5b7c9e7 100644 --- a/cli/src/main/java/dev/starfix/Starfix.java +++ b/cli/src/main/java/dev/starfix/Starfix.java @@ -290,7 +290,11 @@ public static String runCommand(Path directory, String... command) throws IOExce if (isWindows()) { try{ System.out.println("Running " + String.join(" ", command)); - presult = new ProcessExecutor().command("CMD", "/C", command[0], command[1]).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) + String[] commandLineArgs = new String[2 + command.length]; + commandLineArgs[0] = "CMD"; + commandLineArgs[1] = "/C"; + System.arraycopy(command, 0, commandLineArgs, 2, command.length); + presult = new ProcessExecutor().command(commandLineArgs).redirectOutput(System.out).redirectErrorStream(true).readOutput(true) .execute(); } catch (Exception e) { throw new RuntimeException("Error running command", e);