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