Skip to content

Commit fe6fcb6

Browse files
committed
Add task autocompletion
1 parent f9d061c commit fe6fcb6

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

README.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Generate your own Taskfile at [taskfile.sh](https://taskfile.sh).
1616
- Easy to understand and maintain
1717
- Automatically generated list of available task
1818

19-
## How does it work?
19+
# How does it work?
2020

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

26-
### Tasks
26+
## Tasks
2727

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

43-
### Sections
43+
## Sections
4444

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

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

54-
### Help command
54+
## Help command
5555

5656
Running `./Taskfile help`, the `task:help` function is triggered. This task will list all available sections and tasks
5757
using the double `##` comments you've learned about above. Now it's clear how you can run any other task!
5858

59-
## Credits
59+
# Auto-completion
60+
61+
Autocompletion works when you use `zsh` with `oh-my-zsh`. Create the following file in your oh-my-zsh directory
62+
`~/.oh-my-zsh/completions/_task.zsh`:
63+
64+
```shell
65+
#compdef task
66+
67+
_task() {
68+
local -a commands
69+
local tasks=$(task comp_targets)
70+
71+
while IFS= read -r line; do
72+
if [[ -n "$line" ]]; then
73+
commands+=("$line")
74+
fi
75+
done <<< "$tasks"
76+
77+
_describe -t commands 'task commands' commands
78+
}
79+
80+
_task "$@"
81+
```
82+
83+
Now after running `task shorthand`, your `task` commands will get autocompleted.
84+
85+
**Note:** if your task name contains `:` characters, the autocomplete functionality will break. Replace the `:`
86+
character in your task names to prevent this.
87+
88+
# Credits
6089

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

64-
## Contributors
93+
# Contributors
6594

6695
A big thanks to all the contributors of Taskfile!
6796

bin/task

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@ RED=$(printf '\033[31m')
1010
RESET=$(printf '\033[0m')
1111

1212
while [ -d $CURRENT_DIR ] && [ $CURRENT_DIR != '/' ]; do
13-
if [ -f ./Taskfile ];
14-
then
15-
./Taskfile $@
16-
exit $?
17-
elif [ -f ./Makefile ]; then
18-
echo -e "${RED}Found a Makefile instead of a Taskfile...${RESET}\n"
19-
make $@
13+
if [[ -e "${CURRENT_DIR}/Taskfile" ]]; then
14+
TASKFILE="${CURRENT_DIR}/Taskfile"
15+
break
16+
fi
17+
18+
if [[ -e "${CURRENT_DIR}/Makefile" ]]; then
19+
echo -e "Found a ${RED}Makefile${RESET} instead of a Taskfile...\n"
20+
make --directory=$CURRENT_DIR $@
2021
exit $?
21-
else
22-
cd ../
23-
CURRENT_DIR=$(pwd)
2422
fi
23+
24+
CURRENT_DIR=$(dirname $CURRENT_DIR)
2525
done
2626

27-
echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
28-
exit 1
27+
if [[ -z "$TASKFILE" ]]; then
28+
echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
29+
exit 1
30+
fi
31+
32+
if [[ $1 == 'comp_targets' ]]; then
33+
awk 'BEGIN {FS = " { [#][#][ ]?"} /^function task([a-zA-Z_-]*:?.*)(\{ )?[#][#][ ]?/ {printf "%s:%s\n", $1, $2}' \
34+
"$TASKFILE" \
35+
| sed -E 's/[#]{2,}[ ]*//g' \
36+
| sed -E 's/function task://g'
37+
exit 1
38+
fi
39+
40+
$TASKFILE $@
41+
exit $?

0 commit comments

Comments
 (0)