Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
feat: update format of command filenames to be more readable
Browse files Browse the repository at this point in the history
Instead of 'build-NodeJS_Server-after.sh', it is 'NodeJS_Server.build-after.sh'
  • Loading branch information
hyperupcall committed May 10, 2021
1 parent 4f1893a commit e47014e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 69 deletions.
11 changes: 6 additions & 5 deletions lib/do.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
}
58 changes: 26 additions & 32 deletions lib/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,81 +100,74 @@ 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
done
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
Expand All @@ -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
Expand Down
65 changes: 33 additions & 32 deletions tests/helper.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e47014e

Please sign in to comment.