From b9bbb35a76bbc928163afe4168b3f67b8c7e8608 Mon Sep 17 00:00:00 2001 From: Jacob Strieb Date: Thu, 21 Dec 2023 18:18:28 -0500 Subject: [PATCH] Fixes for better portability to macOS --- just_sh/convert.py | 54 ++++++++++++++++++++++++++++++++++++++-------- test/test.py | 4 +++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/just_sh/convert.py b/just_sh/convert.py index 5b14d71..c7d1d8e 100644 --- a/just_sh/convert.py +++ b/just_sh/convert.py @@ -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 @@ -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 @@ -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 }} @@ -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}}" diff --git a/test/test.py b/test/test.py index 61537bb..30d713d 100644 --- a/test/test.py +++ b/test/test.py @@ -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!"))}} @@ -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"),