16ae4e127f6af2f8f3332025ce46d45ab5bcadac

Author
Pablo Cantero <pablo@pablocantero.com>
Committer
Pablo Cantero <pablo@pablocantero.com>
Date

Message

Adds support capistrano multistage completion

Diff

 1diff --git a/plugins/capistrano/_capistrano b/plugins/capistrano/_capistrano
 2index 3cadf3d54be8aabaacaf509d5ca75ab14eb97910..7a74b3732d575efb580d0ae84c7764b0a2b856e2 100644
 3--- a/plugins/capistrano/_capistrano
 4+++ b/plugins/capistrano/_capistrano
 5@@ -1,10 +1,49 @@
 6-#compdef cap
 7+#compdef xcap cap
 8 #autoload
 9 
10-if [[ -f config/deploy.rb || -f Capfile ]]; then
11-  if [[ ! -f .cap_tasks~ || config/deploy.rb -nt .cap_tasks~ ]]; then
12-    echo "\nGenerating .cap_tasks~..." > /dev/stderr
13-    cap -v --tasks | grep '#' | cut -d " " -f 2 > .cap_tasks~
14+# Added `xcap` because `cap` is a reserved word. `cap` completion doesn't work.
15+# http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fcap-Module
16+
17+local curcontext="$curcontext" state line ret=1
18+local -a _configs
19+
20+_arguments -C \
21+  '1: :->cmds' \
22+  '2:: :->args' && ret=0
23+
24+_cap_tasks() {
25+  if [[ -f config/deploy.rb || -f Capfile ]]; then
26+    if [[ ! -f .cap_tasks~ ]]; then
27+      xcap -v --tasks | awk '{command=$2; $1=$2=$3=""; gsub(/^[ \t\r\n]+/, "", $0); gsub(":", "\\:", command); print command"["$0"]"}' > .cap_tasks~
28+    fi
29+
30+    OLD_IFS=$IFS
31+    IFS=$'\n'
32+    _values 'cap commands' $(< .cap_tasks~)
33+    IFS=$OLD_IFS
34+    # zmodload zsh/mapfile
35+    # _values ${(f)mapfile[.cap_tasks~]}
36   fi
37-  compadd `cat .cap_tasks~`
38-fi
39+}
40+
41+_cap_stages() {
42+  compadd $(find config/deploy -name \*.rb | cut -d/ -f3 | sed s:.rb::g)
43+}
44+
45+case $state in
46+  cmds)
47+    # check if it uses multistage
48+    if [[ -d config/deploy ]]; then
49+      _cap_stages
50+    else
51+      _cap_tasks
52+    fi
53+    ret=0
54+    ;;
55+  args)
56+    _cap_tasks
57+    ret=0
58+    ;;
59+esac
60+
61+return ret
62diff --git a/plugins/capistrano/capistrano.plugin.zsh b/plugins/capistrano/capistrano.plugin.zsh
63new file mode 100644
64index 0000000000000000000000000000000000000000..9a4a2cc34b95aa050ca4393e4c32a6a16fa14621
65--- /dev/null
66+++ b/plugins/capistrano/capistrano.plugin.zsh
67@@ -0,0 +1,11 @@
68+# Added `xcap` because `cap` is a reserved word. `cap` completion doesn't work.
69+# http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fcap-Module
70+
71+func xcap() {
72+  if [ -f Gemfile ]
73+  then
74+    bundle exec cap $*
75+  else
76+    cap $*
77+  fi
78+}