Script rework

This commit is contained in:
crschnick 2024-06-07 18:11:52 +00:00
parent 5d9e097825
commit a8f9de7c83
7 changed files with 126 additions and 3 deletions

View file

@ -8,7 +8,9 @@ import lombok.Setter;
@Getter
public enum PredefinedScriptGroup {
CLINK("Clink", null, false),
STARSHIP("Starship", "Sets up and enables the starship shell prompt", true);
STARSHIP("Starship", "Sets up and enables the starship shell prompt", true),
MANAGEMENT("Management", "Some commonly used management scripts", true),
FILES("Files", "Scripts for files", true);
private final String name;
private final String description;

View file

@ -60,6 +60,18 @@ public enum PredefinedScriptStore {
.minimumDialect(ShellDialects.POWERSHELL)
.commands(file("starship_powershell.ps1"))
.initScript(true)
.build()),
APT_UPDATE("Apt update", () -> SimpleScriptStore.builder()
.group(PredefinedScriptGroup.MANAGEMENT.getEntry())
.minimumDialect(ShellDialects.SH)
.commands(file(("apt_update.sh")))
.shellScript(true)
.build()),
REMOVE_CR("CRLF to LF", () -> SimpleScriptStore.builder()
.group(PredefinedScriptGroup.FILES.getEntry())
.minimumDialect(ShellDialects.SH)
.commands(file(("crlf_to_lf.sh")))
.fileScript(true)
.build());
private final String name;

View file

@ -0,0 +1,99 @@
package io.xpipe.ext.base.script;
import io.xpipe.app.browser.action.BranchAction;
import io.xpipe.app.browser.action.BrowserAction;
import io.xpipe.app.browser.action.LeafAction;
import io.xpipe.app.browser.file.BrowserEntry;
import io.xpipe.app.browser.fs.OpenFileSystemModel;
import io.xpipe.app.browser.session.BrowserSessionModel;
import io.xpipe.app.core.AppI18n;
import io.xpipe.app.storage.DataStorage;
import io.xpipe.app.util.ScriptHelper;
import io.xpipe.core.process.ShellControl;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import org.kordamp.ikonli.javafx.FontIcon;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class RunScriptAction implements BrowserAction, BranchAction {
@Override
public Node getIcon(OpenFileSystemModel model, List<BrowserEntry> entries) {
return new FontIcon("mdi2l-linux");
}
@Override
public Category getCategory() {
return Category.OPEN;
}
@Override
public boolean acceptsEmptySelection() {
return true;
}
@Override
public ObservableValue<String> getName(OpenFileSystemModel model, List<BrowserEntry> entries) {
return AppI18n.observable("runScript");
}
@Override
public boolean isApplicable(OpenFileSystemModel model, List<BrowserEntry> entries) {
var sc = model.getFileSystem().getShell().orElseThrow();
return model.getBrowserModel() instanceof BrowserSessionModel
&& !getInstances(sc).isEmpty();
}
private Map<String, SimpleScriptStore> getInstances(ShellControl sc) {
var scripts = ScriptStore.flatten(ScriptStore.getDefaultEnabledScripts());
var map = new LinkedHashMap<String, SimpleScriptStore>();
for (SimpleScriptStore script : scripts) {
if (script.assemble(sc) == null) {
continue;
}
var entry = DataStorage.get().getStoreEntryIfPresent(script, true);
if (entry.isPresent()) {
map.put(entry.get().getName(), script);
}
}
return map;
}
@Override
public List<LeafAction> getBranchingActions(OpenFileSystemModel model, List<BrowserEntry> entries) {
var sc = model.getFileSystem().getShell().orElseThrow();
var scripts = getInstances(sc);
List<LeafAction> actions = scripts.entrySet().stream()
.map(e -> {
return new LeafAction() {
@Override
public void execute(OpenFileSystemModel model, List<BrowserEntry> entries) throws Exception {
var args = entries.stream().map(browserEntry -> browserEntry.getOptionallyQuotedFileName()).collect(Collectors.joining(" "));
execute(model, args);
}
private void execute(OpenFileSystemModel model, String args) throws Exception {
if (model.getBrowserModel() instanceof BrowserSessionModel bm) {
var content = e.getValue().assemble(sc);
var script = ScriptHelper.createExecScript(sc, content);
sc.executeSimpleCommand(sc.getShellDialect().runScriptCommand(sc, script.toString()) + " " + args);
}
}
@Override
public ObservableValue<String> getName(OpenFileSystemModel model, List<BrowserEntry> entries) {
return new SimpleStringProperty(e.getKey());
}
};
})
.map(leafAction -> (LeafAction) leafAction)
.toList();
return actions;
}
}

View file

@ -31,7 +31,7 @@ public class SimpleScriptStore extends ScriptStore implements ShellInitCommand.T
private final boolean shellScript;
private final boolean fileScript;
private String assemble(ShellControl shellControl) {
public String assemble(ShellControl shellControl) {
var targetType = shellControl.getOriginalShellDialect();
if (minimumDialect.isCompatibleTo(targetType)) {
var shebang = commands.startsWith("#");

View file

@ -7,6 +7,7 @@ import io.xpipe.ext.base.browser.*;
import io.xpipe.ext.base.desktop.DesktopApplicationStoreProvider;
import io.xpipe.ext.base.desktop.DesktopCommandStoreProvider;
import io.xpipe.ext.base.desktop.DesktopEnvironmentStoreProvider;
import io.xpipe.ext.base.script.RunScriptAction;
import io.xpipe.ext.base.script.ScriptDataStorageProvider;
import io.xpipe.ext.base.script.ScriptGroupStoreProvider;
import io.xpipe.ext.base.script.SimpleScriptStoreProvider;
@ -35,7 +36,7 @@ open module io.xpipe.ext.base {
requires org.kordamp.ikonli.javafx;
requires atlantafx.base;
provides BrowserAction with
provides BrowserAction with RunScriptAction,
FollowLinkAction,
BackAction,
ForwardAction,

View file

@ -0,0 +1 @@
sudo apt update

View file

@ -0,0 +1,8 @@
for arg in "$@"
do
file="arg"
temp_file=$(mktemp)
awk '{ sub("\r$", ""); print }' "$file" "$temp_file"
cat "$temp_file" > "file"
rm "$temp_file"
done