Created
October 5, 2017 14:11
-
-
Save pesterhazy/77c31e741493b194f4ee926d564e3274 to your computer and use it in GitHub Desktop.
hit - git-style interface for project helper scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail && cd "$(dirname "${BASH_SOURCE[0]}")/.." | |
# hit - git-like command dispatcher. Who needs Makefiles? | |
# | |
# Create a directory `bin/` at the top level of your project | |
# directory (the one committed to git). Copy this file into | |
# that folder, call it `hit` and make it executable. | |
# | |
# Type `bin/hit NAME` to run a subcommand called NAME. Subcommands | |
# delegate to executables named `hit-NAME` in the `bin/` folder. | |
# | |
# Type `bin/hit help` to list all available subcommands. | |
# | |
# A subcommand NAME is implemented as an executable file called | |
# `bin/hit-NAME`. Here's on implemented in bash: | |
# | |
# #!/usr/bin/env bash | |
# set -euo pipefail && cd "$(dirname "${BASH_SOURCE[0]}")/.." | |
# ## $DESCRIPTION: Clear build artifacts | |
# rm -rf build | |
# | |
# Note that subcommands can be implemented in any language (python, perl, ...) | |
# | |
# | |
# Best used by adding the `bin/` directory to your PATH by | |
# configuring a per-project-folder PATH in your `.envrc`. See https://direnv.net/ | |
# This allows you to simply type `hit NAME`. | |
# | |
# `hit` automatically changes to the project root folder before | |
# doing anything else so `hit` commands can be run from anywhere | |
# inside the project hierarcyh. | |
bin_dir=bin | |
commands=("$bin_dir/hit-"*) | |
help() { | |
echo 'Syntax: hit <command> [arguments]' | |
echo | |
echo The following commands are available: | |
echo | |
for command in "${commands[@]}"; do | |
name="$(basename $command | sed 's/hit-//')" | |
description=$(perl -ne '/\$DESCRIPTION: (.*)/ && print "$1\n"' < "$command") | |
echo -en " $name" | |
seq -f " " -s '' $((15 - ${#name})) | |
printf "%s\n" "$description" | |
done | |
echo | |
echo " help Show this help" | |
echo | |
} | |
run() { | |
command="${1-}" | |
if [[ "$command" == "" || "$command" == "help" || "$command" == "--help" ]]; then | |
help | |
else | |
shift | |
fname="$bin_dir/hit-$command" | |
if [[ -x "$fname" ]]; then | |
"$fname" "$@" | |
else | |
echo "Command not found: $command" | |
echo | |
help | |
exit 1 | |
fi | |
fi | |
} | |
run "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment