mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-11-25 00:50:31 +00:00
Create ExecScriptHelper.java
This commit is contained in:
parent
da1fe7a511
commit
188051bfa5
1 changed files with 40 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue