diff --git a/README.md b/README.md index eef7334..f0e186a 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,8 @@ Lines with only a single `#` will not appear as section in `task:help` and can b Running `./Taskfile help`, the `task:help` function is triggered. This task will list all available sections and tasks using the double `##` comments you've learned about above. Now it's clear how you can run any other task! -# Auto-completion +# Advanced +## Auto-completion Autocompletion works when you use `zsh` and `oh-my-zsh`. Create the following file in your oh-my-zsh directory `~/.oh-my-zsh/completions/_task.zsh`: @@ -82,6 +83,50 @@ _task "$@" Now after running `task shorthand`, your `task` commands will get autocompleted. +## SubTaskfiles + +Have a (mono)repo with multiple projects, a group of less-used or specialized tasks or just waaay to many tasks for a +single Taskfile? Splitting your Taskfile might be for you! Using SubTaskfiles allows you to divide your tasks across +multiple files while still calling them from a single one. Most useful for splitting off a group of tasks that can be +logically grouped together, like for specific use-cases or because they are rarely used. + +Example use-cases: git-hooks, frontend- / backend-specific tasks, tasks that fix (infrequently occurring) bugs, etc. + +SubTaskfiles can't be run directly, but are always run "via" a task in the root Taskfile, like this: +`Usage: ./Taskfile foo ` + +### How + +Put this in the root Taskfile: +```shell +function task:foo { ## bar + SUBTASKFILE_DIR="./path/to/subtaskfile/" + + source "$SUBTASKFILE_DIR/SubTaskfile" + + task:"${@-_help}" +} + +# Optional: use proxy-tasks like this for tasks you want to run straight from the root Taskfile +function task:baz { ## Call `foo baz` directly + task:foo baz +} +``` + +Give SubTaskfile the filename `SubTaskfile`. It needs to contain only the tasks and sections you think useful (while +still having access to stuff like `file:ensure` from the root Taskfile!), but it has a few notes: +```shell +# When you refer to files in the subTaskfile's directory, you need prefix them with $SUBTASKFILE_DIR +function task:call-script { ## Call a script + "$SUBTASKFILE_DIR/some-script.sh" +} + +# Without this, you cannot run `./Taskfile foo` or `./Taskfile foo help` +function task:_help { ## Show all available tasks + task:help "$SUBTASKFILE_DIR/SubTaskfile" +} +``` + # Credits This Taskfile setup is based on [Adrian Cooney's Taskfile](https://github.com/adriancooney/Taskfile) and is widely diff --git a/src/components/Generator/GeneredTaskfile/taskfile-base.sh b/src/components/Generator/GeneredTaskfile/taskfile-base.sh index beca5a7..9997d86 100644 --- a/src/components/Generator/GeneredTaskfile/taskfile-base.sh +++ b/src/components/Generator/GeneredTaskfile/taskfile-base.sh @@ -52,13 +52,15 @@ function title { echo -e "\n${BLUE}=>${RESET} $1\n" } +# shellcheck disable=SC2120 function task:help { ## Show all available tasks + TASKFILE_FILE=${1-$0} title "Available tasks" awk 'BEGIN {FS = " { [#][#][ ]?"} /^([a-zA-Z_-]*:?.*)(\{ )?[#][#][ ]?/ \ - {printf "\033[33m%-34s\033[0m %s\n", $1, $2}' $0 |\ + {printf "\033[33m%-34s\033[0m %s\n", $1, $2}' "$TASKFILE_FILE" |\ sed -E "s/[#]{2,}[ ]*/${RESET}/g" |\ sed -E "s/function task:*/ /g" - echo -e "\n${BLUE}Usage:${RESET} ./Taskfile ${YELLOW}${RESET} " + echo -e "\n${BLUE}Usage:${RESET} $TASKFILE_FILE ${YELLOW}${RESET} " } function task:shorthand { ## Create CLI shorthand task instead of ./Taskfile