Os specific fixes

This commit is contained in:
crschnick 2023-05-19 12:55:16 +00:00
parent 28d76c83f6
commit 288f526019
5 changed files with 67 additions and 57 deletions

View file

@ -17,7 +17,7 @@ import java.util.stream.Stream;
public interface ExternalTerminalType extends PrefsChoiceValue { public interface ExternalTerminalType extends PrefsChoiceValue {
public static final ExternalTerminalType CMD = new SimpleType("app.cmd", "cmd.exe", "cmd.exe") { ExternalTerminalType CMD = new SimpleType("app.cmd", "cmd.exe", "cmd.exe") {
@Override @Override
protected String toCommand(String name, String file) { protected String toCommand(String name, String file) {
@ -30,7 +30,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType POWERSHELL_WINDOWS = ExternalTerminalType POWERSHELL_WINDOWS =
new SimpleType("app.powershell", "powershell", "PowerShell") { new SimpleType("app.powershell", "powershell", "PowerShell") {
@Override @Override
@ -44,13 +44,13 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType PWSH_WINDOWS = new SimpleType("app.pwsh", "pwsh", "PowerShell Core") { ExternalTerminalType PWSH_WINDOWS = new SimpleType("app.pwsh", "pwsh", "PowerShell Core") {
@Override @Override
protected String toCommand(String name, String file) { protected String toCommand(String name, String file) {
// Fix for https://github.com/PowerShell/PowerShell/issues/18530#issuecomment-1325691850 // Fix for https://github.com/PowerShell/PowerShell/issues/18530#issuecomment-1325691850
var script = ScriptHelper.createLocalExecScript("set \"PSModulePath=\"\r\n\"" + file + "\"\npause"); var script = ScriptHelper.createLocalExecScript("set \"PSModulePath=\"\r\n\"" + file + "\"\npause");
return "-ExecutionPolicy Bypass -NoProfile -Command cmd /C '" +script + "'"; return "-ExecutionPolicy Bypass -NoProfile -Command cmd /C '" + script + "'";
} }
@Override @Override
@ -59,7 +59,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType WINDOWS_TERMINAL = ExternalTerminalType WINDOWS_TERMINAL =
new SimpleType("app.windowsTerminal", "wt.exe", "Windows Terminal") { new SimpleType("app.windowsTerminal", "wt.exe", "Windows Terminal") {
@Override @Override
@ -77,7 +77,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType GNOME_TERMINAL = ExternalTerminalType GNOME_TERMINAL =
new SimpleType("app.gnomeTerminal", "gnome-terminal", "Gnome Terminal") { new SimpleType("app.gnomeTerminal", "gnome-terminal", "Gnome Terminal") {
@Override @Override
@ -105,11 +105,12 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType KONSOLE = new SimpleType("app.konsole", "konsole", "Konsole") { ExternalTerminalType KONSOLE = new SimpleType("app.konsole", "konsole", "Konsole") {
@Override @Override
protected String toCommand(String name, String file) { protected String toCommand(String name, String file) {
// Note for later: When debugging konsole launches, it will always open as a child process of IntelliJ/X-Pipe even though we try to detach it. // Note for later: When debugging konsole launches, it will always open as a child process of
// IntelliJ/X-Pipe even though we try to detach it.
// This is not the case for production where it works as expected // This is not the case for production where it works as expected
return "--new-tab -e \"" + file + "\""; return "--new-tab -e \"" + file + "\"";
} }
@ -120,7 +121,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType XFCE = new SimpleType("app.xfce", "xfce4-terminal", "Xfce") { ExternalTerminalType XFCE = new SimpleType("app.xfce", "xfce4-terminal", "Xfce") {
@Override @Override
protected String toCommand(String name, String file) { protected String toCommand(String name, String file) {
@ -133,15 +134,15 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
}; };
public static final ExternalTerminalType MACOS_TERMINAL = new MacOsTerminalType(); ExternalTerminalType MACOS_TERMINAL = new MacOsTerminalType();
public static final ExternalTerminalType ITERM2 = new ITerm2Type(); ExternalTerminalType ITERM2 = new ITerm2Type();
public static final ExternalTerminalType WARP = new WarpType(); ExternalTerminalType WARP = new WarpType();
public static final ExternalTerminalType CUSTOM = new CustomType(); ExternalTerminalType CUSTOM = new CustomType();
public static final List<ExternalTerminalType> ALL = Stream.of( List<ExternalTerminalType> ALL = Stream.of(
WINDOWS_TERMINAL, WINDOWS_TERMINAL,
PWSH_WINDOWS, PWSH_WINDOWS,
POWERSHELL_WINDOWS, POWERSHELL_WINDOWS,
@ -156,7 +157,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
.filter(terminalType -> terminalType.isSelectable()) .filter(terminalType -> terminalType.isSelectable())
.toList(); .toList();
public static ExternalTerminalType getDefault() { static ExternalTerminalType getDefault() {
return ALL.stream() return ALL.stream()
.filter(externalTerminalType -> !externalTerminalType.equals(CUSTOM)) .filter(externalTerminalType -> !externalTerminalType.equals(CUSTOM))
.filter(terminalType -> terminalType.isAvailable()) .filter(terminalType -> terminalType.isAvailable())
@ -164,9 +165,9 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
.orElse(null); .orElse(null);
} }
public abstract void launch(String name, String file, boolean elevated) throws Exception; void launch(String name, String file, boolean elevated) throws Exception;
static class MacOsTerminalType extends ExternalApplicationType.MacApplication implements ExternalTerminalType { class MacOsTerminalType extends ExternalApplicationType.MacApplication implements ExternalTerminalType {
public MacOsTerminalType() { public MacOsTerminalType() {
super("app.macosTerminal", "Terminal"); super("app.macosTerminal", "Terminal");
@ -178,19 +179,18 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
var suffix = file.equals(pc.getShellDialect().getOpenCommand()) var suffix = file.equals(pc.getShellDialect().getOpenCommand())
? "\"\"" ? "\"\""
: "\"" + file.replaceAll("\"", "\\\\\"") + "\""; : "\"" + file.replaceAll("\"", "\\\\\"") + "\"";
var cmd = String.format( pc.osascriptCommand(String.format(
""" """
osascript - "$@" <<EOF
activate application "Terminal" activate application "Terminal"
tell app "Terminal" to do script %s tell app "Terminal" to do script %s
EOF""", """,
suffix); suffix))
pc.executeSimpleCommand(cmd); .execute();
} }
} }
} }
static class CustomType extends ExternalApplicationType implements ExternalTerminalType { class CustomType extends ExternalApplicationType implements ExternalTerminalType {
public CustomType() { public CustomType() {
super("app.custom"); super("app.custom");
@ -226,7 +226,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
} }
static class ITerm2Type extends ExternalApplicationType.MacApplication implements ExternalTerminalType { class ITerm2Type extends ExternalApplicationType.MacApplication implements ExternalTerminalType {
public ITerm2Type() { public ITerm2Type() {
super("app.iterm2", "iTerm"); super("app.iterm2", "iTerm");
@ -235,9 +235,8 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
@Override @Override
public void launch(String name, String file, boolean elevated) throws Exception { public void launch(String name, String file, boolean elevated) throws Exception {
try (ShellControl pc = LocalStore.getShell()) { try (ShellControl pc = LocalStore.getShell()) {
var cmd = String.format( pc.osascriptCommand(String.format(
""" """
osascript - "$@" <<EOF
if application "iTerm" is running then if application "iTerm" is running then
tell application "iTerm" tell application "iTerm"
create window with profile "Default" command "%s" create window with profile "Default" command "%s"
@ -253,14 +252,14 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
end tell end tell
end tell end tell
end if end if
EOF""", """,
file.replaceAll("\"", "\\\\\""), file.replaceAll("\"", "\\\\\"")); file.replaceAll("\"", "\\\\\""), file.replaceAll("\"", "\\\\\"")))
pc.executeSimpleCommand(cmd); .execute();
} }
} }
} }
static class WarpType extends ExternalApplicationType.MacApplication implements ExternalTerminalType { class WarpType extends ExternalApplicationType.MacApplication implements ExternalTerminalType {
public WarpType() { public WarpType() {
super("app.warp", "Warp"); super("app.warp", "Warp");
@ -273,28 +272,26 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
} }
try (ShellControl pc = LocalStore.getShell()) { try (ShellControl pc = LocalStore.getShell()) {
var cmd = String.format( pc.osascriptCommand(String.format(
""" """
osascript - "$@" <<EOF tell application "Warp" to activate
tell application "Warp" to activate tell application "System Events" to tell process "Warp" to keystroke "t" using command down
tell application "System Events" to tell process "Warp" to keystroke "t" using command down delay 1
delay 1 tell application "System Events"
tell application "System Events" tell process "Warp"
tell process "Warp" keystroke "%s"
keystroke "%s" key code 36
key code 36 end tell
end tell end tell
end tell """,
EOF file.replaceAll("\"", "\\\\\"")))
""", .execute();
file.replaceAll("\"", "\\\\\""));
pc.executeSimpleCommand(cmd);
} }
} }
} }
@Getter @Getter
public abstract static class SimpleType extends ExternalApplicationType.PathApplication abstract class SimpleType extends ExternalApplicationType.PathApplication
implements ExternalTerminalType { implements ExternalTerminalType {
private final String displayName; private final String displayName;
@ -308,9 +305,12 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
public void launch(String name, String file, boolean elevated) throws Exception { public void launch(String name, String file, boolean elevated) throws Exception {
if (elevated) { if (elevated) {
if (OsType.getLocal().equals(OsType.WINDOWS)) { if (OsType.getLocal().equals(OsType.WINDOWS)) {
try (ShellControl pc = LocalStore.getShell().subShell(ShellDialects.POWERSHELL).start()) { try (ShellControl pc = LocalStore.getShell()
.subShell(ShellDialects.POWERSHELL)
.start()) {
ApplicationHelper.checkSupport(pc, executable, displayName); ApplicationHelper.checkSupport(pc, executable, displayName);
var toExecute = "Start-Process \"" + executable + "\" -Verb RunAs -ArgumentList \"" + toCommand(name, file).replaceAll("\"", "`\"") + "\""; var toExecute = "Start-Process \"" + executable + "\" -Verb RunAs -ArgumentList \""
+ toCommand(name, file).replaceAll("\"", "`\"") + "\"";
pc.executeSimpleCommand(toExecute); pc.executeSimpleCommand(toExecute);
} }
return; return;

View file

@ -17,8 +17,11 @@ public class MacOsPermissions {
var state = new SimpleBooleanProperty(true); var state = new SimpleBooleanProperty(true);
try (var pc = LocalStore.getShell().start()) { try (var pc = LocalStore.getShell().start()) {
while (state.get()) { while (state.get()) {
var success = pc.executeSimpleBooleanCommand( var success = pc.osascriptCommand(
"osascript -e 'tell application \"System Events\" to keystroke \"t\"'"); """
tell application "System Events" to keystroke "t"
""")
.executeAndCheck();
if (success) { if (success) {
Platform.runLater(() -> { Platform.runLater(() -> {
if (alert.get() != null) { if (alert.get() != null) {

View file

@ -37,7 +37,7 @@ public interface ShellControl extends ProcessControl {
public void checkRunning() throws Exception; public void checkRunning() throws Exception;
default CommandControl osaScript(String script) { default CommandControl osascriptCommand(String script) {
return command(String.format( return command(String.format(
""" """
osascript - "$@" <<EOF osascript - "$@" <<EOF

View file

@ -24,13 +24,16 @@ public class OpenFileWithAction implements LeafAction {
null, null,
"open", "open",
"rundll32.exe", "rundll32.exe",
"shell32.dll,OpenAs_RunDLL " + entries.get(0).getRawFileEntry().getPath(), "shell32.dll,OpenAs_RunDLL "
+ entries.get(0).getRawFileEntry().getPath(),
null, null,
WinUser.SW_SHOWNORMAL); WinUser.SW_SHOWNORMAL);
} }
case OsType.Linux linux -> { case OsType.Linux linux -> {
throw new UnsupportedOperationException();
} }
case OsType.MacOs macOs -> { case OsType.MacOs macOs -> {
throw new UnsupportedOperationException();
} }
} }
} }
@ -47,7 +50,11 @@ public class OpenFileWithAction implements LeafAction {
@Override @Override
public boolean isApplicable(OpenFileSystemModel model, List<FileBrowserEntry> entries) { public boolean isApplicable(OpenFileSystemModel model, List<FileBrowserEntry> entries) {
return entries.size() == 1 && entries.stream().noneMatch(entry -> entry.getRawFileEntry().isDirectory()); var os = model.getFileSystem().getShell();
return os.isPresent()
&& os.get().getOsType().equals(OsType.WINDOWS)
&& entries.size() == 1
&& entries.stream().noneMatch(entry -> entry.getRawFileEntry().isDirectory());
} }
@Override @Override

View file

@ -30,7 +30,7 @@ public class OpenNativeFileDetailsAction implements LeafAction {
} }
case OsType.Linux linux -> {} case OsType.Linux linux -> {}
case OsType.MacOs macOs -> { case OsType.MacOs macOs -> {
sc.osaScript(String.format( sc.osascriptCommand(String.format(
""" """
set fileEntry to (POSIX file "%s") as text set fileEntry to (POSIX file "%s") as text
tell application "Finder" to open information window of file fileEntry tell application "Finder" to open information window of file fileEntry