diff --git a/bake b/bake index 3e7eb6c..f151a7f 100755 --- a/bake +++ b/bake @@ -134,7 +134,7 @@ function bake_require_from_fs () { fi done - echo "Error[bake_require_from_fs]: unable to require $file! (not found on BAKEPATH)" + bake_log_fatal "unable to require $file! (not found on BAKEPATH)" return 1 } @@ -217,7 +217,7 @@ function bake_require_from_url () { if [ -e "$fspath" ]; then # shellcheck disable=SC2064 - trap "bake_echo_red 'Error[bake_require_from_url] loading require: $fspath'" EXIT + trap "bake_log_fatal 'loading require: $fspath'" EXIT source "$fspath" trap EXIT # clear the trap return 0 @@ -227,12 +227,12 @@ function bake_require_from_url () { if [ ! -e "$fspath" ]; then # shellcheck disable=SC2064 - echo "Error[bake_require_from_url]: Sorry, after clone, fech and check out, $fspath still doesn't exist" + bake_log_error "Sorry, after clone, fech and check out, $fspath still doesn't exist" bake_echo_red "Require not found: $url => $fspath" fi # shellcheck disable=SC2064 - trap "bake_echo_red 'Error[bake_require_from_url] loading require: $fspath'" EXIT + trap "bake_log_fatal 'loading require: $fspath'" EXIT source "$fspath" trap EXIT # clear the trap } @@ -261,10 +261,24 @@ function bake_task () { local name="${1:-}" local short_desc="${2:-No Description for task: $name}" if [ -z "$name" ]; then - echo "Error[bake_task]: you must supply a task name!" + bake_log_fatal "you must supply a task name!" return 1 fi - BAKE_TASKS[$name]="ok" + BAKE_TASKS[$name]="$name" + BAKE_TASK_DESCRIPTIONS[$name]="$short_desc" +} + +# bake_task_alias taskname function ["description"] +function bake_task_alias () { + local name fn_name short_desc + name="${1:-}" + fn_name="${2:-}" + local short_desc="${3:-No Description for task: $name}" + if [[ -z "$name" || -z "$fn_name" ]]; then + bake_log_fatal "you must supply a task name and function name!" + return 1 + fi + BAKE_TASKS[$name]="$fn_name" BAKE_TASK_DESCRIPTIONS[$name]="$short_desc" } @@ -274,19 +288,19 @@ function bake_subtask () { local short_desc="${3:-No Description for task: $supertask_name $subtask_name}" if [ -z "$supertask_name" ]; then - echo "Error[bake_subtask]: you must supply a [super] task name!" + bake_log_fatal "you must supply a [super] task name!" return 1 fi if [ -z "$subtask_name" ]; then - echo "Error[bake_subtask]: you must supply a [sub] task name!" + bake_log_fatal "you must supply a [sub] task name!" return 1 fi local tname="$supertask_name$BAKE_TASK_SEP$subtask_name" - BAKE_TASKS[$tname]="ok" - BAKE_SUPERTASKS[$supertask_name]="ok" - BAKE_SUBTASKS[$tname]="ok" + BAKE_TASKS[$tname]="$tname" + BAKE_SUPERTASKS[$supertask_name]="$supertask_name" + BAKE_SUBTASKS[$tname]="$tname" BAKE_TASK_DESCRIPTIONS[$tname]="$short_desc" } @@ -295,11 +309,11 @@ function bake_supertask () { local short_desc="${2:-No Description for task: $supertask_name}" if [ -z "$supertask_name" ]; then - echo "Error[bake_subtask]: you must supply a [super] task name!" + bake_log_fatal "you must supply a [super] task name!" return 1 fi - BAKE_SUPERTASKS[$supertask_name]="ok" + BAKE_SUPERTASKS[$supertask_name]="$supertask_name" BAKE_SUPERTASK_DESCRIPTIONS[$supertask_name]="$short_desc" } @@ -314,22 +328,34 @@ function bake_task_short_desc () { fi } -function bake_is_registered_task () { +function bake_task_function () { local name="$1" local value="${BAKE_TASKS[$name]:-}" - test "$value" = "ok" + echo "$value" } -function bake_is_registered_subtask () { +function bake_subtask_function () { local name="$1" local value="${BAKE_SUBTASKS[$name]:-}" - test "$value" = "ok" + echo "$value" } -function bake_is_registered_supertask () { +function bake_supertask_function () { local name="$1" local value="${BAKE_SUPERTASKS[$name]:-}" - test "$value" = "ok" + echo "$value" +} + +function bake_is_registered_task () { + test -n "$(bake_task_function "$1")" +} + +function bake_is_registered_subtask () { + test -n "$(bake_subtask_function "$1")" +} + +function bake_is_registered_supertask () { + test -n "$(bake_supertask_function "$1")" } function bake_cd () { @@ -557,7 +583,7 @@ function bake_run () { return 0 fi - echo "Error[bake_run]: could not locate a Bakefile!" + bake_log_fatal "could not locate a Bakefile!" echo "" bake_inernal_help return 1 @@ -568,7 +594,7 @@ function bake_run () { if [ -n "$task" ]; then if bake_is_registered_task "$task"; then - $task "$@" + $(bake_task_function "$task") "$@" exit $? fi # TODO: if it's a supertask and no subtask exists, show help for the supertask @@ -577,7 +603,7 @@ function bake_run () { local tname="$task-$1" shift if bake_is_registered_task "$tname"; then - $tname "$@" + $(bake_task_function "$tname") "$@" exit $? fi fi @@ -585,11 +611,11 @@ function bake_run () { fi if [ -n "$BAKE_DEFAULT_TASK" ]; then - "$BAKE_DEFAULT_TASK" + $(bake_task_function "$BAKE_DEFAULT_TASK") "$@" exit $? fi - if [ "$task" == upgrade ]; then + if [ "$task" == "upgrade" ]; then bake_upgrade return 0 fi diff --git a/sandbox/Bakefile b/sandbox/Bakefile new file mode 100644 index 0000000..4bef058 --- /dev/null +++ b/sandbox/Bakefile @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +bake_supertask build "Various tasks for building the project" + +bake_subtask build frontend "build the frontend" +function build-frontend () { + echo "Building the frontend" +} + +# Kinda redundent to include a bake_task declaration +# but this is to show that it does not interfere with aliases +bake_task run.my.program "Run the program" +function run.my.program () { + echo "lets run the program! $*" +} + + +function run.tests () { + echo "Running tests!" +} + +# Alias the "run.my.program" function to "run" +bake_task_alias run run.my.program "Run the program [alias]" + +# Aliases also allows us to create bake tasks with the same name +# as built in functions without overriding them +bake_task_alias test run.tests "Run the tests"