diff --git a/core/src/main/java/io/xpipe/core/process/CommandProcessControl.java b/core/src/main/java/io/xpipe/core/process/CommandProcessControl.java index 9c5e8c38..0fa32e01 100644 --- a/core/src/main/java/io/xpipe/core/process/CommandProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/CommandProcessControl.java @@ -58,7 +58,6 @@ public interface CommandProcessControl extends ProcessControl { @Override CommandProcessControl start() throws Exception; - @Override CommandProcessControl exitTimeout(Integer timeout); String readOnlyStdout() throws Exception; 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 9261951b..c814645f 100644 --- a/core/src/main/java/io/xpipe/core/process/ProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ProcessControl.java @@ -24,8 +24,6 @@ public interface ProcessControl extends Closeable, AutoCloseable { void close() throws IOException; void kill() throws Exception; - ProcessControl exitTimeout(Integer timeout); - ProcessControl start() throws Exception; InputStream getStdout(); 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 bf3a7f8e..03b2b04e 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -35,12 +35,6 @@ public interface ShellProcessControl extends ProcessControl { } } - default void executeSimpleCommand(List command) throws Exception { - try (CommandProcessControl c = command(command).start()) { - c.discardOrThrow(); - } - } - default String executeStringSimpleCommand(ShellType type, String command) throws Exception { try (var sub = subShell(type).start()) { return sub.executeStringSimpleCommand(command); diff --git a/core/src/main/java/io/xpipe/core/process/ShellTypes.java b/core/src/main/java/io/xpipe/core/process/ShellTypes.java index 0eaa788f..d634a157 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellTypes.java +++ b/core/src/main/java/io/xpipe/core/process/ShellTypes.java @@ -355,19 +355,23 @@ public class ShellTypes { @Override public String createWhichCommand(String executable) { - return "cmd /C where \"" + executable + "\""; + return "$LASTEXITCODE=(1 - (Get-Command -erroraction \"silentlycontinue\" \"" + executable + "\").Length)"; } @Override public Charset determineCharset(ShellProcessControl control) throws Exception { - control.writeLine("chcp"); - var r = new BufferedReader(new InputStreamReader(control.getStdout(), StandardCharsets.US_ASCII)); + control.writeLine("If (Get-Command -erroraction 'silentlycontinue' chcp) {chcp} Else {echo \"Not Windows\"}"); + // Read echo of command r.readLine(); // Read actual output var line = r.readLine(); + if (line.equals("Not Windows")) { + return StandardCharsets.UTF_8; + } + var matcher = Pattern.compile("\\d+").matcher(line); matcher.find(); return Charset.forName("ibm" + matcher.group()); diff --git a/core/src/main/java/io/xpipe/core/util/SimpleProxyFunction.java b/core/src/main/java/io/xpipe/core/util/SimpleProxyFunction.java index cca847c5..c9877afb 100644 --- a/core/src/main/java/io/xpipe/core/util/SimpleProxyFunction.java +++ b/core/src/main/java/io/xpipe/core/util/SimpleProxyFunction.java @@ -5,6 +5,7 @@ import lombok.SneakyThrows; public abstract class SimpleProxyFunction extends ProxyFunction { @SneakyThrows + @SuppressWarnings("unchecked") public T getResult() { var fields = getClass().getDeclaredFields(); var last = fields[fields.length - 1]; @@ -13,6 +14,7 @@ public abstract class SimpleProxyFunction extends ProxyFunction { } @SneakyThrows + @SuppressWarnings("unchecked") public T callAndGet() { var result = callAndCopy(); return ((SimpleProxyFunction) result).getResult(); diff --git a/extension/src/main/java/io/xpipe/extension/util/ApplicationHelper.java b/extension/src/main/java/io/xpipe/extension/util/ApplicationHelper.java new file mode 100644 index 00000000..f15d89f0 --- /dev/null +++ b/extension/src/main/java/io/xpipe/extension/util/ApplicationHelper.java @@ -0,0 +1,18 @@ +package io.xpipe.extension.util; + +import io.xpipe.core.process.ShellProcessControl; + +import java.io.IOException; + +public class ApplicationHelper { + + public static boolean isInPath(ShellProcessControl processControl, String executable) throws Exception { + return processControl.executeBooleanSimpleCommand(processControl.getShellType().createWhichCommand(executable)); + } + + public static void checkSupport(ShellProcessControl processControl, String executable, String displayName) throws Exception { + if (!isInPath(processControl, executable)) { + throw new IOException(displayName + " executable " + executable + " not found in PATH"); + } + } +} diff --git a/extension/src/main/java/io/xpipe/extension/util/ExpectHelper.java b/extension/src/main/java/io/xpipe/extension/util/ExpectHelper.java deleted file mode 100644 index ec94d7af..00000000 --- a/extension/src/main/java/io/xpipe/extension/util/ExpectHelper.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.xpipe.extension.util; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.List; -import java.util.stream.Collectors; - -public class ExpectHelper { - - private static Path extractedExecutable = null; - - private static Path getExtractedExecutable() { - if (extractedExecutable == null) { - XPipeDaemon.getInstance().withResource("io.xpipe.extension", "bin/expect.exe", path -> { - extractedExecutable = Files.createTempFile(null, ".exe"); - Files.copy(path, extractedExecutable, StandardCopyOption.REPLACE_EXISTING); - }); - } - - return extractedExecutable; - } - - public static List executeExpect(List command, String password) throws IOException { - var file = Files.createTempFile(null, ".lua"); - Files.writeString(file, expectFile(command, password)); - return List.of(getExtractedExecutable().toString(), file.toString()); - } - - private static String expectFile(List command, String password) { - return String.format(""" - echo(false) - if spawn(%s) then - expect(":") - sendln("%s") - echo(true) - end - """, command.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")), password); - } -}