-
Notifications
You must be signed in to change notification settings - Fork 19
/
completion-bundle
254 lines (245 loc) · 9.55 KB
/
completion-bundle
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#! bash
# bash completion for the `bundle` command.
#
# Copyright (c) 2009-2021 Daniel Luz <dev dot gsz at mernen dot com>.
# Distributed under the MIT license.
# https://mernen.com/projects/completion-ruby
#
# To use, source this file on bash:
# . completion-bundle
__bundle() {
local bundle_bin=("${_RUBY_COMMAND_PREFIX[@]}" "$1")
local cur prev
_get_comp_words_by_ref -n : cur prev
local bundle_command
local bundle_command_index
__bundle_get_command
COMPREPLY=()
local options
if [[ $cur = -* && $bundle_command != exec ]]; then
options="-V --help --no-color --no-no-color --verbose --no-verbose"
case $bundle_command in
"")
options="$options --version";;
check)
options="$options --dry-run --gemfile --path -r --retry";;
clean)
options="$options --dry-run --force";;
config)
options="$options --local --global --delete";;
doctor)
options="$options --gemfile --quiet --no-quiet";;
gem)
options="$options -b -e -t --bin --coc --no-coc --edit --exe
--no-exe --ext --no-ext --mit --no-mit --test";;
init)
options="$options --gemspec";;
install)
options="$options --binstubs --clean --deployment --force --frozen
--full-index --gemfile --jobs --local --no-cache
--no-prune --path --quiet --retry --shebang --standalone
--system --trust-policy --with --without";;
lock)
options="$options --add-platform --conservative --full-index
--local --lockfile --major --minor --patch --print
--remove-platform --strict --update";;
package)
options="$options --all --all-platforms";;
platform)
options="$options --ruby";;
show)
options="$options --outdated --paths --no-paths";;
update)
options="$options --bundler --conservative --force --full-index
--group --jobs --local --major --minor --patch --quiet
--ruby --source --strict";;
viz)
options="$options -f -F -R -v -W --file --format --requirements
--no-requirements --version --no-version --without";;
esac
else
case $bundle_command in
"" | help)
options="help install update package exec config
check show outdated console open lock viz init gem
platform clean doctor"
;;
check | install)
case $prev in
--binstubs | --path)
_filedir -d
return;;
--standalone | --with | --without)
__bundle_complete_groups
return;;
--trust-policy)
options="HighSecurity MediumSecurity LowSecurity
AlmostNoSecurity NoSecurity";;
esac
;;
config)
case $prev in
config | --*)
case $cur in
local.*)
options=($(__bundle_exec_ruby 'puts Bundler.definition.specs.to_hash.keys'))
options=("${options[*]/#/local.}")
;;
*)
options=(path frozen without bin gemfile ssl_ca_cert
ssl_client_cert cache_path disable_multisource
ignore_messages retry redirect timeout
force_ruby_platform specific_platform
disable_checksum_validation disable_version_check
allow_offline_install auto_install
cache_all_platforms cache_all clean console
disable_exec_load disable_local_branch_check
disable_shared_gems jobs major_deprecations
no_install no_prune only_update_to_newer_versions
plugins shebang silence_root_warning
ssl_verify_mode system_bindir user_agent)
# We want to suggest the options above as complete words,
# and also "local." and "mirror." as prefixes
# To achieve that, disable automatic space insertion,
# insert it manually, then add the non-spaced prefixes
compopt -o nospace 2>/dev/null
options=("${options[@]/%/ }")
# And add prefix suggestions
options+=(local. mirror.)
# Override $IFS for completion to work
local IFS=$'\n'
COMPREPLY=($(compgen -W '${options[@]}' -- "$cur"))
return
;;
esac
;;
path | local.*)
_filedir -d
return;;
esac
;;
exec)
if [[ $COMP_CWORD -eq $bundle_command_index ]]; then
# Figure out Bundler's binaries dir
local bundler_bin=$(__bundle_exec_ruby 'puts Bundler.bundle_path + "bin"')
if [[ -d $bundler_bin ]]; then
local binaries=("$bundler_bin"/*)
# If there are binaries, strip directory name and use them
[[ -f "$binaries" ]] && options="${binaries[@]##*/}"
else
# No binaries found; use full command completion
COMPREPLY=($(compgen -c -- "$cur"))
return
fi
else
local _RUBY_COMMAND_PREFIX=("${bundle_bin[@]}" exec)
_command_offset $bundle_command_index
return
fi
;;
gem)
case $prev in
-e | --edit)
COMPREPLY=($(compgen -c -- "$cur"))
return;;
-t | --test)
options="minitest rspec";;
esac
;;
update)
case $prev in
--group)
__bundle_complete_groups
return;;
*)
options=($(__bundle_exec_ruby 'puts Bundler.definition.specs.to_hash.keys'))
esac
;;
viz)
case $prev in
-F | --format)
options="dot jpg png svg";;
-W | --without)
__bundle_complete_groups
return;;
esac
;;
esac
fi
COMPREPLY=($(compgen -W "${options[*]}" -- "$cur"))
}
__bundle_get_command() {
local i
for ((i=1; i < $COMP_CWORD; ++i)); do
local arg=${COMP_WORDS[$i]}
case $arg in
[^-]*)
bundle_command=$arg
bundle_command_index=$((i + 1))
return;;
--version)
# Command-killer
bundle_command=-
return;;
--help)
bundle_command=help
bundle_command_index=$((i + 1))
return;;
esac
done
}
# Provides completion for Bundler group names.
#
# Multiple groups can be entered, separated either by spaces or by colons.
# Input is read from $cur, and the result is directly written to $COMPREPLY.
__bundle_complete_groups() {
# Group being currently written
local cur_group=${cur##*[ :]}
# All groups written before
local prefix=${cur%"$cur_group"}
local groups=$(__bundle_exec_ruby 'puts Bundler.definition.dependencies.map(&:groups).reduce(:|).map(&:to_s)')
if [[ ! $groups ]]; then
COMPREPLY=()
return
fi
# Duplicate "default" and anything already in $prefix, so that `uniq`
# strips it; groups may be separated by ':', ' ', or '\ '
local excluded=$'\ndefault\n'${prefix//[: \'\"\\]/$'\n'}
# Include them twice to ensure they are duplicates
groups=$groups$excluded$excluded
COMPREPLY=($(compgen -W "$(sort <<<"$groups" | uniq -u)" -- "$cur_group"))
# Prepend prefix to all entries
COMPREPLY=("${COMPREPLY[@]/#/$prefix}")
__ltrim_colon_completions "$cur"
}
# __bundle_exec_ruby <script> [args...]
#
# Runs a Ruby script with Bundler loaded.
# Results may be cached.
__bundle_exec_ruby() {
local bundle_bin=(${bundle_bin[@]:-bundle})
# Lockfile is inferred here, and might not be correct (for example, when
# running on a subdirectory). However, a wrong file path won't be a
# cadastrophic mistake; it just means the cache won't be invalidated when
# the local gem list changes (but will still invalidate if the command is
# run on another directory)
local lockfile=$PWD/Gemfile.lock
local cachedir=${XDG_CACHE_HOME:-~/.cache}/completion-ruby
local cachefile=$cachedir/bundle--exec-ruby
# A representation of all arguments with newlines replaced by spaces,
# to fit in a single line as a cache identifier
local cache_id_line="${bundle_bin[*]} @ $lockfile: ${*//$'\n'/ }"
if [[ (! -f $lockfile || $cachefile -nt $lockfile) &&
$(head -n 1 -- "$cachefile" 2>/dev/null) = "$cache_id_line" ]]; then
tail -n +2 -- "$cachefile"
else
local output=$("${bundle_bin[@]}" exec ruby -e "$@" 2>/dev/null)
if [[ $? -eq 0 ]]; then
(mkdir -p -- "$cachedir" &&
echo "$cache_id_line"$'\n'"$output" >$cachefile) 2>/dev/null
echo "$output"
fi
fi
}
complete -F __bundle -o bashdefault -o default bundle bundle2.{0..7} bundle3.{0..3} bundler bundler2.{0..7} bundler3.{0..3}
# vim: ai ft=sh sw=4 sts=4 et