From 9c855fa4f5ecc728fa4cf1076ee2e11bad74ac5b Mon Sep 17 00:00:00 2001 From: Isaac Schaaf Date: Sat, 12 Jan 2019 15:04:32 -0800 Subject: [PATCH 1/4] changed BAKE_TASKS array to store task_name->task_function. Added bake_task_alias function for creating tasks where the task_name and function_name are different --- bake | 56 +++++++++++++++++++++++++++++++++++------------- sandbox/Bakefile | 18 ++++++++++++++++ 2 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 sandbox/Bakefile diff --git a/bake b/bake index 3e7eb6c..94a26c3 100755 --- a/bake +++ b/bake @@ -264,7 +264,21 @@ function bake_task () { echo "Error[bake_task]: 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 + echo "Error[bake_task_alias]: you must supply a task name and function name!" + return 1 + fi + BAKE_TASKS[$name]="$fn_name" BAKE_TASK_DESCRIPTIONS[$name]="$short_desc" } @@ -284,9 +298,9 @@ function bake_subtask () { 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" } @@ -299,7 +313,7 @@ function bake_supertask () { 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 () { @@ -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..21e5a88 --- /dev/null +++ b/sandbox/Bakefile @@ -0,0 +1,18 @@ +#!/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! $*" +} + +# Alias the "run.my.program" function to "run" +bake_task_alias run run.my.program "Run the program [alias]" From a721af8c929393040d981a995ddc37ce22dca21e Mon Sep 17 00:00:00 2001 From: Isaac Schaaf Date: Sun, 13 Jan 2019 10:41:57 -0800 Subject: [PATCH 2/4] added example of bake task using a builtin bash command name --- sandbox/Bakefile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sandbox/Bakefile b/sandbox/Bakefile index 21e5a88..4bef058 100644 --- a/sandbox/Bakefile +++ b/sandbox/Bakefile @@ -14,5 +14,14 @@ 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" From adb8befbfc6922014c0a9cc9ad335264ba343ab2 Mon Sep 17 00:00:00 2001 From: Isaac Schaaf Date: Sun, 13 Jan 2019 16:04:47 -0800 Subject: [PATCH 3/4] changed all internal errors to use bake_log --- bake | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bake b/bake index 94a26c3..0e926b8 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,7 +261,7 @@ 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]="$name" @@ -275,7 +275,7 @@ function bake_task_alias () { fn_name="${2:-}" local short_desc="${3:-No Description for task: $name}" if [ -z "$name" ] || [ -z "$fn_name" ]; then - echo "Error[bake_task_alias]: you must supply a task name and function name!" + bake_log_fatal "you must supply a task name and function name!" return 1 fi BAKE_TASKS[$name]="$fn_name" @@ -288,12 +288,12 @@ 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 @@ -309,7 +309,7 @@ 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 @@ -583,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 From a55309a80b1e8a38a4b72629fdeb7668d5095be0 Mon Sep 17 00:00:00 2001 From: Isaac Schaaf Date: Sun, 13 Jan 2019 16:06:38 -0800 Subject: [PATCH 4/4] cleaned up conditional to use double brackets --- bake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bake b/bake index 0e926b8..f151a7f 100755 --- a/bake +++ b/bake @@ -274,7 +274,7 @@ function bake_task_alias () { name="${1:-}" fn_name="${2:-}" local short_desc="${3:-No Description for task: $name}" - if [ -z "$name" ] || [ -z "$fn_name" ]; then + if [[ -z "$name" || -z "$fn_name" ]]; then bake_log_fatal "you must supply a task name and function name!" return 1 fi