Restructure proc module

This commit is contained in:
crschnick
2023-02-08 21:34:19 +00:00
parent d5c99ba49f
commit 51bbf8ad67
26 changed files with 159 additions and 159 deletions

View File

@@ -112,9 +112,7 @@ public abstract class Charsetter {
if (store instanceof FileStore fileStore && fileStore.getFileSystem() instanceof MachineStore m) {
if (result.getNewLine() == null) {
try (var pc = m.create().start()) {
result = new Result(result.getCharset(), pc.getShellType().getNewLine());
}
result = new Result(result.getCharset(), m.getShellType() != null ? m.getShellType().getNewLine() : null);
}
}

View File

@@ -1,6 +1,7 @@
package io.xpipe.core.impl;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.xpipe.core.process.ProcessControlProvider;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.store.FileSystemStore;
import io.xpipe.core.store.MachineStore;
@@ -47,7 +48,7 @@ public class LocalStore extends JacksonizedValue implements FileSystemStore, Mac
}
@Override
public ShellProcessControl create() {
public ShellProcessControl createControl() {
return ProcessControlProvider.createLocal();
}

View File

@@ -1,50 +0,0 @@
package io.xpipe.core.impl;
import io.xpipe.core.process.CommandProcessControl;
import io.xpipe.core.process.ShellProcessControl;
import lombok.NonNull;
import java.util.List;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class ProcessControlProvider {
private static List<ProcessControlProvider> INSTANCES;
public static void init(ModuleLayer layer) {
INSTANCES = ServiceLoader.load(layer, ProcessControlProvider.class)
.stream().map(localProcessControlProviderProvider -> localProcessControlProviderProvider.get()).toList();
}
public static ShellProcessControl createLocal() {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.createLocalProcessControl()).findFirst().orElseThrow();
}
public static ShellProcessControl createSub(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> commandFunction,
BiFunction<ShellProcessControl, String, String> terminalCommand) {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.sub(parent, commandFunction, terminalCommand)).findFirst().orElseThrow();
}
public static CommandProcessControl createCommand(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> command,
Function<ShellProcessControl, String> terminalCommand) {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.command(parent, command, terminalCommand)).findFirst().orElseThrow();
}
public abstract ShellProcessControl sub(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> commandFunction,
BiFunction<ShellProcessControl, String, String> terminalCommand);
public abstract CommandProcessControl command(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> command,
Function<ShellProcessControl, String> terminalCommand);
public abstract ShellProcessControl createLocalProcessControl();
}

View File

