From 0aeabe9bdf8fe0cfa9062c7d09666e7abac8b083 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Tue, 22 Oct 2019 10:51:56 -0700 Subject: [PATCH] Fix broken build when missing shfmt --- build.sh | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/build.sh b/build.sh index c15ab6b..86062a0 100755 --- a/build.sh +++ b/build.sh @@ -31,11 +31,27 @@ next() { } # Prints a build step message. +SMSG_CACHE_MSG=() +SMSG_CACHE_META=() +SMSG_EXPECT=1 smsg() { - case "$2" in - "SKIP") printc " %{YELLOW} %{DIM}%s [skipped]%{CLEAR}\n" "$1" 1>&2;; - *) printc " %{YELLOW} %s...%{CLEAR}\n" "$1" 1>&2;; + if [[ "$1" != "$SMSG_EXPECT" ]]; then + SMSG_CACHE_MSG["$1"]="$2" + SMSG_CACHE_META["$1"]="$3" + return; + fi + + ((SMSG_EXPECT++)) + case "$3" in + "SKIP") printc " %{YELLOW} %{DIM}%s [skipped]%{CLEAR}\n" "$2" 1>&2;; + *) printc " %{YELLOW} %s...%{CLEAR}\n" "$2" 1>&2;; esac + + # Cached messages. + echo "${SMSG_CACHE_MSG[$SMSG_EXPECT]}" 1>&2 + if [[ -n "${SMSG_CACHE_MSG[$SMSG_EXPECT]}" ]]; then + smsg "$SMSG_EXPECT" "${SMSG_CACHE_MSG[$SMSG_EXPECT]}" "${SMSG_CACHE_META[$SMSG_EXPECT]}" + fi } # Build step: read @@ -47,7 +63,7 @@ smsg() { # Output: # The file contents. step_read() { - smsg "Reading" + smsg 1 "Reading" cat "$1" } @@ -62,7 +78,7 @@ step_read() { # Output: # The processed file contents. step_preprocess() { - smsg "Preprocessing" + smsg 2 "Preprocessing" local line while IFS='' read -r line; do @@ -111,12 +127,12 @@ step_preprocess() { # The minified file contents. step_minify() { if [[ "$OPT_MINIFY" =~ ^all($|+.*) ]]; then - smsg "Minifying" "SKIP" + smsg 3 "Minifying" "SKIP" cat return 0 fi - smsg "Minifying" + smsg 3 "Minifying" printf "#!/usr/bin/env bash\n" pp_minify | pp_minify_unsafe } @@ -131,7 +147,7 @@ step_minify() { # The compressed self-executable script. step_compress() { if ! "$OPT_COMPRESS"; then - smsg "Compressing" "SKIP" + smsg 4 "Compressing" "SKIP" cat return 0 fi @@ -141,7 +157,7 @@ step_compress() { printf "(exec -a \"\$0\" bash -c 'eval \"\$(cat <&3)\"' \"\$0\" \"\$@\" 3< <(dd bs=1 if=\"\$0\" skip=::: 2>/dev/null | gunzip)); exit \$?;\n" })" - smsg "Compressing" + smsg 4 "Compressing" sed "s/:::/$(wc -c <<< "$wrapper" | bc)/" <<< "$wrapper" gzip } @@ -158,7 +174,7 @@ step_compress() { # Output: # The file contents. step_write() { - smsg "Building" + smsg 5 "Building" tee "$1" chmod +x "$1" } @@ -177,12 +193,12 @@ step_write() { step_write_install() { if [[ "$OPT_INSTALL" != true ]]; then - smsg "Installing" "SKIP" + smsg 6 "Installing" "SKIP" cat return 0 fi - smsg "Installing" + smsg 6 "Installing" tee "$1" chmod +x "$1" } @@ -198,9 +214,14 @@ pp_strip_comments() { # Minify a Bash source file. # https://github.com/mvdan/sh pp_minify() { - shfmt -mn -} + if [[ "$OPT_MINIFY" = "none" ]]; then + cat + return + fi + shfmt -mn + return $? +} # Minifies the output script (unsafely). # Right now, this doesn't do anything.