From b3cfae5a92e5c6a3e6578c0f28aa4e8d48cacbf5 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Fri, 9 Dec 2022 01:44:12 +0100 Subject: [PATCH] More fixes for shells and prefs --- .gitignore | 3 ++ .../impl/LocalProcessControlProvider.java | 6 ++++ .../java/io/xpipe/core/process/OsType.java | 4 +-- .../io/xpipe/core/process/ProcessControl.java | 2 +- .../core/process/ShellProcessControl.java | 29 +++++++++++----- .../java/io/xpipe/core/process/ShellType.java | 6 ++-- .../io/xpipe/core/util/XPipeInstallation.java | 6 ++-- .../xpipe/core/util/XPipeTempDirectory.java | 34 +++++++++++++++++++ extension/build.gradle | 2 ++ .../extension/XPipeServiceProviders.java | 9 +++-- .../xpipe/extension/prefs/PrefsHandler.java | 6 +++- .../xpipe/extension/prefs/PrefsProvider.java | 27 +++++++++++++-- .../xpipe/extension/prefs/PrefsProviders.java | 22 ------------ .../io/xpipe/extension/util/XPipeDaemon.java | 4 +++ extension/src/main/java/module-info.java | 2 ++ 15 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java delete mode 100644 extension/src/main/java/io/xpipe/extension/prefs/PrefsProviders.java diff --git a/.gitignore b/.gitignore index bb66ac789..d48914325 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ .gradle/ build/ .idea +local/ +local_test/ +local_stage/ dev.properties extensions.txt local/ diff --git a/core/src/main/java/io/xpipe/core/impl/LocalProcessControlProvider.java b/core/src/main/java/io/xpipe/core/impl/LocalProcessControlProvider.java index 62cfaafa2..75bc5e406 100644 --- a/core/src/main/java/io/xpipe/core/impl/LocalProcessControlProvider.java +++ b/core/src/main/java/io/xpipe/core/impl/LocalProcessControlProvider.java @@ -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; } diff --git a/core/src/main/java/io/xpipe/core/process/OsType.java b/core/src/main/java/io/xpipe/core/process/OsType.java index f833eb6c4..1cbf8a9be 100644 --- a/core/src/main/java/io/xpipe/core/process/OsType.java +++ b/core/src/main/java/io/xpipe/core/process/OsType.java @@ -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 diff --git a/core/src/main/java/io/xpipe/core/process/ProcessControl.java b/core/src/main/java/io/xpipe/core/process/ProcessControl.java index 3601eef8a..9261951bc 100644 --- a/core/src/main/java/io/xpipe/core/process/ProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ProcessControl.java @@ -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; diff --git a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java index 47e103a0a..7dbcae7d2 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -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 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 command); default ShellProcessControl consoleCommand(@NonNull String command) { - return consoleCommand(shellProcessControl -> command); + return consoleCommand((shellProcessControl, c) -> command); } - ShellProcessControl consoleCommand(@NonNull Function command); + ShellProcessControl consoleCommand(@NonNull BiFunction command); void executeCommand(String command) throws Exception; diff --git a/core/src/main/java/io/xpipe/core/process/ShellType.java b/core/src/main/java/io/xpipe/core/process/ShellType.java index c05a29b2c..f05714198 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellType.java +++ b/core/src/main/java/io/xpipe/core/process/ShellType.java @@ -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(); diff --git a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java index 1c9157377..b8cce360a 100644 --- a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java +++ b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java @@ -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"; diff --git a/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java new file mode 100644 index 000000000..2ccd8ce78 --- /dev/null +++ b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java @@ -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); + } + } +} diff --git a/extension/build.gradle b/extension/build.gradle index bf4205b56..3943894da 100644 --- a/extension/build.gradle +++ b/extension/build.gradle @@ -31,6 +31,8 @@ dependencies { compileOnly 'com.jfoenix:jfoenix:9.0.10' compileOnly 'org.controlsfx:controlsfx:11.1.2' compileOnly 'org.apache.commons:commons-lang3:3.12.0' + compileOnly group: 'com.dlsc.preferencesfx', name: 'preferencesfx-core', version: '11.9.0' + compileOnly group: 'com.dlsc.formsfx', name: 'formsfx-core', version: '11.3.2' } diff --git a/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java b/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java index ac832916c..fb769ec8a 100644 --- a/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java +++ b/extension/src/main/java/io/xpipe/extension/XPipeServiceProviders.java @@ -5,7 +5,8 @@ import io.xpipe.core.impl.LocalProcessControlProvider; import io.xpipe.core.util.JacksonMapper; import io.xpipe.core.util.ProxyFunction; import io.xpipe.extension.event.TrackEvent; -import io.xpipe.extension.prefs.PrefsProviders; +import io.xpipe.extension.prefs.PrefsProvider; +import io.xpipe.extension.util.XPipeDaemon; public class XPipeServiceProviders { @@ -35,8 +36,12 @@ public class XPipeServiceProviders { DataSourceActionProvider.init(layer); SupportedApplicationProviders.loadAll(layer); - PrefsProviders.init(layer); ProxyFunction.init(layer); + + if (XPipeDaemon.getInstanceIfPresent().isPresent()) { + PrefsProvider.init(layer); + } + TrackEvent.info("Finished loading extension providers"); } } diff --git a/extension/src/main/java/io/xpipe/extension/prefs/PrefsHandler.java b/extension/src/main/java/io/xpipe/extension/prefs/PrefsHandler.java index 9d2528c7e..45fcc0e65 100644 --- a/extension/src/main/java/io/xpipe/extension/prefs/PrefsHandler.java +++ b/extension/src/main/java/io/xpipe/extension/prefs/PrefsHandler.java @@ -1,6 +1,10 @@ package io.xpipe.extension.prefs; +import com.dlsc.preferencesfx.model.Setting; + +import java.util.List; + public interface PrefsHandler { - // void addSetting(List category, String group, Setting setting); + void addSetting(List category, String group, Setting setting, Class c); } diff --git a/extension/src/main/java/io/xpipe/extension/prefs/PrefsProvider.java b/extension/src/main/java/io/xpipe/extension/prefs/PrefsProvider.java index ac35cc904..11065e521 100644 --- a/extension/src/main/java/io/xpipe/extension/prefs/PrefsProvider.java +++ b/extension/src/main/java/io/xpipe/extension/prefs/PrefsProvider.java @@ -1,6 +1,29 @@ package io.xpipe.extension.prefs; -public interface PrefsProvider { +import java.util.ServiceLoader; +import java.util.Set; +import java.util.stream.Collectors; - void addPrefs(PrefsHandler handler); +public abstract class PrefsProvider { + + private static Set ALL; + + public static void init(ModuleLayer layer) { + if (ALL == null) { + ALL = ServiceLoader.load(layer, PrefsProvider.class).stream() + .map(ServiceLoader.Provider::get) + .collect(Collectors.toSet()); + } + } + + public static Set getAll() { + return ALL; + } + + @SuppressWarnings("unchecked") + public static T get(Class c) { + return (T) ALL.stream().filter(prefsProvider -> prefsProvider.getClass().equals(c)).findAny().orElseThrow(); + } + + public abstract void addPrefs(PrefsHandler handler); } diff --git a/extension/src/main/java/io/xpipe/extension/prefs/PrefsProviders.java b/extension/src/main/java/io/xpipe/extension/prefs/PrefsProviders.java deleted file mode 100644 index 9fcfd91a8..000000000 --- a/extension/src/main/java/io/xpipe/extension/prefs/PrefsProviders.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.xpipe.extension.prefs; - -import java.util.ServiceLoader; -import java.util.Set; -import java.util.stream.Collectors; - -public class PrefsProviders { - - private static Set ALL; - - public static void init(ModuleLayer layer) { - if (ALL == null) { - ALL = ServiceLoader.load(layer, PrefsProvider.class).stream() - .map(ServiceLoader.Provider::get) - .collect(Collectors.toSet()); - } - } - - public static Set getAll() { - return ALL; - } -} diff --git a/extension/src/main/java/io/xpipe/extension/util/XPipeDaemon.java b/extension/src/main/java/io/xpipe/extension/util/XPipeDaemon.java index ac4e59fd0..9fce634a5 100644 --- a/extension/src/main/java/io/xpipe/extension/util/XPipeDaemon.java +++ b/extension/src/main/java/io/xpipe/extension/util/XPipeDaemon.java @@ -24,6 +24,10 @@ public interface XPipeDaemon { return ServiceLoader.load(XPipeDaemon.class).findFirst().orElseThrow(); } + static Optional getInstanceIfPresent() { + return ServiceLoader.load(XPipeDaemon.class).findFirst(); + } + void withResource(String module, String file, Charsetter.FailableConsumer con); List getNamedStores(); diff --git a/extension/src/main/java/module-info.java b/extension/src/main/java/module-info.java index 2b3e1c6b6..8dab653b8 100644 --- a/extension/src/main/java/module-info.java +++ b/extension/src/main/java/module-info.java @@ -32,6 +32,8 @@ open module io.xpipe.extension { requires static org.fxmisc.flowless; requires static org.kordamp.ikonli.javafx; requires static com.jfoenix; + requires static com.dlsc.preferencesfx; + requires static com.dlsc.formsfx; uses DataSourceProvider; uses SupportedApplicationProvider;