More fixes for shells and prefs

This commit is contained in:
Christopher Schnick
2022-12-09 01:44:12 +01:00
parent 1982661aa1
commit b3cfae5a92
15 changed files with 119 additions and 43 deletions

View File

@@ -6,6 +6,10 @@ import java.util.ServiceLoader;
public abstract class LocalProcessControlProvider {
public static LocalProcessControlProvider get() {
return INSTANCE;
}
private static LocalProcessControlProvider INSTANCE;
public static void init(ModuleLayer layer) {
@@ -23,4 +27,6 @@ public abstract class LocalProcessControlProvider {
}
public abstract ShellProcessControl createProcessControl();
public abstract void openInTerminal(String title, String command) throws Exception;
}

View File

@@ -47,7 +47,7 @@ public interface OsType {
@Override
public String getTempDirectory(ShellProcessControl pc) throws Exception {
return pc.executeSimpleCommand(ShellTypes.CMD, ShellTypes.CMD.getPrintVariableCommand("TEMP"));
return pc.executeStringSimpleCommand(ShellTypes.CMD, ShellTypes.CMD.getPrintVariableCommand("TEMP"));
}
@Override
@@ -160,7 +160,7 @@ public interface OsType {
@Override
public String getTempDirectory(ShellProcessControl pc) throws Exception {
return pc.executeSimpleCommand(pc.getShellType().getPrintVariableCommand("TEMP"));
return pc.executeStringSimpleCommand(pc.getShellType().getPrintVariableCommand("TEMP"));
}
@Override

View File

@@ -8,7 +8,7 @@ import java.nio.charset.Charset;
public interface ProcessControl extends Closeable, AutoCloseable {
String prepareConsoleOpen(boolean keepOpen) throws Exception;
String prepareTerminalOpen() throws Exception;
void closeStdin() throws IOException;

View File

@@ -5,18 +5,19 @@ import lombok.NonNull;
import java.io.IOException;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
public interface ShellProcessControl extends ProcessControl {
default String prepareConsoleOpen(boolean keepOpen) throws Exception {
return prepareConsoleOpen(null, keepOpen);
default String prepareTerminalOpen() throws Exception {
return prepareTerminalOpen(null);
}
String prepareConsoleOpen(String content, boolean keepOpen) throws Exception;
String prepareTerminalOpen(String content) throws Exception;
default String executeSimpleCommand(String command) throws Exception {
default String executeStringSimpleCommand(String command) throws Exception {
try (CommandProcessControl c = command(command).start()) {
return c.readOrThrow();
}
@@ -28,9 +29,21 @@ public interface ShellProcessControl extends ProcessControl {
}
}
default String executeSimpleCommand(ShellType type, String command) throws Exception {
default void executeSimpleCommand(String command) throws Exception {
try (CommandProcessControl c = command(command).start()) {
c.discardOrThrow();
}
}
default void executeSimpleCommand(List<String> 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.executeSimpleCommand(command);
return sub.executeStringSimpleCommand(command);
}
}
@@ -65,10 +78,10 @@ public interface ShellProcessControl extends ProcessControl {
ShellProcessControl subShell(@NonNull Function<ShellProcessControl, String> command);
default ShellProcessControl consoleCommand(@NonNull String command) {
return consoleCommand(shellProcessControl -> command);
return consoleCommand((shellProcessControl, c) -> command);
}
ShellProcessControl consoleCommand(@NonNull Function<ShellProcessControl, String> command);
ShellProcessControl consoleCommand(@NonNull BiFunction<ShellProcessControl, String, String> command);
void executeCommand(String command) throws Exception;

View File

@@ -54,8 +54,6 @@ public interface ShellType {
String getMakeExecutableCommand(String file);
String elevateConsoleCommand(ShellProcessControl control, String command);
default String getScriptEchoCommand(String s) {
return getEchoCommand(s, false);
}
@@ -82,8 +80,12 @@ public interface ShellType {
String createFileWriteCommand(String file);
String createFileDeleteCommand(String file);
String createFileExistsCommand(String file);
String createWhichCommand(String executable);
Charset determineCharset(ShellProcessControl control) throws Exception;
NewLine getNewLine();

View File

@@ -52,7 +52,7 @@ public class XPipeInstallation {
public static String getDataBasePath(ShellProcessControl p) throws Exception {
if (p.getOsType().equals(OsType.WINDOWS)) {
var base = p.executeSimpleCommand(p.getShellType().getPrintVariableCommand("userprofile"));
var base = p.executeStringSimpleCommand(p.getShellType().getPrintVariableCommand("userprofile"));
return FileNames.join(base, ".xpipe");
} else {
return FileNames.join("~", ".xpipe");
@@ -66,14 +66,14 @@ public class XPipeInstallation {
}
public static String getDefaultInstallationBasePath(ShellProcessControl p) throws Exception {
var customHome = p.executeSimpleCommand(p.getShellType().getPrintVariableCommand("XPIPE_HOME"));
var customHome = p.executeStringSimpleCommand(p.getShellType().getPrintVariableCommand("XPIPE_HOME"));
if (!customHome.isEmpty()) {
return customHome;
}
String path = null;
if (p.getOsType().equals(OsType.WINDOWS)) {
var base = p.executeSimpleCommand(p.getShellType().getPrintVariableCommand("LOCALAPPDATA"));
var base = p.executeStringSimpleCommand(p.getShellType().getPrintVariableCommand("LOCALAPPDATA"));
path = FileNames.join(base, "X-Pipe");
} else {
path = "/opt/xpipe";

View File

@@ -0,0 +1,34 @@
package io.xpipe.core.util;
import io.xpipe.core.impl.FileNames;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.store.ShellStore;
import java.io.IOException;
import java.nio.file.Path;
public class XPipeTempDirectory {
public static Path getLocal() throws Exception {
try (var pc = ShellStore.local().create().start()) {
return Path.of(get(pc));
}
}
public static String get(ShellProcessControl proc) throws Exception {
var base = proc.getOsType().getTempDirectory(proc);
var dir = FileNames.join(base, "xpipe");
if (!proc.executeBooleanSimpleCommand(proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(dir))) ){
throw new IOException("Unable to access or create temporary directory " + dir);
}
return dir;
}
public static void clear(ShellProcessControl proc) throws Exception {
var dir = get(proc);
if (!proc.executeBooleanSimpleCommand(proc.getShellType().createFileDeleteCommand(dir)) ){
throw new IOException("Unable to delete temporary directory " + dir);
}
}
}