From 26a7592ed6f3787afa8eba654a262038c09bce50 Mon Sep 17 00:00:00 2001 From: crschnick Date: Sat, 25 Feb 2023 02:46:40 +0000 Subject: [PATCH] More browser improvements --- .../app/prefs/ExternalApplicationType.java | 3 +- .../xpipe/app/prefs/ExternalTerminalType.java | 14 +- .../io/xpipe/app/util/ApplicationHelper.java | 8 +- .../io/xpipe/app/util/DesktopShortcuts.java | 3 +- .../java/io/xpipe/core/impl/LocalStore.java | 188 +++++++++--------- .../io/xpipe/core/process/ShellDialect.java | 2 + .../java/io/xpipe/core/store/ShellStore.java | 2 +- 7 files changed, 113 insertions(+), 107 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java index f43d83c7..13d84cbe 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java @@ -2,6 +2,7 @@ package io.xpipe.app.prefs; import io.xpipe.app.ext.PrefsChoiceValue; import io.xpipe.app.issue.ErrorEvent; +import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.OsType; import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.store.ShellStore; @@ -76,7 +77,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { } public boolean isAvailable() { - try (ShellProcessControl pc = ShellStore.local().create().start()) { + try (ShellProcessControl pc = LocalStore.getShell()) { return pc.executeBooleanSimpleCommand(pc.getShellDialect().getWhichCommand(executable)); } catch (Exception e) { ErrorEvent.fromThrowable(e).omit().handle(); diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java index 371f92cd..34ced2e2 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java @@ -4,9 +4,9 @@ import io.xpipe.app.ext.PrefsChoiceValue; import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.util.ApplicationHelper; import io.xpipe.app.util.MacOsPermissions; +import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.OsType; import io.xpipe.core.process.ShellProcessControl; -import io.xpipe.core.store.ShellStore; import java.util.List; @@ -133,7 +133,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override public void launch(String name, String command) throws Exception { - try (ShellProcessControl pc = ShellStore.local().create().start()) { + try (ShellProcessControl pc = LocalStore.getShell()) { var suffix = command.equals(pc.getShellDialect().getNormalOpenCommand()) ? "\"\"" : "\"" + command.replaceAll("\"", "\\\\\"") + "\""; @@ -157,7 +157,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { } var format = custom.contains("$cmd") ? custom : custom + " $cmd"; - try (var pc = ShellStore.local().create().start()) { + try (var pc = LocalStore.getShell()) { var toExecute = format.replace("$cmd", command); if (pc.getOsType().equals(OsType.WINDOWS)) { toExecute = "start \"" + name + "\" " + toExecute; @@ -187,7 +187,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override public void launch(String name, String command) throws Exception { - try (ShellProcessControl pc = ShellStore.local().create().start()) { + try (ShellProcessControl pc = LocalStore.getShell()) { var cmd = String.format( """ osascript - "$@" < getShell() { - return Optional.empty(); - } - - @Override - public FileSystem open() throws Exception { - return this; - } - - @Override - public boolean exists(String file) { - return Files.exists(Path.of(file)); - } - - @Override - public void delete(String file) throws Exception { - Files.delete(Path.of(file)); - } - - @Override - public void copy(String file, String newFile) throws Exception { - Files.copy(Path.of(file), Path.of(newFile), StandardCopyOption.REPLACE_EXISTING); - } - - @Override - public void move(String file, String newFile) throws Exception { - Files.move(Path.of(file), Path.of(newFile), StandardCopyOption.REPLACE_EXISTING); - } - - @Override - public boolean mkdirs(String file) throws Exception { - try { - Files.createDirectories(Path.of(file)); - return true; - } catch (Exception ex) { - return false; - } - } - - @Override - public void touch(String file) throws Exception { - if (exists(file)) { - return; - } - - Files.createFile(Path.of(file)); - } - - @Override - public boolean isDirectory(String file) throws Exception { - return Files.isDirectory(Path.of(file)); - } - - @Override - public Stream listFiles(String file) throws Exception { - return Files.list(Path.of(file)).map(path -> { - try { - var date = Files.getLastModifiedTime(path); - var size = Files.isDirectory(path) ? 0 : Files.size(path); - return new FileEntry( - this, - path.toString(), - date.toInstant(), - Files.isDirectory(path), - Files.isHidden(path), - Files.isExecutable(path), - size); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); - } - - @Override - public List listRoots() throws Exception { - return StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false).map(path -> path.toString()).toList(); - } + return new ConnectionFileSystem(ShellStore.local().create()) { @Override public InputStream openInput(String file) throws Exception { - var p = Path.of(file); + var p = wrap(file); return Files.newInputStream(p); } @Override public OutputStream openOutput(String file) throws Exception { - var p = Path.of(file); + var p = wrap(file); return Files.newOutputStream(p); } - @Override - public void close() throws IOException {} + private Path wrap(String file) { + for (var e : System.getenv().entrySet()) { + file = file.replace( + ShellDialects.getPlatformDefault().environmentVariable(e.getKey()), + e.getValue()); + } + return Path.of(file); + } + +// @Override +// public boolean exists(String file) { +// return Files.exists(wrap(file)); +// } +// +// @Override +// public void delete(String file) throws Exception { +// Files.delete(wrap(file)); +// } +// +// @Override +// public void copy(String file, String newFile) throws Exception { +// Files.copy(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING); +// } +// +// @Override +// public void move(String file, String newFile) throws Exception { +// Files.move(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING); +// } +// +// @Override +// public boolean mkdirs(String file) throws Exception { +// try { +// Files.createDirectories(wrap(file)); +// return true; +// } catch (Exception ex) { +// return false; +// } +// } +// +// @Override +// public void touch(String file) throws Exception { +// if (exists(file)) { +// return; +// } +// +// Files.createFile(wrap(file)); +// } +// +// @Override +// public boolean isDirectory(String file) throws Exception { +// return Files.isDirectory(wrap(file)); +// } +// +// @Override +// public Stream listFiles(String file) throws Exception { +// return Files.list(wrap(file)).map(path -> { +// try { +// var date = Files.getLastModifiedTime(path); +// var size = Files.isDirectory(path) ? 0 : Files.size(path); +// return new FileEntry( +// this, +// path.toString(), +// date.toInstant(), +// Files.isDirectory(path), +// Files.isHidden(path), +// Files.isExecutable(path), +// size); +// } catch (IOException e) { +// throw new UncheckedIOException(e); +// } +// }); +// } +// +// @Override +// public List listRoots() throws Exception { +// return StreamSupport.stream( +// FileSystems.getDefault().getRootDirectories().spliterator(), false) +// .map(path -> path.toString()) +// .toList(); +// } }; } diff --git a/core/src/main/java/io/xpipe/core/process/ShellDialect.java b/core/src/main/java/io/xpipe/core/process/ShellDialect.java index 5331377f..7f81d1c3 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellDialect.java +++ b/core/src/main/java/io/xpipe/core/process/ShellDialect.java @@ -47,6 +47,8 @@ public interface ShellDialect { String getExitCodeVariable(); + String environmentVariable(String name); + default String getConcatenationOperator() { return ";"; } diff --git a/core/src/main/java/io/xpipe/core/store/ShellStore.java b/core/src/main/java/io/xpipe/core/store/ShellStore.java index 8381b78b..078f512d 100644 --- a/core/src/main/java/io/xpipe/core/store/ShellStore.java +++ b/core/src/main/java/io/xpipe/core/store/ShellStore.java @@ -10,7 +10,7 @@ import java.nio.charset.Charset; public interface ShellStore extends DataStore, StatefulDataStore, LaunchableStore, FileSystemStore { - public static MachineStore local() { + public static ShellStore local() { return new LocalStore(); }