cc53e4cb9945dca873a5f62647db1b29973c6e34

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #2471 from phstc/capistrano-multistage

Adds support to capistrano multistage completion

Diff

 1diff --git a/plugins/capistrano/_capistrano b/plugins/capistrano/_capistrano
 2index 3cadf3d54be8aabaacaf509d5ca75ab14eb97910..e6e71ffcc80d121d3adba7bde7f461bafcaa26b3 100644
 3--- a/plugins/capistrano/_capistrano
 4+++ b/plugins/capistrano/_capistrano
 5@@ -1,10 +1,49 @@
 6-#compdef cap
 7+#compdef shipit
 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 `shipit` 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+      shipit -v --tasks | sed 's/\(\[\)\(.*\)\(\]\)/\2:/' | 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..c85eb474ca8a9cd85f8f3cc4446e54ce5a5a0c85
65--- /dev/null
66+++ b/plugins/capistrano/capistrano.plugin.zsh
67@@ -0,0 +1,11 @@
68+# Added `shipit` 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 shipit() {
72+  if [ -f Gemfile ]
73+  then
74+    bundle exec cap $*
75+  else
76+    cap $*
77+  fi
78+}