3 # -----------------------------------------------------------------------------
6 # Completion script for grunt.
7 # - https://github.com/gruntjs/grunt
8 # - https://github.com/gruntjs/grunt-cli
10 # -----------------------------------------------------------------------------
13 # Author : Yonchu <yuyuchu3333@gmail.com>
14 # License : MIT License
15 # Repository : https://github.com/yonchu/grunt-zsh-completion
16 # Last Change : 20 Aug 2014.
18 # Copyright (c) 2013 Yonchu.
20 # -----------------------------------------------------------------------------
26 # If you want to use the cache, set the followings in your .zshrc:
28 # zstyle ':completion:*' use-cache yes
33 # - Show grunt file path:
34 # zstyle ':completion::complete:grunt::options:' show_grunt_path yes
36 # - Cache expiration days (default: 7):
37 # zstyle ':completion::complete:grunt::options:' expire 1
39 # - Not update options cache if target gruntfile is changed.
40 # zstyle ':completion::complete:grunt::options:' no_update_options yes
42 # Note that if you change the zstyle settings,
43 # you should delete the cache file and restart zsh.
45 # $ rm ~/.zcompcache/grunt
48 # -----------------------------------------------------------------------------
51 local curcontext="$curcontext" update_policy state
52 local show_grunt_path update_msg gruntfile opts tasks
55 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
56 if [[ -z $update_policy ]]; then
57 zstyle ":completion:${curcontext}:" cache-policy __grunt_caching_policy
60 # Check show_path option.
61 zstyle -b ":completion:${curcontext}:options:" show_grunt_path show_grunt_path
63 # Get current gruntfile.
64 gruntfile=$(__grunt_get_gruntfile)
66 # Initialize opts and tasks.
71 opts+=('(- 1 *)'{-h,--help}'[Display this help text.]')
73 ## Complete without gruntfile.
74 if [[ ! -f $gruntfile ]]; then
75 _arguments "${opts[@]}"
79 ## Complete with gruntfile.
81 if ! __grunt_update_cache "$gruntfile"; then
82 update_msg=' (cache updated)'
85 # Make options completion.
86 if [[ ${#__grunt_opts} -gt 0 ]]; then
87 opts+=("${__grunt_opts[@]}")
98 if [[ $show_grunt_path == 'yes' ]]; then
99 update_msg="$update_msg: ${${gruntfile/#$HOME/~}%/}"
101 # Make tasks completion.
102 if [[ ${#__grunt_tasks} -gt 0 ]]; then
103 tasks+=("${__grunt_tasks[@]}")
104 _describe -t grunt-task "$verbose grunt task$update_msg" tasks || return 1
113 # The cache file name: grunt
114 # The cache variable name: __grunt_version __grunt_gruntfile __grunt_opts __grunt_tasks
115 function __grunt_update_cache() {
117 local version='0.1.2'
120 local grunt_info no_update_options cache_path
122 # Check no_update_options option.
123 zstyle -b ":completion:${curcontext}:options:" no_update_options no_update_options
126 if ! ( (( $+__grunt_gruntfile )) \
127 && (( $+__grunt_opts )) \
128 && (( $+__grunt_tasks )) ) \
129 && ! _retrieve_cache 'grunt'; then
133 if [[ $gruntfile != $__grunt_gruntfile ]]; then
134 # Except for --help options.
135 __grunt_gruntfile=$gruntfile
136 if [[ $no_update_options == 'yes' ]]; then
137 if [[ $PREFIX == ${PREFIX#-} ]]; then
138 # Not options completions.
140 elif [[ ${#__grunt_opts} -lt 2 ]]; then
143 unset __grunt_gruntfile
149 if [[ $PREFIX != ${PREFIX#-} && ${#__grunt_opts} -gt 1 ]]; then
150 unset __grunt_gruntfile
154 if _cache_invalid 'grunt'; then
158 # Check _grunt version.
159 if [[ $__grunt_version != $version ]]; then
163 if [[ $is_updating -ne 0 ]]; then
165 __grunt_version=$version
166 __grunt_gruntfile=$gruntfile
168 grunt_info=$(grunt --help --no-color --gruntfile "$__grunt_gruntfile" 2>/dev/null)
169 __grunt_opts=(${(f)"$(__grunt_get_opts "$grunt_info")"})
170 __grunt_tasks=(${(f)"$(__grunt_get_tasks "$grunt_info")"})
171 _store_cache 'grunt' __grunt_version __grunt_gruntfile __grunt_opts __grunt_tasks
176 function __grunt_get_tasks() {
178 | grep 'Available tasks' -A 100 \
180 | sed -e 's/^[[:blank:]]*//' -e 's/[[:blank:]]*$//' \
181 | sed -e 's/:/\\:/g' \
185 function __grunt_get_opts() {
186 local opt_hunk opt_sep opt_num line opt
187 opt_hunk=$(echo -E "$1" \
188 | grep 'Options$' -A 100 \
190 | sed -e 's/[[:blank:]]*$//' \
194 opt_hunk=(${(f)opt_hunk})
196 for line in "$opt_hunk[@]"; do
197 opt=$(echo -E "$line" | sed -e 's/^[[:blank:]]*//')
198 if [[ $line == $opt ]]; then
201 if [[ $opt != ${opt#-} ]]; then
204 opt=$(echo -E "$opt" | sed 's/^\(\(--[^ ]*\)\(, \(-[^ ]*\)\)*\) */\2\\t\4\\\t/')
206 opt_sep[$opt_num]=("${opt_sep[$opt_num]}${opt}")
209 for line in "$opt_sep[@]"; do
211 if [[ ${opt[1]} == '--help' ]]; then
214 if [[ ${#opt} -eq 2 ]]; then
215 echo -E "(${opt[1]})${opt[1]}[${opt[2]}]"
217 echo -E "(${opt[1]},${opt[2]})${opt[1]}[${opt[3]}]"
218 echo -E "(${opt[1]},${opt[2]})${opt[2]}[${opt[3]}]"
223 function __grunt_get_gruntfile() {
226 while [ "$curpath" ]; do
227 for gruntfile in "$curpath/"{G,g}runtfile.{js,coffee}; do
228 if [[ -e "$gruntfile" ]]; then
233 curpath=${curpath%/*}
238 function __grunt_caching_policy() {
239 # Returns status zero if the completions cache needs rebuilding.
241 # Rebuild if .agignore more recent than cache.
242 if [[ -f $__grunt_gruntfile && $__grunt_gruntfile -nt $1 ]]; then
243 # Invalid cache because gruntfile is old.
249 zstyle -s ":completion:${curcontext}:options:" expire expire || expire=7
250 # Rebuild if cache is more than $expire days.
251 oldp=( "$1"(Nm+$expire) )
255 compdef __grunt grunt