Skip to content

Instantly share code, notes, and snippets.

@CodingNinja
Last active April 9, 2018 01:17
Show Gist options
  • Save CodingNinja/c736e6cff18a4b37322558b6bb9dd93e to your computer and use it in GitHub Desktop.
Save CodingNinja/c736e6cff18a4b37322558b6bb9dd93e to your computer and use it in GitHub Desktop.
Argbash Interpreter

Simple wrapper around argbash to enable Argbash scripts without the need for directly compiling as changes are made

A shasum of the script is generated when run and this is compared with the exxisting files hash, if it's changed, we compile the script with argbash, save it to the cache folder and run the rebuilt file

#!/usr/bin/env bash
#
set -eu
SHA_BIN=shasum
if [[ `which shasum` == "" ]]; then
SHA_BIN="sha256sum"
fi
SCRIPT_HOME="~"
CACHE_PATH="$SCRIPT_HOME/bin/.argbash-cache"
SCRIPT_NAME=`basename "$1"`
SCRIPT_PATH="`dirname "$1"`/$SCRIPT_NAME"
SCRIPT_CONTENT=`cat "$SCRIPT_PATH"`
NAME_HASH=`echo "$SCRIPT_PATH" | $SHA_BIN | awk '{print \$1}'`
SCRIPT_HASH=`$SHA_BIN "$SCRIPT_PATH" | awk '{print \$1}'`
CACHE_FILE="$CACHE_PATH/${SCRIPT_NAME}-${NAME_HASH}"
DEBUG="${DEBUG_INTERPRETER:-}"
_debug_interpreter()
{
if [[ "$DEBUG" != "" ]]; then
echo $@
fi
}
if [ ! -d "$CACHE_PATH" ]; then
DEBUG="true"
echo "Creating cache path"
mkdir $CACHE_PATH
fi
if [ -f "$CACHE_FILE".shasum ]; then
_debug_interpreter "Existing checksum found, comparing hashes"
OLD_SCRIPT_HASH=`cat "$CACHE_FILE".shasum`
if [[ "$OLD_SCRIPT_HASH" != "$SCRIPT_HASH" ]]; then
_debug_interpreter "Existing hash does not match, removing old files"
rm -f "$CACHE_FILE" "$CACHE_FILE.shasum" "$CACHE_PATH/$OLD_SCRIPT_HASH" "$CACHE_PATH/$OLD_SCRIPT_HASH.shasum"
else
_debug_interpreter "Hashes matched, no compilation required"
fi
else
if [ -f "$CACHE_FILE" ]; then
_debug_interpreter "A cache file exists even though there is no checksum, deleting"
rm "$CACHE_FILE"
fi
fi
if [ ! -f "$CACHE_FILE" ]; then
_debug_interpreter "Rebuilding script $SCRIPT_PATH, caching to $CACHE_FILE"
argbash "$SCRIPT_PATH" | sed -e "s~argbash-interpreter~bash~" > "$CACHE_FILE"
echo "$SCRIPT_HASH" > "$CACHE_FILE".shasum
chmod +x "$CACHE_FILE"
fi
_debug_interpreter "
"
$CACHE_FILE "${@:2}"
#!/usr/bin/env argbash-interpreter
#
# m4_ignore(
echo "This is just a script template, not the script (yet) - pass it to 'argbash' to fix this." >&2
exit 11 #)Created by argbash-init v2.3.0
#
#
# ARG_OPTIONAL_BOOLEAN([debug],[d],[Print commands that are run?], [off])
# ARG_HELP([The general script's help msg])
# ARGBASH_GO
# [ <-- needed because of Argbash
set -eu
if [[ $_arg_debug == "on" ]]; then
set -x
fi
# ] <-- needed because of Argbash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment