xpipe/ext/base/src/main/java/io/xpipe/ext/base/browser/RunAction.java

82 lines
2.4 KiB
Java
Raw Normal View History

2023-05-21 01:49:58 +12:00
package io.xpipe.ext.base.browser;
2024-04-14 04:23:09 +12:00
import io.xpipe.app.browser.file.BrowserEntry;
import io.xpipe.app.browser.fs.OpenFileSystemModel;
import io.xpipe.app.core.AppI18n;
2024-04-01 20:25:06 +13:00
import io.xpipe.core.process.CommandBuilder;
2023-05-21 01:49:58 +12:00
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellControl;
2024-03-13 20:04:54 +13:00
import io.xpipe.core.process.ShellDialects;
2023-05-30 22:08:58 +12:00
import io.xpipe.core.store.FileKind;
2023-05-21 01:49:58 +12:00
import io.xpipe.core.store.FileSystem;
2024-04-19 09:37:28 +12:00
2024-04-14 04:23:09 +12:00
import javafx.beans.value.ObservableValue;
2023-05-21 01:49:58 +12:00
import javafx.scene.Node;
2024-04-19 09:37:28 +12:00
2023-05-21 01:49:58 +12:00
import org.kordamp.ikonli.javafx.FontIcon;
import java.util.List;
2023-06-16 15:38:39 +12:00
import java.util.stream.Stream;
2023-05-21 01:49:58 +12:00
public class RunAction extends MultiExecuteAction {
private boolean isExecutable(FileSystem.FileEntry e) {
2023-05-30 22:08:58 +12:00
if (e.getKind() != FileKind.FILE) {
2023-05-21 01:49:58 +12:00
return false;
}
if (e.getExecutable() != null && e.getExecutable()) {
return true;
}
var shell = e.getFileSystem().getShell();
if (shell.isEmpty()) {
return false;
}
var os = shell.get().getOsType();
2023-06-16 15:38:39 +12:00
if (os.equals(OsType.WINDOWS)
&& Stream.of("exe", "bat", "ps1", "cmd")
.anyMatch(s -> e.getPath().endsWith(s))) {
2023-05-21 01:49:58 +12:00
return true;
}
2024-03-23 19:53:08 +13:00
if (ShellDialects.isPowershell(shell.get())
&& Stream.of("ps1").anyMatch(s -> e.getPath().endsWith(s))) {
2024-03-13 20:04:54 +13:00
return true;
}
2023-06-16 15:38:39 +12:00
if (Stream.of("sh", "command").anyMatch(s -> e.getPath().endsWith(s))) {
2023-05-21 01:49:58 +12:00
return true;
}
return false;
}
@Override
public Node getIcon(OpenFileSystemModel model, List<BrowserEntry> entries) {
return new FontIcon("mdi2p-play");
2023-05-21 01:49:58 +12:00
}
@Override
public Category getCategory() {
return Category.CUSTOM;
2023-05-21 01:49:58 +12:00
}
@Override
2024-04-14 04:23:09 +12:00
public ObservableValue<String> getName(OpenFileSystemModel model, List<BrowserEntry> entries) {
return AppI18n.observable("run");
2023-05-21 01:49:58 +12:00
}
@Override
public boolean isApplicable(OpenFileSystemModel model, List<BrowserEntry> entries) {
return entries.stream().allMatch(entry -> isExecutable(entry.getRawFileEntry()));
}
2024-04-01 20:25:06 +13:00
protected CommandBuilder createCommand(ShellControl sc, OpenFileSystemModel model, BrowserEntry entry) {
2024-04-14 04:23:09 +12:00
return CommandBuilder.of()
.add(sc.getShellDialect()
.runScriptCommand(sc, entry.getRawFileEntry().getPath()));
2023-05-21 01:49:58 +12:00
}
}