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

3
.gitignore vendored
View file

@ -1,6 +1,9 @@
.gradle/
build/
.idea
local/
local_test/
local_stage/
dev.properties
extensions.txt
local/

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

View file

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

View file

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

View file

@ -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<String> category, String group, Setting<?,?> setting);
void addSetting(List<String> category, String group, Setting<?,?> setting, Class<?> c);
}

View file

@ -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<PrefsProvider> 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<PrefsProvider> getAll() {
return ALL;
}
@SuppressWarnings("unchecked")
public static <T extends PrefsProvider> T get(Class<T> c) {
return (T) ALL.stream().filter(prefsProvider -> prefsProvider.getClass().equals(c)).findAny().orElseThrow();
}
public abstract void addPrefs(PrefsHandler handler);
}

View file

@ -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<PrefsProvider> 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<PrefsProvider> getAll() {
return ALL;
}
}

View file

@ -24,6 +24,10 @@ public interface XPipeDaemon {
return ServiceLoader.load(XPipeDaemon.class).findFirst().orElseThrow();
}
static Optional<XPipeDaemon> getInstanceIfPresent() {
return ServiceLoader.load(XPipeDaemon.class).findFirst();
}
void withResource(String module, String file, Charsetter.FailableConsumer<Path, IOException> con);
List<DataStore> getNamedStores();

View file

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