From 936095aa24bc0a88159de25feb9bb3e02e966188 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Mon, 5 Dec 2022 00:50:58 +0100 Subject: [PATCH] Rework on shells --- .../java/io/xpipe/core/process/OsType.java | 17 +++++ .../core/process/ShellProcessControl.java | 6 ++ .../java/io/xpipe/core/process/ShellType.java | 6 ++ .../xpipe/core/store/DataStoreFormatter.java | 30 --------- .../io/xpipe/core/util/CoreJacksonModule.java | 2 +- .../extension/DataStoreActionProvider.java | 4 ++ .../extension/prefs/PrefsChoiceValue.java | 9 ++- .../extension/util/DataStoreFormatter.java | 63 +++++++++++++++++++ 8 files changed, 104 insertions(+), 33 deletions(-) delete mode 100644 core/src/main/java/io/xpipe/core/store/DataStoreFormatter.java create mode 100644 extension/src/main/java/io/xpipe/extension/util/DataStoreFormatter.java diff --git a/core/src/main/java/io/xpipe/core/process/OsType.java b/core/src/main/java/io/xpipe/core/process/OsType.java index 5322d286b..8ca26305a 100644 --- a/core/src/main/java/io/xpipe/core/process/OsType.java +++ b/core/src/main/java/io/xpipe/core/process/OsType.java @@ -11,6 +11,8 @@ public interface OsType { Linux LINUX = new Linux(); Mac MAC = new Mac(); + String getScriptFileEnding(); + String getName(); String getTempDirectory(ShellProcessControl pc) throws Exception; @@ -40,6 +42,11 @@ public interface OsType { static class Windows implements OsType { + @Override + public String getScriptFileEnding() { + return "bat"; + } + @Override public String getName() { return "Windows"; @@ -98,6 +105,11 @@ public interface OsType { return String.join("/", file.split("[\\\\/]+")); } + @Override + public String getScriptFileEnding() { + return "sh"; + } + @Override public String getName() { return "Linux"; @@ -168,6 +180,11 @@ public interface OsType { return String.join("/", file.split("[\\\\/]+")); } + @Override + public String getScriptFileEnding() { + return "sh"; + } + @Override public String getName() { return "Mac"; diff --git a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java index a474d76a4..0bc2ec2a5 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -11,6 +11,12 @@ import java.util.stream.Collectors; public interface ShellProcessControl extends ProcessControl { + default String prepareOpen() throws Exception { + return prepareOpen(null); + } + + String prepareOpen(String content) throws Exception; + default String executeSimpleCommand(String command) throws Exception { try (CommandProcessControl c = command(command).start()) { return c.readOrThrow(); diff --git a/core/src/main/java/io/xpipe/core/process/ShellType.java b/core/src/main/java/io/xpipe/core/process/ShellType.java index 1e20577e3..ae80fcc3a 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellType.java +++ b/core/src/main/java/io/xpipe/core/process/ShellType.java @@ -10,6 +10,10 @@ import java.util.stream.Collectors; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") public interface ShellType { + String createInitFileContent(String command); + + List getOpenWithInitFileCommand(String file); + default String flatten(List command) { return command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")); } @@ -65,5 +69,7 @@ public interface ShellType { String getDisplayName(); + String getExecutable(); + boolean echoesInput(); } diff --git a/core/src/main/java/io/xpipe/core/store/DataStoreFormatter.java b/core/src/main/java/io/xpipe/core/store/DataStoreFormatter.java deleted file mode 100644 index b3779a9f6..000000000 --- a/core/src/main/java/io/xpipe/core/store/DataStoreFormatter.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.xpipe.core.store; - -public class DataStoreFormatter { - - public static String ellipsis(String input, int length) { - if (input == null) { - return ""; - } - - var end = Math.min(input.length(), length); - if (end < input.length()) { - return input.substring(0, end) + "..."; - } - return input; - } - - public static String specialFormatHostName(String input) { - if (input.contains(":")) { - input = input.split(":")[0]; - } - - if (input.endsWith(".rds.amazonaws.com")) { - var split = input.split("\\."); - var name = split[0]; - var region = split[2]; - return String.format("RDS %s @ %s", name, region); - } - return null; - } -} diff --git a/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java b/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java index 2f73771db..5d65b9ea5 100644 --- a/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java +++ b/core/src/main/java/io/xpipe/core/util/CoreJacksonModule.java @@ -46,7 +46,7 @@ public class CoreJacksonModule extends SimpleModule { new NamedType(WildcardType.class), new NamedType(ShellTypes.Cmd.class), new NamedType(ShellTypes.PowerShell.class), - new NamedType(ShellTypes.Sh.class), + new NamedType(ShellTypes.PosixBase.class), new NamedType(BaseQueryElement.class), new NamedType(ChoiceElement.class), new NamedType(BusyElement.class), diff --git a/extension/src/main/java/io/xpipe/extension/DataStoreActionProvider.java b/extension/src/main/java/io/xpipe/extension/DataStoreActionProvider.java index 3eb0db74b..7f1cb58c7 100644 --- a/extension/src/main/java/io/xpipe/extension/DataStoreActionProvider.java +++ b/extension/src/main/java/io/xpipe/extension/DataStoreActionProvider.java @@ -31,6 +31,10 @@ public interface DataStoreActionProvider { Class getApplicableClass(); + default boolean isMajor() { + return false; + } + default boolean isActive() throws Exception { return true; } diff --git a/extension/src/main/java/io/xpipe/extension/prefs/PrefsChoiceValue.java b/extension/src/main/java/io/xpipe/extension/prefs/PrefsChoiceValue.java index ac2787daa..12722bfa8 100644 --- a/extension/src/main/java/io/xpipe/extension/prefs/PrefsChoiceValue.java +++ b/extension/src/main/java/io/xpipe/extension/prefs/PrefsChoiceValue.java @@ -30,11 +30,16 @@ public interface PrefsChoiceValue extends Translatable { } @SuppressWarnings("unchecked") - static List getSupported(Class type) { + static List getSupported(Class type) { try { return (List) type.getDeclaredField("SUPPORTED").get(null); } catch (IllegalAccessException | NoSuchFieldException e) { - return getAll(type).stream().filter(t -> t.isSupported()).toList(); + var all = getAll(type); + if (all == null) { + throw new AssertionError(); + } + + return all.stream().filter(t -> ((PrefsChoiceValue)t).isSupported()).toList(); } } diff --git a/extension/src/main/java/io/xpipe/extension/util/DataStoreFormatter.java b/extension/src/main/java/io/xpipe/extension/util/DataStoreFormatter.java new file mode 100644 index 000000000..437d8fdd2 --- /dev/null +++ b/extension/src/main/java/io/xpipe/extension/util/DataStoreFormatter.java @@ -0,0 +1,63 @@ +package io.xpipe.extension.util; + +import io.xpipe.core.store.DataStore; +import io.xpipe.core.store.ShellStore; +import io.xpipe.extension.DataStoreProviders; + +import java.util.function.IntFunction; + +public class DataStoreFormatter { + + public static String formatAtHost(IntFunction func, DataStore at, int length) { + var atString = at instanceof ShellStore shellStore && !ShellStore.isLocal(shellStore) + ? DataStoreProviders.byStore(at).toSummaryString(at, length) + : null; + if (atString == null) { + return func.apply(length); + } + + var fileString = func.apply(length - atString.length() - 3); + return String.format("%s @ %s", fileString, atString); + } + + public static String format(DataStore input, int length) { + var named = XPipeDaemon.getInstance().getStoreName(input); + if (named.isPresent()) { + return cut(named.get(), length); + } + + return DataStoreProviders.byStore(input).toSummaryString(input, length); + } + + public static String cut(String input, int length) { + if (input == null) { + return ""; + } + + var end = Math.min(input.length(), length); + if (end < input.length()) { + return input.substring(0, end) + "..."; + } + return input; + } + + public static String formatHostName(String input, int length) { + // Remove port + if (input.contains(":")) { + input = input.split(":")[0]; + } + + // Check for amazon web services + if (input.endsWith(".rds.amazonaws.com")) { + var split = input.split("\\."); + var name = split[0]; + var region = split[2]; + var lengthShare = (length - 3) / 2; + return String.format( + "%s @ %s", + DataStoreFormatter.cut(name, lengthShare), DataStoreFormatter.cut(region, length - lengthShare)); + } + + return cut(input, length); + } +}