From 620c30382fc76d90d09b2eda6493d230e14108dc Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 10 May 2024 08:15:01 +0000 Subject: [PATCH] Fix scripts not applying --- .../io/xpipe/core/process/ScriptSnippet.java | 35 ---------- .../io/xpipe/core/process/ShellControl.java | 4 +- .../xpipe/core/process/ShellInitCommand.java | 69 +++++++++++++++++++ .../core/process/SimpleScriptSnippet.java | 27 -------- dist/changelogs/9.2_incremental.md | 1 + .../io/xpipe/ext/base/script/ScriptStore.java | 49 +++++++------ .../script/ScriptStoreTypeChoiceComp.java | 33 --------- .../ext/base/script/SimpleScriptStore.java | 23 +++---- .../ext/base/resources/scripts/clink.bat | 2 +- 9 files changed, 105 insertions(+), 138 deletions(-) delete mode 100644 core/src/main/java/io/xpipe/core/process/ScriptSnippet.java create mode 100644 core/src/main/java/io/xpipe/core/process/ShellInitCommand.java delete mode 100644 core/src/main/java/io/xpipe/core/process/SimpleScriptSnippet.java delete mode 100644 ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStoreTypeChoiceComp.java diff --git a/core/src/main/java/io/xpipe/core/process/ScriptSnippet.java b/core/src/main/java/io/xpipe/core/process/ScriptSnippet.java deleted file mode 100644 index ee2f6023..00000000 --- a/core/src/main/java/io/xpipe/core/process/ScriptSnippet.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.xpipe.core.process; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Getter; - -public interface ScriptSnippet { - - String content(ShellControl shellControl); - - ExecutionType executionType(); - - @Getter - enum ExecutionType { - @JsonProperty("dumbOnly") - DUMB_ONLY("dumbOnly"), - @JsonProperty("terminalOnly") - TERMINAL_ONLY("terminalOnly"), - @JsonProperty("both") - BOTH("both"); - - private final String id; - - ExecutionType(String id) { - this.id = id; - } - - public boolean runInDumb() { - return this == DUMB_ONLY || this == BOTH; - } - - public boolean runInTerminal() { - return this == TERMINAL_ONLY || this == BOTH; - } - } -} diff --git a/core/src/main/java/io/xpipe/core/process/ShellControl.java b/core/src/main/java/io/xpipe/core/process/ShellControl.java index 4220acbe..2b52f330 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellControl.java @@ -30,7 +30,7 @@ public interface ShellControl extends ProcessControl { ShellControl withSourceStore(ShellStore store); - List getInitCommands(); + List getInitCommands(); ParentSystemAccess getParentSystemAccess(); @@ -173,7 +173,7 @@ public interface ShellControl extends ProcessControl { ShellControl elevated(ElevationFunction elevationFunction); - ShellControl withInitSnippet(ScriptSnippet snippet); + ShellControl withInitSnippet(ShellInitCommand snippet); default ShellControl subShell(@NonNull ShellDialect type) { var o = new ShellOpenFunction() { diff --git a/core/src/main/java/io/xpipe/core/process/ShellInitCommand.java b/core/src/main/java/io/xpipe/core/process/ShellInitCommand.java new file mode 100644 index 00000000..6103d8f8 --- /dev/null +++ b/core/src/main/java/io/xpipe/core/process/ShellInitCommand.java @@ -0,0 +1,69 @@ +package io.xpipe.core.process; + +import lombok.NonNull; + +import java.util.Optional; + +public interface ShellInitCommand { + + default void runDumb(ShellControl shellControl) throws Exception { + throw new UnsupportedOperationException(); + } + + default Optional terminalContent(ShellControl shellControl) throws Exception { + throw new UnsupportedOperationException(); + } + + default boolean runInDumb() { + return false; + } + + default boolean runInTerminal() { + return false; + } + + interface Terminal extends ShellInitCommand { + + Optional terminalContent(ShellControl shellControl) throws Exception; + + default boolean runInTerminal() { + return true; + } + } + + class Simple implements ShellInitCommand { + + @NonNull + private final String content; + + private final boolean dumb; + + private final boolean terminal; + + public Simple(@NonNull String content, boolean dumb, boolean terminal) { + this.content = content; + this.dumb = dumb; + this.terminal = terminal; + } + + @Override + public void runDumb(ShellControl shellControl) throws Exception { + shellControl.executeSimpleCommand(content); + } + + @Override + public Optional terminalContent(ShellControl shellControl) throws Exception { + return Optional.of(content); + } + + @Override + public boolean runInDumb() { + return dumb; + } + + @Override + public boolean runInTerminal() { + return terminal; + } + } +} diff --git a/core/src/main/java/io/xpipe/core/process/SimpleScriptSnippet.java b/core/src/main/java/io/xpipe/core/process/SimpleScriptSnippet.java deleted file mode 100644 index b1122a5b..00000000 --- a/core/src/main/java/io/xpipe/core/process/SimpleScriptSnippet.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.xpipe.core.process; - -import lombok.NonNull; - -public class SimpleScriptSnippet implements ScriptSnippet { - - @NonNull - private final String content; - - @NonNull - private final ExecutionType executionType; - - public SimpleScriptSnippet(@NonNull String content, @NonNull ExecutionType executionType) { - this.content = content; - this.executionType = executionType; - } - - @Override - public String content(ShellControl shellControl) { - return content; - } - - @Override - public ExecutionType executionType() { - return executionType; - } -} diff --git a/dist/changelogs/9.2_incremental.md b/dist/changelogs/9.2_incremental.md index 598c8c1f..121132d9 100644 --- a/dist/changelogs/9.2_incremental.md +++ b/dist/changelogs/9.2_incremental.md @@ -26,6 +26,7 @@ The file browser has been reworked to support many new keyboard shortcuts and th ## Fixes +- Fix custom scripts not properly applying - Fix closing application window while XPipe was saving not properly applying all changes - Fix race condition when loading file icons - Fix state corruption of local shell, leading to NullPointers once a shell connection had to be killed diff --git a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java b/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java index 50aa290a..4c8e950b 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java @@ -6,15 +6,13 @@ import io.xpipe.app.storage.DataStoreEntry; import io.xpipe.app.storage.DataStoreEntryRef; import io.xpipe.app.util.ShellTemp; import io.xpipe.app.util.Validators; -import io.xpipe.core.process.ScriptSnippet; +import io.xpipe.core.process.ShellInitCommand; import io.xpipe.core.process.ShellControl; -import io.xpipe.core.process.SimpleScriptSnippet; import io.xpipe.core.store.DataStore; import io.xpipe.core.store.DataStoreState; import io.xpipe.core.store.FileNames; import io.xpipe.core.store.StatefulDataStore; import io.xpipe.core.util.JacksonizedValue; - import lombok.*; import lombok.experimental.FieldDefaults; import lombok.experimental.SuperBuilder; @@ -56,16 +54,29 @@ public abstract class ScriptStore extends JacksonizedValue implements DataStore, return pc; } - pc.onInit(shellControl -> { - passInitScripts(pc, initFlattened); - - var dir = initScriptsDirectory(shellControl, bringFlattened); - if (dir != null) { - shellControl.withInitSnippet(new SimpleScriptSnippet( - shellControl.getShellDialect().addToPathVariableCommand(List.of(dir), true), - ScriptSnippet.ExecutionType.TERMINAL_ONLY)); - } + initFlattened.forEach(simpleScriptStore -> { + pc.withInitSnippet(simpleScriptStore); }); + if (!bringFlattened.isEmpty()) { + pc.withInitSnippet(new ShellInitCommand() { + + String dir; + + @Override + public Optional terminalContent(ShellControl shellControl) throws Exception { + if (dir == null) { + dir = initScriptsDirectory(shellControl, bringFlattened); + } + + return Optional.ofNullable(shellControl.getShellDialect().addToPathVariableCommand(List.of(dir), true)); + } + + @Override + public boolean runInTerminal() { + return true; + } + }); + } return pc; } catch (StackOverflowError t) { throw new RuntimeException("Unable to set up scripts. Is there a circular script dependency?", t); @@ -74,20 +85,6 @@ public abstract class ScriptStore extends JacksonizedValue implements DataStore, } } - private static void passInitScripts(ShellControl pc, List scriptStores) { - scriptStores.forEach(simpleScriptStore -> { - if (pc.getInitCommands().contains(simpleScriptStore)) { - return; - } - - if (!simpleScriptStore.getMinimumDialect().isCompatibleTo(pc.getShellDialect())) { - return; - } - - pc.withInitSnippet(simpleScriptStore); - }); - } - private static String initScriptsDirectory(ShellControl proc, List scriptStores) throws Exception { if (scriptStores.isEmpty()) { diff --git a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStoreTypeChoiceComp.java b/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStoreTypeChoiceComp.java deleted file mode 100644 index 1e849029..00000000 --- a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStoreTypeChoiceComp.java +++ /dev/null @@ -1,33 +0,0 @@ -package io.xpipe.ext.base.script; - -import io.xpipe.app.core.AppI18n; -import io.xpipe.app.fxcomps.SimpleComp; -import io.xpipe.app.fxcomps.impl.ToggleGroupComp; - -import javafx.beans.property.Property; -import javafx.beans.property.SimpleObjectProperty; -import javafx.beans.value.ObservableValue; -import javafx.scene.layout.Region; - -import lombok.EqualsAndHashCode; -import lombok.Value; - -import java.util.Arrays; -import java.util.LinkedHashMap; - -@Value -@EqualsAndHashCode(callSuper = true) -public class ScriptStoreTypeChoiceComp extends SimpleComp { - - Property selected; - SimpleScriptStore.ExecutionType[] available = SimpleScriptStore.ExecutionType.values(); - - @Override - protected Region createSimple() { - var map = new LinkedHashMap>(); - Arrays.stream(available).forEach(executionType -> { - map.put(executionType, AppI18n.observable(executionType.getId())); - }); - return new ToggleGroupComp<>(selected, new SimpleObjectProperty<>(map)).createRegion(); - } -} diff --git a/ext/base/src/main/java/io/xpipe/ext/base/script/SimpleScriptStore.java b/ext/base/src/main/java/io/xpipe/ext/base/script/SimpleScriptStore.java index 48a9cd92..f239e30a 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/script/SimpleScriptStore.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/script/SimpleScriptStore.java @@ -1,13 +1,12 @@ package io.xpipe.ext.base.script; +import com.fasterxml.jackson.annotation.JsonTypeName; import io.xpipe.app.storage.DataStoreEntryRef; import io.xpipe.app.util.ScriptHelper; import io.xpipe.app.util.Validators; -import io.xpipe.core.process.ScriptSnippet; import io.xpipe.core.process.ShellControl; import io.xpipe.core.process.ShellDialect; - -import com.fasterxml.jackson.annotation.JsonTypeName; +import io.xpipe.core.process.ShellInitCommand; import lombok.Getter; import lombok.experimental.SuperBuilder; import lombok.extern.jackson.Jacksonized; @@ -15,13 +14,14 @@ import lombok.extern.jackson.Jacksonized; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; @SuperBuilder @Getter @Jacksonized @JsonTypeName("script") -public class SimpleScriptStore extends ScriptStore implements ScriptSnippet { +public class SimpleScriptStore extends ScriptStore implements ShellInitCommand.Terminal { private final ShellDialect minimumDialect; private final String commands; @@ -42,16 +42,6 @@ public class SimpleScriptStore extends ScriptStore implements ScriptSnippet { return null; } - @Override - public String content(ShellControl shellControl) { - return assemble(shellControl); - } - - @Override - public ScriptSnippet.ExecutionType executionType() { - return ExecutionType.TERMINAL_ONLY; - } - @Override public void checkComplete() throws Throwable { Validators.nonNull(group); @@ -75,4 +65,9 @@ public class SimpleScriptStore extends ScriptStore implements ScriptSnippet { public List> getEffectiveScripts() { return scripts != null ? scripts.stream().filter(Objects::nonNull).toList() : List.of(); } + + @Override + public Optional terminalContent(ShellControl shellControl) throws Exception { + return Optional.ofNullable(assemble(shellControl)); + } } diff --git a/ext/base/src/main/resources/io/xpipe/ext/base/resources/scripts/clink.bat b/ext/base/src/main/resources/io/xpipe/ext/base/resources/scripts/clink.bat index 665b0afa..c685eb3d 100644 --- a/ext/base/src/main/resources/io/xpipe/ext/base/resources/scripts/clink.bat +++ b/ext/base/src/main/resources/io/xpipe/ext/base/resources/scripts/clink.bat @@ -15,5 +15,5 @@ $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials;^ if ($defaultCreds) {^ $downloader.Credentials = $defaultCreds^ }^ -$downloader.DownloadFile("https://github.com/chrisant996/clink/releases/download/v1.6.5/clink.1.6.5.8f46a4.zip", "$env:TEMP\clink.zip");^ +$downloader.DownloadFile("https://github.com/chrisant996/clink/releases/download/v1.6.13/clink.1.6.13.eb61b2.zip", "$env:TEMP\clink.zip");^ Expand-Archive -Force -LiteralPath "$env:TEMP\clink.zip" -DestinationPath "$env:TEMP\xpipe\scriptdata\clink"; | powershell -NoLogo >NUL