forked from mernen/completion-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion-rails
More file actions
213 lines (191 loc) · 5.2 KB
/
completion-rails
File metadata and controls
213 lines (191 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! bash
# bash completion for the `rails` command and rails scripts.
#
# Copyright (c) 2008-2011 Daniel Luz <dev at mernen dot com>.
# Distributed under the MIT license.
# http://mernen.com/projects/completion-ruby
#
# To use, source this file on bash:
# . completion-rails
__rails() {
if [[ -z $__RAILS_VERSION ]]; then
local ver=$(rails --version 2>/dev/null)
if [[ $ver = "Rails 2."* ]]; then
__RAILS_VERSION=__rails2
else
__RAILS_VERSION=__rails3
fi
fi
$__RAILS_VERSION "$@"
}
# sets $rails_command and $rails_subcommand
__rails_get_command() {
rails_command=
rails_subcommand=
local i
for ((i=1; i < $COMP_CWORD; ++i)); do
local arg=${COMP_WORDS[$i]}
case $arg in
-b | --builder | -r | --ruby | --root | -m | --template |\
-s | --source | -e | --environment)
# ignore next argument
((++i))
;;
[^-]*)
if [[ -n $rails_command ]]; then
rails_subcommand=$arg
return
else
rails_command=$arg
case $arg in
generate | g | destroy | plugin)
# continue processing, looking for a subcommand
;;
*)
# end processing
return;;
esac
fi
;;
esac
done
}
__rails_complete_environments() {
local environments=(config/environments/*.rb)
if [[ -f $environments ]]; then
environments=("${environments[@]##*/}")
environments=("${environments[@]%.rb}")
else
environments=(development test production)
fi
COMPREPLY=($(compgen -W "${environments[*]}" -- "$1"))
}
__rails3() {
local cur=$2
local prev=$3
__rails_get_command
COMPREPLY=()
case $prev in
-b | --builder | -r | --ruby | --root | -m | --template | -s | --source)
# leave it to complete the path
return;;
-e | --environment)
__rails_complete_environments "$cur"
return;;
--mode)
if [[ $rails_command = dbconsole || $rails_command = db ]]; then
local options="html list line column"
COMPREPLY=($(compgen -W "$options" -- "$cur"))
return
fi
;;
esac
local options
if [[ $cur = -* ]]; then
if [[ -z $rails_command ]]; then
options="-h --help -v --version"
else
local param
options=$(rails "$rails_command" $rails_subcommand --help |
ruby -ne 'puts $_.scan(/(-\w)|(--\w[-\w]*)/)')
fi
else
case $rails_command in
"")
options=$(rails |
ruby -ne 'puts $1 if ~/^ rails (\S+)/;
puts $1 if ~/^ (\S+)/;
puts $1 if ~/alias: "(\S+)"/')
;;
server | s)
# load list dynamically?
options="mongrel thin"
;;
console | c | dbconsole | db)
__rails_complete_environments "$cur"
return;;
generate | g | destroy)
options=$(rails "$rails_command" --help |
ruby -ne 'puts $1 if ~/^ (\w\S*)/')
;;
plugin)
options="install remove"
esac
fi
COMPREPLY=($(compgen -W "$options" -- "$cur"))
}
__rails2() {
local cur=$2
local prev=$3
COMPREPLY=()
case $prev in
-r | --ruby)
# leave it to complete the path to a binary
return;;
-d | --database)
local dbs="mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db"
COMPREPLY=($(compgen -W "$dbs" -- "$cur"))
return;;
esac
if [[ $cur = -* ]]; then
local options="
-r --ruby -d --database -f --freeze -v --version -h --help -p --pretend
--force -s --skip -q --quiet -t --backtrace -c --svn -g --git"
COMPREPLY=($(compgen -W "$options" -- "$cur"))
fi
}
__rails2_script_server() {
local cur=$2
local prev=$3
COMPREPLY=()
case $prev in
-e | --environment)
__rails_complete_environments "$cur"
return;;
*)
if [[ $cur = -* ]]; then
local options="
-p --port -b --binding -d --daemon -u --debugger -e --environment"
COMPREPLY=($(compgen -W "$options" -- "$cur"))
fi
esac
}
__rails2_script_console() {
local cur=$2
local prev=$3
COMPREPLY=()
if [[ $cur = -* ]]; then
local options="-s --sandbox --irb --debugger"
COMPREPLY=($(compgen -W "$options" -- "$cur"))
elif [[ $prev = --irb ]]; then
COMPREPLY=($(compgen -A command -- "$cur"))
else
__rails_complete_environments "$cur"
fi
}
__rails2_script_generate() {
local cur=$2
local prev=$3
COMPREPLY=()
if [[ $cur = -* ]]; then
local options="
-v --version -h --help -p --pretend -f --force -s --skip
-q --quiet -t --backtrace -c --svn -g --git"
COMPREPLY=($(compgen -W "$options" -- "$cur"))
elif __rails_get_command && [[ $rails_command = "" ]]; then
__rails2_complete_generators "$cur"
fi
}
__rails2_complete_generators() {
local generators="
controller integration_test mailer migration model observer
plugin resource scaffold session_migration"
COMPREPLY=($(compgen -W "$generators" -- "$1"))
}
# clear the version cache, targeted version might change on reload
unset __RAILS_VERSION
complete -F __rails -o default rails
complete -F __rails2_script_server -o default script/server ./script/server
complete -F __rails2_script_console -o default script/console ./script/console
complete -F __rails2_script_generate -o default script/generate ./script/generate
# vim: ai ft=sh sw=2 sts=2 et