From 188051bfa59ba20b7a95480e49af001328c4943b Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Wed, 14 Dec 2022 20:20:41 +0100 Subject: [PATCH] Create ExecScriptHelper.java --- .../extension/util/ExecScriptHelper.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 extension/src/main/java/io/xpipe/extension/util/ExecScriptHelper.java diff --git a/extension/src/main/java/io/xpipe/extension/util/ExecScriptHelper.java b/extension/src/main/java/io/xpipe/extension/util/ExecScriptHelper.java new file mode 100644 index 000000000..011466f94 --- /dev/null +++ b/extension/src/main/java/io/xpipe/extension/util/ExecScriptHelper.java @@ -0,0 +1,40 @@ +package io.xpipe.extension.util; + +import io.xpipe.core.impl.FileNames; +import io.xpipe.core.process.ShellProcessControl; +import io.xpipe.core.util.XPipeTempDirectory; +import lombok.SneakyThrows; + +import java.util.Objects; + +public class ExecScriptHelper { + + public static int getConnectionHash(String command) { + return Objects.hash(command); + } + + @SneakyThrows + public static String createExecScript(ShellProcessControl processControl, String content) { + var fileName = "exec-" + getConnectionHash(content); + content = processControl.getShellType().createInitFileContent(content); + var temp = XPipeTempDirectory.get(processControl); + var file = FileNames.join( + temp, fileName + "." + processControl.getShellType().getScriptFileEnding()); + + if (processControl.executeBooleanSimpleCommand(processControl.getShellType().createFileExistsCommand(file))) { + return file; + } + + try (var c = processControl.command(processControl.getShellType() + .joinCommands( + processControl.getShellType().createFileWriteCommand(file), + processControl.getShellType().getMakeExecutableCommand(file))) + .start()) { + c.discardOut(); + c.discardErr(); + c.getStdin().write(content.getBytes(processControl.getCharset())); + c.closeStdin(); + } + return file; + } +}