From e47014ef98e6689397873e006c14d9ee5dcf2734 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Mon, 10 May 2021 08:45:21 -0700 Subject: [PATCH] feat: update format of command filenames to be more readable Instead of 'build-NodeJS_Server-after.sh', it is 'NodeJS_Server.build-after.sh' --- lib/do.sh | 11 ++++---- lib/helper.sh | 58 +++++++++++++++++++----------------------- tests/helper.bats | 65 ++++++++++++++++++++++++----------------------- 3 files changed, 65 insertions(+), 69 deletions(-) diff --git a/lib/do.sh b/lib/do.sh index 6f64557..9aa494d 100644 --- a/lib/do.sh +++ b/lib/do.sh @@ -72,7 +72,7 @@ doCmd() { # source the configuration file local glueFile="$WD/glue.sh" - helper_source_config "$glueFile" # exposes: languages + helper_source_config "$glueFile" # exposes: using # *this* task is the specific task like 'build', 'ci', etc., even tough # we still call $1 a 'task' @@ -84,13 +84,14 @@ doCmd() { if [[ -z $projectType ]]; then # no specific language on cli. run all specified languages # as per config - [[ -z $languages ]] && { - die "Please set 'languages' in the Glue project configuration (glue.sh)" + [[ ! -v using ]] && { + die "Please set 'using' in the Glue project configuration (glue.sh)" return } - helper_get_command_scripts "$task" "$languages" "$commandDir" + + helper_run_task_scripts "$task" "$commandDir" "${using[@]}" else # run only the command specific to a language - helper_get_command_and_lang_scripts "$task" "$projectType" "$commandDir" + helper_run_task_and_projectType_scripts "$task" "$commandDir" "$projectType" fi } diff --git a/lib/helper.sh b/lib/helper.sh index c519016..f08e61a 100644 --- a/lib/helper.sh +++ b/lib/helper.sh @@ -100,31 +100,26 @@ helper_sort_files_by_when() { # run each command that is language-specific. then # run the generic version of a particular command. for each one, # only run the-user command file is one in 'auto' isn't present -helper_get_command_scripts() { - ensure_fn_args 'helper_get_command_scripts' '1 2 3' "$@" || return - local subcommand="$1" - local langs="$2" - local dir="$3" - - shopt -q nullglob - shoptExitStatus="$?" - shopt -s nullglob +helper_run_task_scripts() { + ensure_fn_args 'helper_run_task_scripts' '1 2 3' "$@" || return + local task="$1" + local commandDir="$2" + shift; shift local hasRan=no - # prepend hypthen for each language - local newLangs - for l in $langs; do - newLangs+="-$l " + # the blank 'lang' represents a file like 'build-before.sh' or 'build.sh' + local -a projectTypes=() + for projectType; do + projectTypes+=("$projectType.") done - # the blank 'lang' represents a file like 'build-before.sh' or 'build.sh' - for lang in $newLangs ''; do + for projectType in "${projectTypes[@]}" ''; do # a blank 'when' represents a file like 'build-go.sh' or 'build.sh' for when in -before '' -after; do # this either runs the 'auto' script or the user-override, depending # on whether which ones are present - helper_run_a_relevant_script "$subcommand" "$dir" "$lang" "$when" + helper_run_a_relevant_script "$task" "$commandDir" "$projectType" "$when" if [[ $? -eq 0 ]]; then hasRan=yes fi @@ -132,49 +127,47 @@ helper_get_command_scripts() { done if [[ $hasRan == no ]]; then - die "Particular subcommand '$subcommand' did not run any files" + die "Task '$task' did not match any files in directory '$commandDir'" fi - - (( shoptExitStatus != 0 )) && shopt -u nullglob } # only run a language specific version of a command -helper_get_command_and_lang_scripts() { - ensure_fn_args 'helper_get_command_and_lang_scripts' '1 2 3' "$@" || return - local subcommand="$1" - local lang="$2" - local dir="$3" +helper_run_task_and_projectType_scripts() { + ensure_fn_args 'helper_run_task_and_projectType_scripts' '1 2 3' "$@" || return + local task="$1" + local commandDir="$2" + local projectType="$3" local hasRan=no for when in -before '' -after; do # this either runs the 'auto' script or the user-override, depending # on whether which ones are present - helper_run_a_relevant_script "$subcommand" "$dir" "-$lang" "$when" + helper_run_a_relevant_script "$task" "$commandDir" "$projectType." "$when" if [[ $? -eq 0 ]]; then hasRan=yes fi done if [[ $hasRan == no ]]; then - die "Particular subcommand '$subcommand' did not run any files" + die "Task '$task' did not match any files in directory '$commandDir'" fi } helper_run_a_relevant_script() { ensure_fn_args 'helper_run_a_relevant_script' '1 2' "$@" || return - local subcommand="$1" - local dir="$2" - local lang="$3" # can be blank + local task="$1" + local commandDir="$2" + local projectType="$3" # can be blank local when="$4" # can be blank shopt -q nullglob shoptExitStatus="$?" - shopt -s nullglob + # TODO: cleanup # run the file, if it exists (override) local hasRanFile=no - for file in "$dir/$subcommand$lang$when".*?; do + for file in "$commandDir/${projectType}${task}${when}".*?; do if [[ $hasRanFile = yes ]]; then log_error "Duplicate file '$file' should not exist" break @@ -187,11 +180,12 @@ helper_run_a_relevant_script() { # we ran the user file, which overrides the auto file # continue to next 'when' by returning if [[ $hasRanFile == yes ]]; then + (( shoptExitStatus != 0 )) && shopt -u nullglob return fi # if no files were ran, run the auto file, if it exists - for file in "$dir/auto/$subcommand$lang$when".*?; do + for file in "$commandDir/auto/${projectType}${task}${when}".*?; do if [[ $hasRanFile = yes ]]; then log_error "Duplicate file '$file' should not exist" break diff --git a/tests/helper.bats b/tests/helper.bats index d6bc26c..bc4f61f 100644 --- a/tests/helper.bats +++ b/tests/helper.bats @@ -100,42 +100,43 @@ source ../lib/util.sh done } -# @test "helper_sort_files_by_when" { - -# # 1 -# local -a input=('build-before.sh' 'build.sh') -# local -a expected=('build-before.sh' 'build.sh') -# local -a result -# readarray -d $'\0' result < <(helper_sort_files_by_when "${input[@]}") - -# # ensure equivalency -# [[ ${#expected[@]} == "${#result[@]}" ]] -# for i in "${!result[@]}"; do -# [[ ${expected[$i]} == "${result[$i]}" ]] -# done +# TODO: dead code +@test "helper_sort_files_by_when" { + + # 1 + local -a input=('build-before.sh' 'build.sh') + local -a expected=('build-before.sh' 'build.sh') + local -a result + readarray -d $'\0' result < <(helper_sort_files_by_when "${input[@]}") + + # ensure equivalency + [[ ${#expected[@]} == "${#result[@]}" ]] + for i in "${!result[@]}"; do + [[ ${expected[$i]} == "${result[$i]}" ]] + done -# # 2 -# local -a input=('build-go-after.sh' 'build-go-before.sh' 'build-go.sh') -# local -a expected=('build-go-before.sh' 'build-go.sh' 'build-go-after.sh') -# local -a result -# readarray -d $'\0' result < <(helper_sort_files_by_when "${input[@]}") + # 2 + local -a input=('build-go-after.sh' 'build-go-before.sh' 'build-go.sh') + local -a expected=('build-go-before.sh' 'build-go.sh' 'build-go-after.sh') + local -a result + readarray -d $'\0' result < <(helper_sort_files_by_when "${input[@]}") -# # ensure equivalency -# [[ ${#expected[@]} == "${#result[@]}" ]] -# for i in "${!result[@]}"; do -# [[ ${expected[$i]} == "${result[$i]}" ]] -# done -# } + # ensure equivalency + [[ ${#expected[@]} == "${#result[@]}" ]] + for i in "${!result[@]}"; do + [[ ${expected[$i]} == "${result[$i]}" ]] + done +} -# @test "helper_get_command_and_lang_scripts" { -# local dir +@test "helper_run_task_and_projectType_scripts" { + local dir -# dir="$PWD/mocks/util-source-commands" + dir="$PWD/mocks/util-source-commands" -# # 1 -# local -a result -# readarray -d $'\0' result < <(helper_get_command_and_lang_scripts "build" "go" "$dir") + # 1 + local -a result + readarray -d $'\0' result < <(helper_run_task_and_projectType_scripts "build" "go" "$dir") -# # echo "${result[@]}" >&3 -# } + # echo "${result[@]}" >&3 +}