Code polish, added Shell convenience method

This commit is contained in:
nathan 2017-07-18 02:25:10 +02:00
parent 3ab5076d9b
commit 740087f2cf
2 changed files with 41 additions and 5 deletions

View File

@ -27,12 +27,29 @@ class ShellAsyncExecutor extends ShellExecutor {
*/
public static
boolean run(String executableName, String... args) {
ShellExecutor shell = new ShellExecutor();
ShellAsyncExecutor shell = new ShellAsyncExecutor();
shell.setExecutable(executableName);
shell.addArguments(args);
shell.createReadWriterThreads();
return shell.start(false) == 0;
return shell.start() == 0;
}
/**
* This is a convenience method to easily create a default process. Will immediately return, and does not wait for the process to finish
*
* @param executableName the name of the executable to run
* @param args the arguments for the executable
*
* @return true if the process ran successfully (exit value was 0), otherwise false
*/
public static
boolean runShell(String executableName, String... args) {
ShellAsyncExecutor shell = new ShellAsyncExecutor();
shell.setExecutable(executableName);
shell.addArguments(args);
shell.executeAsShellCommand();
return shell.start() == 0;
}
@Override

View File

@ -87,6 +87,25 @@ class ShellExecutor {
return shell.start() == 0;
}
/**
* This is a convenience method to easily create a default process. Will immediately return, and does not wait for the process to finish
*
* @param executableName the name of the executable to run
* @param args the arguments for the executable
*
* @return true if the process ran successfully (exit value was 0), otherwise false
*/
public static
boolean runShell(String executableName, String... args) {
ShellExecutor shell = new ShellExecutor();
shell.setExecutable(executableName);
shell.addArguments(args);
shell.executeAsShellCommand();
// blocks until finished
return shell.start() == 0;
}
/**
* This will cause the spawned process to pipe it's output to a String, so it can be retrieved.
*/
@ -164,8 +183,8 @@ class ShellExecutor {
}
public final
ShellExecutor addArguments(final String... paths) {
for (String path : paths) {
ShellExecutor addArguments(final String... args) {
for (String path : args) {
this.arguments.add(path);
}
return this;