@@ -7,12 +7,12 @@ public interface OsType {
Windows WINDOWS = new Windows();
Linux LINUX = new Linux();
Mac MAC = new Mac();
MacOs MACOS = new MacOs();
public static OsType getLocal() {
String osName = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if ((osName.contains("mac")) || (osName.contains("darwin"))) {
return MAC;
return MACOS;
} else if (osName.contains("win")) {
return WINDOWS;
} else if (osName.contains("nux")) {
@@ -124,7 +124,7 @@ public interface OsType {
}
}
static class Mac implements OsType {
static class MacOs implements OsType {
@Override
public String getTempDirectory(ShellProcessControl pc) throws Exception {

View File

@@ -1,6 +1,58 @@
package io.xpipe.core.process;
import lombok.NonNull;
import java.util.List;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.function.Function;
public abstract class ProcessControlProvider {
public abstract ProcessControl local();
private static List<ProcessControlProvider> INSTANCES;
public static void init(ModuleLayer layer) {
INSTANCES = ServiceLoader.load(layer, ProcessControlProvider.class)
.stream().map(localProcessControlProviderProvider -> localProcessControlProviderProvider.get()).toList();
}
public static ShellProcessControl createLocal() {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.createLocalProcessControl()).findFirst().orElseThrow();
}
public static ShellProcessControl createSub(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> commandFunction,
BiFunction<ShellProcessControl, String, String> terminalCommand) {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.sub(parent, commandFunction, terminalCommand)).filter(
Objects::nonNull).findFirst().orElseThrow();
}
public static CommandProcessControl createCommand(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> command,
Function<ShellProcessControl, String> terminalCommand) {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.command(parent, command, terminalCommand)).filter(
Objects::nonNull).findFirst().orElseThrow();
}
public static ShellProcessControl createSsh(Object sshStore) {
return INSTANCES.stream().map(localProcessControlProvider -> localProcessControlProvider.createSshControl(sshStore)).filter(
Objects::nonNull).findFirst().orElseThrow();
}
public abstract ShellProcessControl sub(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> commandFunction,
BiFunction<ShellProcessControl, String, String> terminalCommand);
public abstract CommandProcessControl command(
ShellProcessControl parent,
@NonNull Function<ShellProcessControl, String> command,
Function<ShellProcessControl, String> terminalCommand);
public abstract ShellProcessControl createLocalProcessControl();
public abstract ShellProcessControl createSshControl(Object sshStore);
}

View File

@@ -6,11 +6,14 @@ import lombok.NonNull;
import java.io.IOException;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public interface ShellProcessControl extends ProcessControl {
void onInit(Consumer<ShellProcessControl> pc);
String prepareTerminalOpen() throws Exception;
String prepareIntermediateTerminalOpen(String content) throws Exception;

View File

@@ -2,10 +2,14 @@ package io.xpipe.core.store;
import io.xpipe.core.charsetter.Charsetter;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.process.ShellType;
public interface ShellStore extends DataStore {
import java.nio.charset.Charset;
public interface ShellStore extends DataStore, StatefulDataStore
{
public static MachineStore local() {
return new LocalStore();
@@ -21,7 +25,28 @@ public interface ShellStore extends DataStore {
return s instanceof LocalStore;
}
ShellProcessControl create();
default ShellProcessControl create() {
var pc = createControl();
pc.onInit(processControl -> {
setState("type", processControl.getShellType());
setState("os", processControl.getOsType());
setState("charset", processControl.getCharset());
});
return pc;
}
default ShellType getShellType() {
return getState("type", ShellType.class, null);
}
default OsType getOsType() {
return getState("os", OsType.class, null);
}
default Charset getCharset() {
return getState("charset", Charset.class, null);
}
ShellProcessControl createControl();
public default ShellType determineType() throws Exception {
try (var pc = create().start()) {

View File

@@ -20,7 +20,7 @@ public class XPipeInstallation {
if (OsType.getLocal().equals(OsType.LINUX)) {
return "nohup \"" + installationBase + "/app/bin/xpiped\" --mode " + mode.getDisplayName() + suffix
+ " & disown";
} else if (OsType.getLocal().equals(OsType.MAC)) {
} else if (OsType.getLocal().equals(OsType.MACOS)) {
return "open \"" + installationBase + "\" --args --mode " + mode.getDisplayName() + suffix;
}
@@ -55,7 +55,7 @@ public class XPipeInstallation {
public static boolean isInstallationDistribution() {
var base = getLocalInstallationBasePath();
if (OsType.getLocal().equals(OsType.MAC)) {
if (OsType.getLocal().equals(OsType.MACOS)) {
if (!base.toString().equals(getLocalDefaultInstallationBasePath(false))) {
return false;
}
@@ -93,13 +93,13 @@ public class XPipeInstallation {
public static Path getLocalExtensionsDirectory() {
Path path = getLocalInstallationBasePath();
return OsType.getLocal().equals(OsType.MAC)
return OsType.getLocal().equals(OsType.MACOS)
? path.resolve("Contents").resolve("Resources").resolve("extensions")
: path.resolve("app").resolve("extensions");
}
private static Path getLocalInstallationBasePathForJavaExecutable(Path executable) {
if (OsType.getLocal().equals(OsType.MAC)) {
if (OsType.getLocal().equals(OsType.MACOS)) {
return executable
.getParent()
.getParent()
@@ -115,7 +115,7 @@ public class XPipeInstallation {
}
private static Path getLocalInstallationBasePathForDaemonExecutable(Path executable) {
if (OsType.getLocal().equals(OsType.MAC)) {
if (OsType.getLocal().equals(OsType.MACOS)) {
return executable.getParent().getParent().getParent();
} else if (OsType.getLocal().equals(OsType.LINUX)) {
return executable.getParent().getParent().getParent();
@@ -138,7 +138,7 @@ public class XPipeInstallation {
return defaultInstallation;
}
if (OsType.getLocal().equals(OsType.MAC)) {
if (OsType.getLocal().equals(OsType.MACOS)) {
return FileNames.getParent(FileNames.getParent(FileNames.getParent(cliExecutable)));
} else {
return FileNames.getParent(FileNames.getParent(cliExecutable));

View File

@@ -26,7 +26,7 @@ public class XPipeTempDirectory {
if (!proc.executeBooleanSimpleCommand(proc.getShellType().getFileExistsCommand(dir))) {
proc.executeSimpleCommand(proc.getShellType().flatten(proc.getShellType().getMkdirsCommand(dir)), "Unable to access or create temporary directory " + dir);
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) {
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MACOS)) {
proc.executeSimpleCommand("chmod -f 777 \"" + dir + "\"");
}
}

View File

@@ -1,4 +1,4 @@
import io.xpipe.core.impl.ProcessControlProvider;
import io.xpipe.core.process.ProcessControlProvider;
import io.xpipe.core.source.WriteMode;
import io.xpipe.core.util.CoreJacksonModule;