From fd05f9bb0f376f8b129f210bd16c39386f64aaeb Mon Sep 17 00:00:00 2001 From: crschnick Date: Sat, 4 Feb 2023 08:40:15 +0000 Subject: [PATCH] More fixes for mac --- .../app/prefs/ExternalApplicationType.java | 2 +- dist/licenses/zshi.license | 21 ++++ dist/licenses/zshi.properties | 4 + .../io/xpipe/extension/util/ScriptHelper.java | 116 ++++++++++-------- 4 files changed, 89 insertions(+), 54 deletions(-) create mode 100644 dist/licenses/zshi.license create mode 100644 dist/licenses/zshi.properties diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java index f65cdc12..84004e12 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java @@ -40,7 +40,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { try (ShellProcessControl pc = ShellStore.local().create().start()) { try (var c = pc.command(String.format("osascript -e 'POSIX path of (path to application \"%s\")'", applicationName)).start()) { var path = c.readOnlyStdout(); - if (!c.waitFor()) { + if (c.getExitCode() != 0) { return Optional.empty(); } return Optional.of(Path.of(path)); diff --git a/dist/licenses/zshi.license b/dist/licenses/zshi.license new file mode 100644 index 00000000..84b8367b --- /dev/null +++ b/dist/licenses/zshi.license @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Roman Perepelitsa + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/dist/licenses/zshi.properties b/dist/licenses/zshi.properties new file mode 100644 index 00000000..8f950cab --- /dev/null +++ b/dist/licenses/zshi.properties @@ -0,0 +1,4 @@ +name=zshi +version=1.0 +license=MIT License +link=https://github.com/romkatv/zshi \ No newline at end of file diff --git a/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java b/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java index 4551fcd3..3ab59a01 100644 --- a/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java +++ b/extension/src/main/java/io/xpipe/extension/util/ScriptHelper.java @@ -1,7 +1,6 @@ package io.xpipe.extension.util; import io.xpipe.core.impl.FileNames; -import io.xpipe.core.process.OsType; import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellType; import io.xpipe.core.process.ShellTypes; @@ -16,7 +15,8 @@ import java.util.Random; public class ScriptHelper { public static int getScriptId() { - // A deterministic approach can cause permission problems when two different users execute the same command on a system + // A deterministic approach can cause permission problems when two different users execute the same command on a + // system // Therefore, use a random approach return new Random().nextInt(Integer.MAX_VALUE); } @@ -30,64 +30,78 @@ public class ScriptHelper { private static final String ZSHI = """ - #!/usr/bin/env zsh + #!/usr/bin/env zsh - emulate -L zsh -o no_unset + emulate -L zsh -o no_unset - if (( ARGC == 0 )); then - print -ru2 -- 'Usage: zshi [zsh-flag]... - The same as plain `zsh [zsh-flag]...` except that an additional - gets executed after all standard Zsh startup files - have been sourced.' - return 1 + if (( ARGC == 0 )); then + print -ru2 -- 'Usage: zshi [zsh-flag]... + The same as plain `zsh [zsh-flag]...` except that an additional + gets executed after all standard Zsh startup files + have been sourced.' + return 1 + fi + + () { + local init=$1 + shift + local tmp + { + tmp=$(mktemp -d ${TMPDIR:-/tmp}/zsh.XXXXXXXXXX) || return + local rc + for rc in .zshenv .zprofile .zshrc .zlogin; do + >$tmp/$rc <<<'{ + if (( ${+_zshi_global_rcs} )); then + "builtin" "set" "-o" "global_rcs" + "builtin" "unset" "_zshi_global_rcs" fi - - () { - local init=$1 - shift - local tmp - { - tmp=$(mktemp -d ${TMPDIR:-/tmp}/zsh.XXXXXXXXXX) || return - local rc - for rc in .zshenv .zprofile .zshrc .zlogin; do - >$tmp/$rc <<<'{ - ZDOTDIR="$_zshi_zdotdir" - if [[ -f "$ZDOTDIR/'$rc'" && -r "$ZDOTDIR/'$rc'" ]]; then - "builtin" "source" "--" "$ZDOTDIR/'$rc'" + ZDOTDIR="$_zshi_zdotdir" + # Not .zshenv because /etc/zshenv has already been read + if [[ -o global_rcs && "'$rc'" != ".zshenv" && -f "/etc/'${rc:1}'" && -r "/etc/'${rc:1}'" ]]; then + "builtin" "source" "--" "/etc/'${rc:1}'" + fi + if [[ -f "$ZDOTDIR/'$rc'" && -r "$ZDOTDIR/'$rc'" ]]; then + "builtin" "source" "--" "$ZDOTDIR/'$rc'" + fi + } always { + if [[ -o "no_rcs" || + -o "login" && "'$rc'" == ".zlogin" || + -o "no_login" && "'$rc'" == ".zshrc" || + -o "no_login" && -o "no_interactive" && "'$rc'" == ".zshenv" ]]; then + if (( ${+_zshi_global_rcs} )); then + set -o global_rcs fi + "builtin" "unset" "_zshi_rcs" "_zshi_zdotdir" + "builtin" "command" "rm" "-rf" "--" '${(q)tmp}' + "builtin" "eval" '${(q)init}' + else + if [[ -o global_rcs ]]; then + _zshi_global_rcs= + fi + set -o no_global_rcs + _zshi_zdotdir=${ZDOTDIR:-~} + ZDOTDIR='${(q)tmp}' + fi + }' || return + done + _zshi_zdotdir=${ZDOTDIR:-~} ZDOTDIR=$tmp zsh "$@" } always { - if [[ -o "no_rcs" || - -o "login" && "'$rc'" == ".zlogin" || - -o "no_login" && "'$rc'" == ".zshrc" || - -o "no_login" && -o "no_interactive" && "'$rc'" == ".zshenv" ]]; then - "builtin" "unset" "_zshi_rcs" "_zshi_zdotdir" - "builtin" "command" "rm" "-rf" "--" '${(q)tmp}' - "builtin" "eval" '${(q)init}' - else - _zshi_zdotdir=${ZDOTDIR:-~} - ZDOTDIR='${(q)tmp}' - fi - }' || return - done - _zshi_zdotdir=${ZDOTDIR:-~} ZDOTDIR=$tmp zsh "$@" - } always { - [[ -e $tmp ]] && rm -rf -- $tmp - } - } "$@" + [[ -e $tmp ]] && rm -rf -- $tmp + } + } "$@" """; - public static String constructOpenWithInitScriptCommand(ShellProcessControl processControl, List init, String toExecuteInShell) { + public static String constructOpenWithInitScriptCommand( + ShellProcessControl processControl, List init, String toExecuteInShell) { ShellType t = processControl.getShellType(); if (init.size() == 0 && toExecuteInShell == null) { return t.getNormalOpenCommand(); } String nl = t.getNewLine().getNewLineString(); - var content = String.join(nl, init) - + nl; + var content = String.join(nl, init) + nl; - if (processControl.getOsType().equals(OsType.LINUX) - || processControl.getOsType().equals(OsType.MAC)) { + if (t.equals(ShellTypes.BASH)) { content = "if [ -f ~/.bashrc ]; then . ~/.bashrc; fi\n" + content; } @@ -101,7 +115,7 @@ public class ScriptHelper { if (t.equals(ShellTypes.ZSH)) { var zshiFile = createExecScript(processControl, ZSHI); - return t.getNormalOpenCommand() + " " + zshiFile + " " + initFile; + return t.getNormalOpenCommand() + " \"" + zshiFile + "\" \"" + initFile + "\""; } return t.getInitFileOpenCommand(initFile); @@ -117,9 +131,7 @@ public class ScriptHelper { } @SneakyThrows - private static String createExecScript( - ShellProcessControl processControl, String file, String content - ) { + private static String createExecScript(ShellProcessControl processControl, String file, String content) { ShellType type = processControl.getShellType(); content = type.prepareScriptContent(content); @@ -136,9 +148,7 @@ public class ScriptHelper { } @SneakyThrows - public static String createAskPassScript( - SecretValue pass, ShellProcessControl parent, ShellType type - ) { + public static String createAskPassScript(SecretValue pass, ShellProcessControl parent, ShellType type) { var content = type.getScriptEchoCommand(pass.getSecretValue()); var temp = parent.getTemporaryDirectory(); var file = FileNames.join(temp, "askpass-" + getScriptId() + "." + type.getScriptFileEnding());