Fix scripts not applying

This commit is contained in:
crschnick 2024-05-10 08:15:01 +00:00
parent 7507f664df
commit 620c30382f
9 changed files with 105 additions and 138 deletions

View file

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

View file

@ -30,7 +30,7 @@ public interface ShellControl extends ProcessControl {
ShellControl withSourceStore(ShellStore store);
List<ScriptSnippet> getInitCommands();
List<ShellInitCommand> 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() {

View file

@ -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<String> terminalContent(ShellControl shellControl) throws Exception {
throw new UnsupportedOperationException();
}
default boolean runInDumb() {
return false;
}
default boolean runInTerminal() {
return false;
}
interface Terminal extends ShellInitCommand {
Optional<String> 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<String> terminalContent(ShellControl shellControl) throws Exception {
return Optional.of(content);
}
@Override
public boolean runInDumb() {
return dumb;
}
@Override
public boolean runInTerminal() {
return terminal;
}
}
}

View file

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

View file

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

View file

@ -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<String> 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<SimpleScriptStore> 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<SimpleScriptStore> scriptStores)
throws Exception {
if (scriptStores.isEmpty()) {

View file

@ -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<SimpleScriptStore.ExecutionType> selected;
SimpleScriptStore.ExecutionType[] available = SimpleScriptStore.ExecutionType.values();
@Override
protected Region createSimple() {
var map = new LinkedHashMap<SimpleScriptStore.ExecutionType, ObservableValue<String>>();
Arrays.stream(available).forEach(executionType -> {
map.put(executionType, AppI18n.observable(executionType.getId()));
});
return new ToggleGroupComp<>(selected, new SimpleObjectProperty<>(map)).createRegion();
}
}

View file

@ -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<DataStoreEntryRef<ScriptStore>> getEffectiveScripts() {
return scripts != null ? scripts.stream().filter(Objects::nonNull).toList() : List.of();
}
@Override
public Optional<String> terminalContent(ShellControl shellControl) throws Exception {
return Optional.ofNullable(assemble(shellControl));
}
}

View file

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