Skip to content

Commit

Permalink
Fixes for better portability to macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrieb committed Dec 21, 2023
1 parent 218caf8 commit b9bbb35
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
54 changes: 45 additions & 9 deletions just_sh/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def expression_to_string(expression: ExpressionType, depth: int = 0) -> str:
elif type python3 > /dev/null 2>&1; then
python3 -c 'from hashlib import sha256; import sys; print(sha256(sys.stdin.buffer.read()).hexdigest())' \
< "${1}"
elif type python > /dev/null 2>&1; then
python -c 'from hashlib import sha256; import sys; print sha256(sys.stdin.read()).hexdigest()' \
< "${1}"
else
echo_error "No sha256sum binary found"
exit 1
Expand All @@ -285,6 +288,9 @@ def expression_to_string(expression: ExpressionType, depth: int = 0) -> str:
elif type python3 > /dev/null 2>&1; then
printf "%s" "${1}" | \
python3 -c 'from hashlib import sha256; import sys; print(sha256(sys.stdin.buffer.read()).hexdigest())'
elif type python > /dev/null 2>&1; then
printf "%s" "${1}" | \
python3 -c 'from hashlib import sha256; import sys; print sha256(sys.stdin.read()).hexdigest()'
else
echo_error "No sha256sum binary found"
exit 1
Expand Down Expand Up @@ -1463,6 +1469,45 @@ def helper_functions() -> str:
return f"""
{header_comment("Helper functions")}
# Sane, portable echo that doesn't escape characters like "\\n" behind your back
echo() {{
if [ "${{#}}" -gt 0 ]; then
printf "%s\\n" "${{@}}"
else
printf "\\n"
fi
}}
# realpath is a GNU coreutils extension
realpath() {{
# The methods to replicate it get increasingly error-prone
# TODO: improve
if type -P realpath > /dev/null 2>&1; then
"$(type -P realpath)" "${{1}}"
elif type python3 > /dev/null 2>&1; then
python3 -c 'import os.path, sys; print(os.path.realpath(sys.argv[1]))' "${{1}}"
elif type python > /dev/null 2>&1; then
python -c 'import os.path, sys; print os.path.realpath(sys.argv[1])' "${{1}}"
elif [ -f "${{1}}" ] && ! [ -z "$(dirname "${{1}}")" ]; then
# We assume the directory exists. For our uses, it always does
echo "$(
cd "$(dirname "${{1}}")";
pwd -P
)/$(
basename "${{1}}"
)"
elif [ -f "${{1}}" ]; then
pwd -P
elif [ -d "${{1}}" ]; then
(
cd "${{1}}"
pwd -P
)
else
echo "${{1}}"
fi
}}
echo_error() {{
echo "${{RED}}error${{NOCOLOR}}: ${{BOLD}}${{1}}${{NOCOLOR}}" >&2
}}
Expand All @@ -1480,15 +1525,6 @@ def helper_functions() -> str:
echo_recipe_line() {{
echo "${{BOLD}}${{1}}${{NOCOLOR}}" >&2
}}
# Sane, portable echo that doesn't escape characters like "\\n" behind your back
echo() {{
if [ "${{#}}" -gt 0 ]; then
printf "%s\\n" "${{@}}"
else
printf "\\n"
fi
}}
set_var() {{
export "VAR_${{1}}=${{2}}"
Expand Down
4 changes: 3 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def run_justfile(args: Iterable[str]) -> None:
echo "{{ if home_dir == home_dir_2 { "$(echo equal)" } else { "unequal!" } }}"
echo "{{env_var_or_default('ThIs_DoEs_NoT_eXiSt', 'FAKE!!!')}}"
echo "{{invocation_directory()}}"
echo "{{justfile_directory()}}+{{justfile()}} = {{justfile_directory() / justfile()}}"
echo "{{justfile_directory()}} + {{justfile()}} = {{justfile_directory() / justfile()}}"
echo "Alternatively, {{justfile_directory() + justfile()}}"
echo {{quote("I'd've should've quoted this!")}}
echo {{uppercase(quote("I'd've should've quoted this!"))}}
Expand Down Expand Up @@ -1145,6 +1145,8 @@ def test_evalute_without_quoting() -> None:
(re.compile(rb" +"), rb" "),
(re.compile(rb'"'), rb"'"),
(re.compile(rb"tmp\.[a-zA-Z0-9]+"), rb"tmp"),
(re.compile(rb"/var/folder[^\s]+"), rb"tmp"), # macOS temp directories
(re.compile(rb"/[^\s]*tmp[^\s]+"), rb"tmp"),
(re.compile(rb"\d{1,2}:\d{1,2}"), rb"12:00"), # Normalize times
(re.compile(rb"\nDid you mean[^\n]*"), rb""),
(re.compile(rb"which wasn't expected"), rb"that wasn't expected"),
Expand Down

0 comments on commit b9bbb35

Please sign in to comment.