Skip to content

Add task auto-completion with zsh to the Taskfile setup #22

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

Merged
merged 6 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Generate your own Taskfile at [taskfile.sh](https://taskfile.sh).
- Easy to understand and maintain
- Automatically generated list of available task

## How does it work?
# How does it work?

Taskfiles are simple bash scripts, but an easy-to-read function format. There are some things that we need to explain
for our Taskfile setup. It all starts with a `Taskfile`. Download your `Taskfile` from
[taskfile.sh](https://taskfile.sh) and save it. Make sure the Taskfile is executable: `chmod +x ./Taskfile`. You can now
run `./Taskfile` in your terminal.

### Tasks
## Tasks

A task is defined by creating a function that starts with `task:`. This defines a task that can be triggered by running
the `./Taskfile`. Right next to the task, you should add a task definition with two hashes. This will let the
Expand All @@ -40,7 +40,7 @@ function task:example { ## Show some example text
In a task you can call other functions, and run all tooling you desire. Now running `./Taskfile example` will execute
the new task.

### Sections
## Sections

To group multiple tasks, sections can be created in your Taskfile. A section is created by creating a comment line with
a double hashtag like so:
Expand All @@ -51,17 +51,43 @@ a double hashtag like so:

Lines with only a single `#` will not appear as section in `task:help` and can be seen as plain comments.

### Help command
## Help command

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!

## Credits
# 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`:

```shell
#compdef task

_task() {
local -a commands
local tasks=$(task comp_targets)

while IFS= read -r line; do
if [[ -n "$line" ]]; then
commands+=("$line")
fi
done <<< "$tasks"

_describe -t commands 'task commands' commands
}

_task "$@"
```

Now after running `task shorthand`, your `task` commands will get autocompleted.

# Credits

This Taskfile setup is based on [Adrian Cooney's Taskfile](https://github.com/adriancooney/Taskfile) and is widely
adopted by [Enrise](https://enrise.com) in our modified flavour.

## Contributors
# Contributors

A big thanks to all the contributors of Taskfile!

Expand Down
37 changes: 25 additions & 12 deletions bin/task
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@ RED=$(printf '\033[31m')
RESET=$(printf '\033[0m')

while [ -d $CURRENT_DIR ] && [ $CURRENT_DIR != '/' ]; do
if [ -f ./Taskfile ];
then
./Taskfile $@
exit $?
elif [ -f ./Makefile ]; then
echo -e "${RED}Found a Makefile instead of a Taskfile...${RESET}\n"
make $@
if [[ -e "${CURRENT_DIR}/Taskfile" ]]; then
TASKFILE="${CURRENT_DIR}/Taskfile"
break
fi

if [[ -e "${CURRENT_DIR}/Makefile" ]]; then
echo -e "Found a ${RED}Makefile${RESET} instead of a Taskfile...\n"
make --directory=$CURRENT_DIR $@
exit $?
else
cd ../
CURRENT_DIR=$(pwd)
fi

CURRENT_DIR=$(dirname $CURRENT_DIR)
done

echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
exit 1
if [[ -z "$TASKFILE" ]]; then
echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
exit 1
fi

if [[ $1 == 'comp_targets' ]]; then
awk 'BEGIN {FS = " { [#][#][ ]?"} /^function task:(.*)[#][#][ ]?/ {gsub(/:/, "\\:", $1); printf "%s:%s\n", $1, $2}' \
"$TASKFILE" \
| sed -E 's/[#]{2,}[ ]*//g' \
| sed -E 's/function task\\://g'
exit 1
fi

$TASKFILE $@
exit $?