More fixes for mac

This commit is contained in:
crschnick 2023-02-04 08:40:15 +00:00
parent ded82617e6
commit fd05f9bb0f
4 changed files with 89 additions and 54 deletions

View file

@ -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));

21
dist/licenses/zshi.license vendored Normal file
View file

@ -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.

4
dist/licenses/zshi.properties vendored Normal file
View file

@ -0,0 +1,4 @@
name=zshi
version=1.0
license=MIT License
link=https://github.com/romkatv/zshi

View file

@ -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 <init-command> [zsh-flag]...
The same as plain `zsh [zsh-flag]...` except that an additional
<init-command> gets executed after all standard Zsh startup files
have been sourced.'
return 1
if (( ARGC == 0 )); then
print -ru2 -- 'Usage: zshi <init-command> [zsh-flag]...
The same as plain `zsh [zsh-flag]...` except that an additional
<init-command> 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<String> init, String toExecuteInShell) {
public static String constructOpenWithInitScriptCommand(
ShellProcessControl processControl, List<String> 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());