Skip to content

WIP: Subtaskfiles #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -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 <task> <args>`

### 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
Expand Down
6 changes: 4 additions & 2 deletions src/components/Generator/GeneredTaskfile/taskfile-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}<task>${RESET} <args>"
echo -e "\n${BLUE}Usage:${RESET} $TASKFILE_FILE ${YELLOW}<task>${RESET} <args>"
}

function task:shorthand { ## Create CLI shorthand task instead of ./Taskfile
Expand Down