From 42deba76b94dba4d0e11c05e508fa19fb9fdb260 Mon Sep 17 00:00:00 2001 From: Gustavo Dutra Date: Mon, 23 Feb 2015 15:10:43 -0300 Subject: [PATCH 1/2] Add `b.path.matching` function Allows globing files recursively by a given criteria. --- modules/core.sh | 8 ++++++++ modules/path.sh | 7 +++++++ modules/task.sh | 14 ++------------ tests/modules/path_test.sh | 13 +++++++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/modules/core.sh b/modules/core.sh index 82d9d6a..ff5e50c 100644 --- a/modules/core.sh +++ b/modules/core.sh @@ -167,6 +167,14 @@ function b.resolve_path () { return 1 } +## Loads all files matching `*.sh` under the given pathname +## @param path - the root path +function b.preload () { + for file_path in $(b.path.matching "$1" "*.sh"); do + source "$file_path" + done +} + ## Check if a given dependency is executable. ## ## In case it is not raises `DependencyNotMetException`. diff --git a/modules/path.sh b/modules/path.sh index 234e9bf..8b6dc6f 100644 --- a/modules/path.sh +++ b/modules/path.sh @@ -65,3 +65,10 @@ function b.path.older? () { function b.path.newer? () { test "$1" -nt "$2" } + +## Recursively finds files matching the given criteria +## @param path - the root path to start the search at +## @param criteria - a criteria to search files for +function b.path.matching () { + find "$1" -name "$2" +} diff --git a/modules/task.sh b/modules/task.sh index 3358cc2..f659216 100644 --- a/modules/task.sh +++ b/modules/task.sh @@ -1,5 +1,3 @@ -_BANG_TASK_DIRS=(./tasks "$_BANG_PATH/tasks") - ## Adds a new task. It is possible to add a description which is used when ## describing it. ## @param name - the name of the task @@ -21,9 +19,7 @@ function b.task.run () { shift if b.task.exists? "$task"; then - local task_path="$(b.task.resolve_path $task)" - source "$task_path" - "btask.$task.run" "$@" + "btask.${task}.run" "$@" else b.raise TaskNotKnown "Task '$task' is unknown" fi @@ -32,11 +28,5 @@ function b.task.run () { ## Checks whether a task is loaded ## @param task - the name of the task function b.task.exists? () { - b.task.resolve_path "$1" &> /dev/null -} - -## Resolves a given task name to its filename -## @param task - the name of the task -function b.task.resolve_path () { - b.resolve_path $1 "${_BANG_TASK_DIRS[@]}" + declare -f "btask.${1}.run" &> /dev/null } diff --git a/tests/modules/path_test.sh b/tests/modules/path_test.sh index 7489ce4..718fd2b 100644 --- a/tests/modules/path_test.sh +++ b/tests/modules/path_test.sh @@ -32,3 +32,16 @@ function b.test.path_expand () { unlink "${dir}2" } + +function b.test.path_matching () { + local dir="$(mktemp -d -t preload.source.XXXX)" + touch "$dir/file1.sh" + touch "$dir/file2.sh" + local files="$(b.path.matching "$dir" "file*")" + + echo "$files" | grep -q 'file1.sh' + b.unittest.assert_success $? + + echo "$files" | grep -q 'file2.sh' + b.unittest.assert_success $? +} From 039c795ba9f39b28f77bd83970636fe6f64f6553 Mon Sep 17 00:00:00 2001 From: Gustavo Dutra Date: Mon, 23 Feb 2015 15:19:54 -0300 Subject: [PATCH 2/2] Preload all the modules and tasks before running it --- bang | 1 + tasks/run.sh | 10 ++-------- tasks/test.sh | 4 ++-- tests/bang_test.sh | 15 +++++++++++++++ tests/bin/tasks/run_test.sh | 1 - tests/modules/task_test.sh | 8 +------- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/bang b/bang index ffeec36..b3ecc79 100755 --- a/bang +++ b/bang @@ -21,6 +21,7 @@ b.module.require 'task' b.set bang.src_path "$_BANG_PATH" b.set bang.working_dir "$PWD" +b.preload "$_BANG_PATH/tasks" task="$1" shift diff --git a/tasks/run.sh b/tasks/run.sh index 2674952..a6365de 100644 --- a/tasks/run.sh +++ b/tasks/run.sh @@ -12,17 +12,11 @@ function btask.run.run () { local base_path="$(dirname "$file")" # Preloading modules and tasks - b.path.dir? "$base_path/modules" && _load_bash_files_from "$base_path/modules" - b.path.dir? "$base_path/tasks" && _load_bash_files_from "$base_path/tasks" + b.path.dir? "$base_path/modules" && b.preload "$base_path/modules" + b.path.dir? "$base_path/tasks" && b.preload "$base_path/tasks" . "$file" else b.abort "Could not run '$file' because it is not a file." fi } - -function _load_bash_files_from () { - for file_path in $(find "$1" -name '*.sh'); do - source "$file_path" - done -} diff --git a/tasks/test.sh b/tasks/test.sh index 444b54a..56925e4 100644 --- a/tasks/test.sh +++ b/tasks/test.sh @@ -68,8 +68,8 @@ function btask.test.run () { green="\e[32m" red="\e[91m" reset="\e[0;0m" fi - b.module.prepend_lookup_dir "$base_path/modules" - _BANG_TASK_DIRS=("$base_path/tasks") + b.path.dir? "$base_path/modules" && b.preload "$base_path/modules" + b.path.dir? "$base_path/tasks" && b.preload "$base_path/tasks" local files_to_be_tested="$(_expand_into_files ${files:-$base_path/tests})" diff --git a/tests/bang_test.sh b/tests/bang_test.sh index 1a706e6..330e559 100644 --- a/tests/bang_test.sh +++ b/tests/bang_test.sh @@ -103,3 +103,18 @@ function b.test.depends_on_returns_true_when_command_is_executable () { b.unittest.assert_true $? } + +function b.test.preload_sources_files () { + local dir="$(mktemp -d -t preload.source.XXXX)" + echo 'function this_is_a_preloaded_function () { echo ; }' > "$dir/file.sh" + + declare -f this_is_a_preloaded_function &> /dev/null + b.unittest.assert_error $? + + b.preload "$dir" + + declare -f this_is_a_preloaded_function &> /dev/null + b.unittest.assert_success $? + + rm -r "$dir" +} diff --git a/tests/bin/tasks/run_test.sh b/tests/bin/tasks/run_test.sh index ad48d06..05e25cc 100644 --- a/tests/bin/tasks/run_test.sh +++ b/tests/bin/tasks/run_test.sh @@ -1,5 +1,4 @@ function b.test.task_run () { - btask.run.run lero local output=$(b.task.run run fixtures/tests/test_run_file.sh) assert_equal 'bang is loaded' "$output" diff --git a/tests/modules/task_test.sh b/tests/modules/task_test.sh index e577507..1d54d63 100644 --- a/tests/modules/task_test.sh +++ b/tests/modules/task_test.sh @@ -23,17 +23,11 @@ function b.test.it_adds_the_task_when_task_exist () { } function b.test.it_runs_added_tasks () { - function a_path () { echo "/path/to/task-$1.sh" ; } - b.unittest.double.do b.task.resolve_path a_path - - local resolved_path="" func_runned=0 - function source () { resolved_path="$1" ; } + local func_runned=0 function btask.google.run () { func_runned=1 ; } - b.task.add 'google' 'Googles me something' b.task.run 'google' - b.unittest.assert_equal "/path/to/task-google.sh" "$resolved_path" b.unittest.assert_equal "1" "$func_runned" }