Skip to content

Instantly share code, notes, and snippets.

@nazfox
Last active February 20, 2022 12:41
Show Gist options
  • Save nazfox/d7e9a10206c13357e86cb0ef1ef61d49 to your computer and use it in GitHub Desktop.
Save nazfox/d7e9a10206c13357e86cb0ef1ef61d49 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
#===========================================================
#
# This is a task runner.
# To learn how to use this, try running `./task help` first.
#
#===========================================================
#===========================================================
# VARIABLES
#===========================================================
this_file="$0"
command_max_char="20"
notebook_dir="./notebooks"
tensorboard_dir="./experiments/logs"
#===========================================================
# UTILITIES
#===========================================================
function find_command() {
type "$1" >/dev/null 2>&1
}
function pipenv_error() {
echo "Can't find pipenv. See https://pipenv.pypa.io/en/latest/install/"
exit 1
}
function build_command_help() {
white_spaces=$(printf "%""$command_max_char""s")
awk_code=' \
{ \
if ($0 ~ /^#\?/) { \
if (help_message) { \
help_message = help_message "\n'"$white_spaces"' "; \
} \
help_message = help_message substr($0, 3); \
} else if ($0 ~ /^function [a-zA-Z_0-9]+()/) { \
cmd_len = index($0, "()") - 10; \
target = substr($0, 10, cmd_len); \
if (help_message) { \
printf " %-'"$command_max_char"'s %s\n", target, help_message; \
help_message = ""; \
} \
} else { \
help_message = ""; \
} \
}'
awk "$awk_code" "$this_file"
}
#===========================================================
# COMMANDS
#===========================================================
#? Launch the jupyterlab.
function jupyterlab() {
pipenv run jupyter-lab --notebook-dir=$notebook_dir
}
#? Launch the tensorboard.
function tensorboard() {
pipenv run tensorboard --logdir $tensorboard_dr
}
#? Launch the python interpreter.
function python() {
pipenv run python
}
#? Install the dependencies using pipenv.
function install() {
find_command pipenv && pipenv install || pipenv_error
}
#? Remove the pipenv environment.
function uninstall() {
pipenv --rm
}
#? Display this message.
function help() {
echo "Usage:"
echo " ./task [command] ..."
echo ""
echo "Available commands:"
echo "$(build_command_help)"
}
[[ -z $@ ]] && help || $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment