From c15c1204f03f0a20a639ea4ca2a884b2a930bb07 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Wed, 7 Dec 2022 00:13:16 +0100 Subject: [PATCH] Fixes for shells --- .../java/io/xpipe/beacon/BeaconProxyImpl.java | 2 ++ .../java/io/xpipe/core/process/OsType.java | 21 ++----------- .../io/xpipe/core/process/ProcessControl.java | 2 ++ .../core/process/ShellProcessControl.java | 31 ++++++++++--------- .../java/io/xpipe/core/process/ShellType.java | 20 ++++++++++-- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconProxyImpl.java b/beacon/src/main/java/io/xpipe/beacon/BeaconProxyImpl.java index 36df8b42..6e92eafd 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconProxyImpl.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconProxyImpl.java @@ -71,6 +71,7 @@ public class BeaconProxyImpl extends ProxyProvider { } @Override + @SuppressWarnings("unchecked") public T createRemoteReadConnection(DataSource source, ShellStore proxy) throws Exception { var downstream = downstreamTransform(source, proxy); @@ -99,6 +100,7 @@ public class BeaconProxyImpl extends ProxyProvider { } @Override + @SuppressWarnings("unchecked") public T createRemoteWriteConnection(DataSource source, WriteMode mode, ShellStore proxy) throws Exception { var downstream = downstreamTransform(source, proxy); diff --git a/core/src/main/java/io/xpipe/core/process/OsType.java b/core/src/main/java/io/xpipe/core/process/OsType.java index 8ca26305..71efc63c 100644 --- a/core/src/main/java/io/xpipe/core/process/OsType.java +++ b/core/src/main/java/io/xpipe/core/process/OsType.java @@ -11,8 +11,6 @@ public interface OsType { Linux LINUX = new Linux(); Mac MAC = new Mac(); - String getScriptFileEnding(); - String getName(); String getTempDirectory(ShellProcessControl pc) throws Exception; @@ -42,11 +40,6 @@ public interface OsType { static class Windows implements OsType { - @Override - public String getScriptFileEnding() { - return "bat"; - } - @Override public String getName() { return "Windows"; @@ -54,7 +47,7 @@ public interface OsType { @Override public String getTempDirectory(ShellProcessControl pc) throws Exception { - return pc.executeSimpleCommand(pc.getShellType().getPrintVariableCommand("TEMP")); + return pc.executeSimpleCommand(ShellTypes.CMD, pc.getShellType().getPrintVariableCommand("TEMP")); } @Override @@ -65,7 +58,7 @@ public interface OsType { @Override public Map getProperties(ShellProcessControl pc) throws Exception { try (CommandProcessControl c = - pc.shell(ShellTypes.CMD).command("systeminfo").start()) { + pc.subShell(ShellTypes.CMD).command("systeminfo").start()) { var text = c.readOrThrow(); return PropertiesFormatsParser.parse(text, ":"); } @@ -105,11 +98,6 @@ public interface OsType { return String.join("/", file.split("[\\\\/]+")); } - @Override - public String getScriptFileEnding() { - return "sh"; - } - @Override public String getName() { return "Linux"; @@ -180,11 +168,6 @@ public interface OsType { return String.join("/", file.split("[\\\\/]+")); } - @Override - public String getScriptFileEnding() { - return "sh"; - } - @Override public String getName() { return "Mac"; diff --git a/core/src/main/java/io/xpipe/core/process/ProcessControl.java b/core/src/main/java/io/xpipe/core/process/ProcessControl.java index 570e3240..3601eef8 100644 --- a/core/src/main/java/io/xpipe/core/process/ProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ProcessControl.java @@ -8,6 +8,8 @@ import java.nio.charset.Charset; public interface ProcessControl extends Closeable, AutoCloseable { + String prepareConsoleOpen(boolean keepOpen) throws Exception; + void closeStdin() throws IOException; boolean isStdinClosed(); diff --git a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java index 0bc2ec2a..5640653f 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -11,11 +11,11 @@ import java.util.stream.Collectors; public interface ShellProcessControl extends ProcessControl { - default String prepareOpen() throws Exception { - return prepareOpen(null); + default String prepareConsoleOpen(boolean keepOpen) throws Exception { + return prepareConsoleOpen(null, keepOpen); } - String prepareOpen(String content) throws Exception; + String prepareConsoleOpen(String content, boolean keepOpen) throws Exception; default String executeSimpleCommand(String command) throws Exception { try (CommandProcessControl c = command(command).start()) { @@ -33,10 +33,7 @@ public interface ShellProcessControl extends ProcessControl { return executeSimpleCommand(type.switchTo(command)); } - default void restart() throws Exception { - exitAndWait(); - start(); - } + void restart() throws Exception; boolean isLocal(); @@ -52,24 +49,30 @@ public interface ShellProcessControl extends ProcessControl { SecretValue getElevationPassword(); - default ShellProcessControl shell(@NonNull ShellType type) { - return shell(type.openCommand()).elevation(getElevationPassword()); + default ShellProcessControl subShell(@NonNull ShellType type) { + return subShell(type.openCommand()).elevation(getElevationPassword()); } default CommandProcessControl command(@NonNull ShellType type, String command) { return command(type.switchTo(command)); } - default ShellProcessControl shell(@NonNull List command) { - return shell( + default ShellProcessControl subShell(@NonNull List command) { + return subShell( command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" "))); } - default ShellProcessControl shell(@NonNull String command) { - return shell(processControl -> command); + default ShellProcessControl subShell(@NonNull String command) { + return subShell(processControl -> command); } - ShellProcessControl shell(@NonNull Function command); + ShellProcessControl subShell(@NonNull Function command); + + default ShellProcessControl consoleCommand(@NonNull String command) { + return consoleCommand(shellProcessControl -> command); + } + + ShellProcessControl consoleCommand(@NonNull Function command); void executeCommand(String command) throws Exception; diff --git a/core/src/main/java/io/xpipe/core/process/ShellType.java b/core/src/main/java/io/xpipe/core/process/ShellType.java index ae80fcc3..c5cabb0b 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellType.java +++ b/core/src/main/java/io/xpipe/core/process/ShellType.java @@ -10,12 +10,20 @@ import java.util.stream.Collectors; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") public interface ShellType { + String getScriptFileEnding(); + + default String commandWithVariable(String key, String value, String command) { + return joinCommands(getSetVariableCommand(key, value), command); + } + + String getPauseCommand(); + String createInitFileContent(String command); - List getOpenWithInitFileCommand(String file); + String getOpenWithInitFileCommand(String file); default String flatten(List command) { - return command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")); + return command.stream().map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")); } @@ -41,6 +49,14 @@ public interface ShellType { return "&&"; } + default String getOrConcatenationOperator() { + return "||"; + } + + String getMakeExecutableCommand(String file); + + String elevateConsoleCommand(ShellProcessControl control, String command); + String getEchoCommand(String s, boolean toErrorStream); String queryShellProcessId(ShellProcessControl control) throws Exception;