From 464e04a6bb66e64c9ac3d8449195a6bea8006470 Mon Sep 17 00:00:00 2001 From: crschnick Date: Wed, 1 Feb 2023 10:15:51 +0000 Subject: [PATCH] Various shell connection optimizations --- .../comp/storage/store/StoreEntryComp.java | 19 +++++----- .../java/io/xpipe/app/grid/AppInstaller.java | 4 +-- build.gradle | 2 -- .../core/process/ShellProcessControl.java | 5 +++ .../io/xpipe/extension/util/ScriptHelper.java | 35 ++++++------------- version | 2 +- 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/comp/storage/store/StoreEntryComp.java b/app/src/main/java/io/xpipe/app/comp/storage/store/StoreEntryComp.java index 9c007a29..9f159ec9 100644 --- a/app/src/main/java/io/xpipe/app/comp/storage/store/StoreEntryComp.java +++ b/app/src/main/java/io/xpipe/app/comp/storage/store/StoreEntryComp.java @@ -8,7 +8,6 @@ import io.xpipe.app.core.AppI18n; import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.storage.DataStorage; import io.xpipe.extension.I18n; -import io.xpipe.extension.event.ErrorEvent; import io.xpipe.extension.fxcomps.Comp; import io.xpipe.extension.fxcomps.SimpleComp; import io.xpipe.extension.fxcomps.SimpleCompStructure; @@ -21,6 +20,7 @@ import io.xpipe.extension.fxcomps.impl.PrettyImageComp; import io.xpipe.extension.fxcomps.util.PlatformThread; import io.xpipe.extension.fxcomps.util.SimpleChangeListener; import io.xpipe.extension.util.OsHelper; +import io.xpipe.extension.util.ThreadHelper; import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleStringProperty; import javafx.css.PseudoClass; @@ -103,7 +103,9 @@ public class StoreEntryComp extends SimpleComp { var storeIcon = imageComp.createRegion(); storeIcon.getStyleClass().add("icon"); if (entry.getState().getValue().isUsable()) { - new FancyTooltipAugment<>(new SimpleStringProperty(entry.getEntry().getProvider().getDisplayName())).augment(storeIcon); + new FancyTooltipAugment<>(new SimpleStringProperty( + entry.getEntry().getProvider().getDisplayName())) + .augment(storeIcon); } return storeIcon; } @@ -122,7 +124,6 @@ public class StoreEntryComp extends SimpleComp { var storeIcon = createIcon(); - grid.getColumnConstraints() .addAll( createShareConstraint(grid, STORE_TYPE_WIDTH), createShareConstraint(grid, NAME_WIDTH), @@ -167,11 +168,9 @@ public class StoreEntryComp extends SimpleComp { var button = new IconButtonComp( actionProvider.getIcon(entry.getEntry().getStore().asNeeded()), () -> { - try { + ThreadHelper.runFailableAsync(() -> { actionProvider.execute(entry.getEntry().getStore().asNeeded()); - } catch (Exception e) { - ErrorEvent.fromThrowable(e).handle(); - } + }); }); button.apply(new FancyTooltipAugment<>( actionProvider.getName(entry.getEntry().getStore().asNeeded()))); @@ -224,11 +223,9 @@ public class StoreEntryComp extends SimpleComp { var icon = actionProvider.getIcon(entry.getEntry().getStore().asNeeded()); var item = new MenuItem(null, new FontIcon(icon)); item.setOnAction(event -> { - try { + ThreadHelper.runFailableAsync(() -> { actionProvider.execute(entry.getEntry().getStore().asNeeded()); - } catch (Exception e) { - ErrorEvent.fromThrowable(e).handle(); - } + }); }); item.textProperty().bind(name); item.disableProperty().bind(Bindings.not(p.getValue())); diff --git a/app/src/main/java/io/xpipe/app/grid/AppInstaller.java b/app/src/main/java/io/xpipe/app/grid/AppInstaller.java index 7db7b16b..0ae357ef 100644 --- a/app/src/main/java/io/xpipe/app/grid/AppInstaller.java +++ b/app/src/main/java/io/xpipe/app/grid/AppInstaller.java @@ -11,7 +11,6 @@ import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellTypes; import io.xpipe.core.store.ShellStore; import io.xpipe.core.util.XPipeInstallation; -import io.xpipe.core.util.XPipeTempDirectory; import io.xpipe.extension.util.ScriptHelper; import lombok.Getter; @@ -38,8 +37,7 @@ public class AppInstaller { if (s.isLocal()) { targetFile = localFile.toString(); } else { - targetFile = FileNames.join( - XPipeTempDirectory.get(s), localFile.getFileName().toString()); + targetFile = FileNames.join(s.getTemporaryDirectory(), localFile.getFileName().toString()); try (CommandProcessControl c = s.command(s.getShellType().getStreamFileWriteCommand(targetFile)) .start()) { c.discardOut(); diff --git a/build.gradle b/build.gradle index 2ed2cd52..0d4d3d65 100644 --- a/build.gradle +++ b/build.gradle @@ -19,8 +19,6 @@ def getArchName() { return arch } -println(System.getenv('RELEASE')) - project.ext { ci = System.getenv('CI') != null os = org.gradle.internal.os.OperatingSystem.current() 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 24074cbf..5e38444a 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -15,6 +15,11 @@ public interface ShellProcessControl extends ProcessControl { String prepareIntermediateTerminalOpen(String content) throws Exception; + String getTemporaryDirectory() throws Exception; + + public void checkRunning() throws Exception; + + default String executeStringSimpleCommand(String command) throws Exception { try (CommandProcessControl c = command(command).start()) { return c.readOrThrow(); diff --git a/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java b/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java index cd857ea6..ef0cc99c 100644 --- a/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java +++ b/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java @@ -6,7 +6,6 @@ import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellType; import io.xpipe.core.store.ShellStore; import io.xpipe.core.util.SecretValue; -import io.xpipe.core.util.XPipeTempDirectory; import io.xpipe.extension.event.TrackEvent; import lombok.SneakyThrows; @@ -15,10 +14,10 @@ import java.util.Random; public class ScriptHelper { - public static int getConnectionHash(String command) { - // This deterministic approach can cause permission problems when two different users execute the same command on a system + public static int getScriptId() { + // A deterministic approach can cause permission problems when two different users execute the same command on a system + // Therefore, use a random approach return new Random().nextInt(Integer.MAX_VALUE); - //return Math.abs(Objects.hash(command, XPipeSession.get().getSystemSessionId())); } @SneakyThrows @@ -44,7 +43,8 @@ public class ScriptHelper { } if (toExecuteInShell != null) { - content += toExecuteInShell + nl; + // Normalize line endings + content += String.join(nl, toExecuteInShell.lines().toList()) + nl; content += t.getExitCommand() + nl; } @@ -52,19 +52,11 @@ public class ScriptHelper { return t.getInitFileOpenCommand(initFile); } - public static String prepend(ShellProcessControl processControl, List init, String commands) { - var prefix = init != null && init.size() > 0 - ? String.join(processControl.getShellType().getNewLine().getNewLineString(), init) - + processControl.getShellType().getNewLine().getNewLineString() - : ""; - return prefix + commands; - } - @SneakyThrows public static String createExecScript(ShellProcessControl processControl, String content, boolean restart) { - var fileName = "exec-" + getConnectionHash(content); + var fileName = "exec-" + getScriptId(); ShellType type = processControl.getShellType(); - var temp = XPipeTempDirectory.get(processControl); + var temp = processControl.getTemporaryDirectory(); var file = FileNames.join(temp, fileName + "." + type.getScriptFileEnding()); return createExecScript(processControl, file, content, restart); } @@ -75,20 +67,15 @@ public class ScriptHelper { ShellType type = processControl.getShellType(); content = type.prepareScriptContent(content); - if (processControl.executeBooleanSimpleCommand(type.getFileExistsCommand(file))) { - return file; - } - TrackEvent.withTrace("proc", "Writing exec script") .tag("file", file) .tag("content", content) .handle(); - processControl.executeSimpleCommand(type.getFileTouchCommand(file), "Failed to create script " + file); + // processControl.executeSimpleCommand(type.getFileTouchCommand(file), "Failed to create script " + file); + processControl.executeSimpleCommand(type.getTextFileWriteCommand(content, file)); processControl.executeSimpleCommand( type.getMakeExecutableCommand(file), "Failed to make script " + file + " executable"); - - processControl.executeSimpleCommand(type.getTextFileWriteCommand(content, file)); return file; } @@ -96,8 +83,8 @@ public class ScriptHelper { public static String createAskPassScript( SecretValue pass, ShellProcessControl parent, ShellType type, boolean restart) { var content = type.getScriptEchoCommand(pass.getSecretValue()); - var temp = XPipeTempDirectory.get(parent); - var file = FileNames.join(temp, "askpass-" + getConnectionHash(content) + "." + type.getScriptFileEnding()); + var temp = parent.getTemporaryDirectory(); + var file = FileNames.join(temp, "askpass-" + getScriptId() + "." + type.getScriptFileEnding()); return createExecScript(parent, file, content, restart); } } diff --git a/version b/version index b1714db3..d39a225b 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.4.22 \ No newline at end of file +0.4.23 \ No newline at end of file