Create ExecScriptHelper.java

This commit is contained in:
Christopher Schnick 2022-12-14 20:20:41 +01:00
parent da1fe7a511
commit 188051bfa5

View file

@ -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;
}
}