2 # ------------------------------------------------------------------------------
3 # Copyright (c) 2016 Github zsh-users - http://github.com/zsh-users
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the zsh-users nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # ------------------------------------------------------------------------------
31 # Completion script for Ruby on Rails (http://rubyonrails.org/).
33 # ------------------------------------------------------------------------------
37 # * Kazuya Takeshima (https://github.com/mitukiii)
39 # ------------------------------------------------------------------------------
43 local context state line curcontext="$curcontext"
45 if (( CURRENT > 2 )); then
48 _call_function - "_rails_${words[1]}" || _nothing
55 local context state line curcontext="$curcontext"
57 local -a rails_options
58 __rails_setup_rails_options
67 local application_directory
68 __rails_setup_application_directory
70 if [ -n "$application_directory" ]; then
72 {generate,g}'[Generate new code]'
73 {console,c}'[Start the Rails console]'
74 {server,s}'[Start the Rails server]'
75 {dbconsole,db}'[Start a console for the database specified in config/database.yml]'
76 application'[Generate the Rails application code]'
77 {destroy,d}'[Undo code generated with "generate"]'
78 benchmarker'[See how fast a piece of code runs]'
79 profiler'[Get profile information from a piece of code]'
80 plugin'[Install a plugin]'
81 {runner,r}'[Run a piece of code in the application environment]'
86 new'[Create a new Rails application]'
90 _values 'command' $commands
95 __rails_setup_application_directory() {
96 application_directory="$(pwd)"
98 while [ -n "$application_directory" ]; do
99 if [ -f "${application_directory}/script/rails" -o -f "${application_directory}/bin/rails" ]; then
102 application_directory="${application_directory%/*}"
105 application_directory=
108 __rails_setup_rails_options() {
110 {-h,--help}'[Show this help message and quit]'
111 {-v,--version}'[Show Rails version number and quit]'
115 __rails_setup_runtime_options() {
117 '(-f --force)'{-f,--force}'[Overwrite files that already exist]'
118 '(-p --pretend)'{-p,--pretend}'[Run but do not make any changes]'
119 '(-q --quiet)'{-q,--quiet}'[Suppress status output]'
120 '(-s --skip)'{-s,--skip}'[Skip files that already exist]'
124 __rails_setup_generators_options() {
125 local -a runtime_options
126 __rails_setup_runtime_options
130 --skip-namespace'[Skip namespace (affects only isolated applications)]'
131 --old-style-hash"[Force using old style hash (:foo => 'bar') on Ruby >= 1.9]"
135 __rails_setup_model_generators_options() {
136 local -a generators_options
137 __rails_setup_generators_options
139 model_generators_options=(
141 '(-o --orm)'{-o,--orm=}'[Orm to be invoked]:orm'
145 __rails_setup_resource_generators_options() {
146 local -a model_generators_options
147 __rails_setup_model_generators_options
149 resource_generators_options=(
150 $model_generators_options
151 --force-plural'[Forces the use of a plural ModelName]'
152 --resource-route'[Indicates when to generate resource route]: :__rails_boolean'
157 _values 'boolean' 'true' 'false'
160 __rails_migration_fields() {
161 if compset -P '*:*:'; then
162 _values 'index' 'index' 'uniq'
164 if compset -P '*:'; then
165 _values -s ':' 'type' 'string' 'text' 'integer' 'float' 'decimal' 'datetime' 'timestamp' 'time' 'date' 'binary' 'boolean' 'references'
167 _guard '[[:alnum:]_]#' 'field'
173 local context state line curcontext="$curcontext"
175 if (( CURRENT > 2 )); then
178 _call_function - "_rails_generate_${words[1]}" || _rails_generate_default
180 __rails_generate_commands
188 __rails_generate_commands() {
189 local context curcontext="$curcontext" update_policy
191 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
192 if [ -z "$update_policy" ]; then
193 zstyle ":completion:${curcontext}:" cache-policy _rails_generate_commands_caching_policy
196 local application_directory
197 __rails_setup_application_directory
199 cache_name="rails/${application_directory##*/}/all_generators"
200 if ! _retrieve_cache ${cache_name}; then
201 local -a all_generators
202 all_generators=($(_call_program rails_generators rails generate 2> /dev/null | awk '/^ [a-zA-Z_]+/{ print $1 }'))
203 _store_cache ${cache_name} all_generators
206 local -a rails_generators
207 rails_generators=(${all_generators:#*:*})
208 _describe -t rails_generators 'rails generator' rails_generators
210 local -a -U namespaces
213 namespaces=(${(R)${(M)all_generators:#*:*}%:*})
214 for namespace in $namespaces; do
215 generators=(${${(M)all_generators:#${namespace}:*}/:/\\:})
216 _describe -t ${namespace}_generators "${namespace/_/ } generator" generators
220 _rails_generate_commands_caching_policy() {
221 local application_directory
222 __rails_setup_application_directory
224 if [ "${application_directory}/Gemfile" -nt "$1" ]; then
233 _rails_generate_default() {
234 local -a generators_options
235 __rails_setup_generators_options
238 $generators_options \
242 _rails_generate_assets() {
243 local -a generators_options
244 __rails_setup_generators_options
247 $generators_options \
248 '(-j --javascripts)'{-j,--javascripts}'[Generate JavaScripts]: :__rails_boolean' \
249 '(-y --stylesheets)'{-y,--stylesheets}'[Generate Stylesheets]: :__rails_boolean' \
250 '(-je --javascript-engine)'{-je,--javascript-engine=}'[Engine for JavaScripts]:javascript engine' \
251 '(-se --stylesheet-engine)'{-se,--stylesheet-engine=}'[Engine for Stylesheets]:stylesheet engine' \
252 ': :_guard "^-*" "name"'
255 _rails_generate_controller() {
256 local -a generators_options
257 __rails_setup_generators_options
260 $generators_options \
261 '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
262 '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
263 --helper'[Indicates when to generate helper]: :__rails_boolean' \
264 --assets'[Indicates when to generate assets]: :__rails_boolean' \
265 ': :_guard "^-*" "name"' \
266 '*: :_guard "^-*" "action"'
269 _rails_generate_generator() {
270 local -a generators_options
271 __rails_setup_generators_options
274 $generators_options \
275 --namespace'[Namespace generator under lib/generators/name]: :__rails_boolean' \
276 ': :_guard "^-*" "name"'
279 _rails_generate_helper() {
280 local -a generators_options
281 __rails_setup_generators_options
284 $generators_options \
285 '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
286 ': :_guard "^-*" "name"' \
289 _rails_generate_integration_test() {
290 local -a generators_options
291 __rails_setup_generators_options
294 $generators_options \
295 --integration-tool='[Integration tool to be invoke]:integration tool' \
296 ': :_guard "^-*" "name"' \
299 _rails_generate_jbuilder() {
300 local -a generators_options
301 __rails_setup_generators_options
304 $generators_options \
305 ': :_guard "^-*" "name"' \
306 '*: :__rails_migration_fields'
309 _rails_generate_mailer() {
310 local -a generators_options
311 __rails_setup_generators_options
314 $generators_options \
315 '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
316 '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
317 ': :_guard "^-*" "name"' \
318 '*: :_guard "^-*" "method"'
321 _rails_generate_migration() {
322 local -a modelgenerators_options
323 __rails_setup_model_generators_options
326 $model_generators_options \
327 ': :_guard "^-*" "name"' \
328 '*: :__rails_migration_fields'
331 _rails_generate_model() {
332 _rails_generate_migration
335 _rails_generate_observer() {
336 local -a model_generators_options
337 __rails_setup_model_generators_options
340 $model_generators_options \
341 ': :_guard "^-*" "name"'
344 _rails_generate_performance_test() {
345 local -a generators_options
346 __rails_setup_generators_options
349 $generators_options \
350 --performance-tool='[Performance tool to be invoked]:performance tool' \
351 ': :_guard "^-*" "name"' \
354 _rails_generate_resource() {
355 local context state line curcontext="$curcontext"
357 local -a resource_generators_options
358 __rails_setup_resource_generators_options
361 $resource_generators_options \
362 '(-c --resource-controller)'{-c,--resource-controller=}'[Resource controller to be invoked]:name' \
363 '(-a --actions)'{-a,--actions=}'[Actions for the resource controller]: :->actions' \
367 if (( words[(I)(--actions=*|-a)] > 0 && words[(I)(--actions=*|-a)] == words[(I)-*] )); then
373 _guard "[[:alnum:]_]#" "actions"
379 __rails_migration_fields
384 _rails_generate_scaffold() {
385 local -a resource_generators_options
386 __rails_setup_resource_generators_options
389 $resource_generators_options \
390 '(-y --stylesheets)'{-y,--stylesheets}'[Generate Stylesheets]: :__rails_boolean' \
391 '(-se --stylesheet-engine)'{-se,--stylesheet-engine=}'[Engine for Stylesheets]:stylesheet engine' \
392 '(-c --scaffold-controller)'{-c,--scaffold-controller=}'[Scaffold controller to be invoked]:name' \
393 --assets'[Indicates when to generate assets]:boolean:(true false)' \
394 ': :_guard "^-*" "name"' \
395 '*: :__rails_migration_fields'
398 _rails_generate_scaffold_controller() {
399 local -a model_generators_options
400 __rails_setup_model_generators_options
403 $model_generators_options \
404 '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
405 '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
406 --helper'[Indicates when to generate helper]: :__rails_boolean' \
407 ': :_guard "^-*" "name"'
410 _rails_generate_session_migration() {
411 local -a model_generators_options
412 __rails_setup_model_generators_options
415 $model_generators_options \
416 ': :_guard "^-*" "name"'
419 _rails_generate_task() {
420 local -a generators_options
421 __rails_setup_generators_options
424 $generators_options \
425 ': :_guard "^-*" "name"' \
426 '*: :_guard "^-*" "action"'
431 '(- *)'{-h,--help}'[Show this help message]' \
432 '(-s --sandbox)'{-s,--sandbox}'[Rollback database modifications on exit]' \
433 --debugger'[Enable ruby-debugging for the console]'
442 '(- *)'{-h,--help}'[Show this help message]' \
443 '(-p --port)'{-p,--port=}'[Runs Rails on the specified port]: :_guard "[[\:digit\:]]#" "port"' \
444 '(-b --binding)'{-b,--binding=}'[Binds Rails to the specified ip]:ip:_hosts' \
445 '(-c --config)'{-c,--config=}'[Use custom rackup configuration file]:file:_files -g "*.ru"' \
446 '(-d --daemon)'{-d,--daemon}'[Make server run as a Daemon]' \
447 '(-u --debugger)'{-u,--debugger}'[Enable ruby-debugging for the server]' \
448 '(-e --environment)'{-e,--environment=}'[Specifies the environment to run this server under (test/development/production)]:name:(test development production)' \
449 '(-P --pid)'{-P,--pid=}'[Specifies the PID file]:pid:_files -g "*.pid"'
458 '(- *)'--help'[Show this help message]' \
459 '(-p --include-password)'{-p,--include-password}'[Automatically provide the password from database.yml]' \
460 --mode'[Automatically put the sqlite3 database in the specified mode (html, list, line, column)]:mode:(html list line column)' \
465 local context state line curcontext="$curcontext"
467 local _a rails_options runtime_options
468 __rails_setup_rails_options
469 __rails_setup_runtime_options
474 '(-r --ruby)'{-r,--ruby=}'[Path to the Ruby binary of your choice]:path' \
475 '(-b --builder)'{-b,--builder=}'[Path to a application builder (can be a filesystem path or URL)]: :->path_or_url' \
476 '(-m --template)'{-m,--template=}'[Path to an application template (can be a filesystem path or URL)]: :->path_or_url' \
477 --skip-gemfile"[Don't create a Gemfile]" \
478 --skip-bundle"[Don't run bundle install]" \
479 '(-G --skip-git)'{-G,--skip-git}'[Skip Git ignores and keeps]' \
480 '(-O --skip-active-record)'{-O,--skip-active-record}'[Skip Active Record files]' \
481 '(-S --skip-sprockets)'{-S,--skip-sprockets}'[Skip Sprockets files]' \
482 '(-d --database)'{-d,--database=}'[Preconfigure for selected database]:database:(mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc)' \
483 '(-j --javascript)'{-j,--javascript=}'[Preconfigure for selected JavaScript library]:javascript' \
484 '(-J --skip-javascript)'{-J,--skip-javascript}'[Skip JavaScript files]' \
485 --dev'[Setup the application with Gemfile pointing to your Rails checkout]' \
486 --edge'[Setup the application with Gemfile pointing to Rails repository]' \
487 '(-T --skip-test-unit)'{-T,--skip-test-unit}'[Skip Test::Unit files]' \
488 --old-style-hash"[Force using old style hash (:foo => 'bar') on Ruby >= 1.9]" \
489 ':app path:_directories'
494 'files:path:_files -g "*.rb"' \
500 _rails_application() {
516 _rails_benchmarker() {
518 '(- *)'{-h,--help}'[Show this help message]' \
519 '(-r --runs)'{-r,--runs}'[Number of runs]: :_guard "[[\:digit\:]]#" "number"' \
520 '(-o --output)'{-o,--output}'[Directory to use when writing the results]:directory:_directories' \
521 '(-m --metrics)'{-m,--metrics}'[Metrics to use]: :_values -s "," "metrics" "wall_time" "memory" "objects" "gc_runs" "gc_time"' \
522 '*: :_guard "^-*" "ruby code"'
527 '(- *)'{-h,--help}'[Show this help message]' \
528 '(-r --runs)'{-r,--runs}'[Number of runs]: :_guard "[[\:digit\:]]#" "number"' \
529 '(-o --output)'{-o,--output}'[Directory to use when writing the results]:directory:_directories' \
530 '(-m --metrics)'{-m,--metrics}'[Metrics to use]: :_values -s "," "metrics" "process_time" "memory" "objects"' \
531 '(-f --formats)'{-f,--formats}'[Formats to output to]: :_values -s "," "formats" "flat" "graph" "html" "call_tree" "call_stack"' \
532 '*: :_guard "^-*" "ruby code"'
536 local context state line curcontext="$curcontext"
538 if (( CURRENT > 2 )); then
541 _call_function - "_rails_plugin_${words[1]}" || _nothing
543 __rails_plugin_commands
547 __rails_plugin_commands() {
548 _values 'plugin command' \
549 install'[Install plugin(s) from known repositories or URLs]' \
550 remove'[Uninstall plugins]' \
554 _rails_plugin_install() {
556 '(-x --externals)'{-x,--externals}'[Use svn:externals to grab the plugin. Enables plugin updates and plugin versioning]' \
557 '(-o --checkout)'{-o,--checkout}'[Use svn checkout to grab the plugin. Enables updating but does not add a svn:externals entry]' \
558 '(-e --export)'{-e,--export}'[Use svn export to grab the plugin. Exports the plugin, allowing you to check it into your local repository. Does not enable updates or add an svn:externals entry]' \
559 '(-q --quiet)'{-q,--quiet}'[Suppresses the output from installation. Ignored if -v is passed (rails plugin -v install ...)]' \
560 '(-r --revision)'{-r,--revision=}'[Checks out the given revision from subversion or git. Ignored if subversion/git is not used]:revision' \
561 '(-f --force)'{-f,--force}"[Reinstalls a plugin if it's already installed]" \
565 _rails_plugin_remove() {
568 plugins=($(_call_program rails_plugins ls -1 vendor/plugins))
570 _describe -t plugins 'plugin' plugins
573 _rails_plugin_new() {
578 local context state line curcontext="$curcontext"
581 '(- *)'{-h,--help}'[Show this help message]' \
582 '(-e --environment)'{-e,--environment=}'[Specifies the environment for the runner to operate under (test/development/production)]:name:(test development production)' \
588 'files:filename:_files -g "*.rb"' \
589 'codes:ruby code:_guard "^-*" "ruby code"'
599 local context state line curcontext="$curcontext"
607 'files:filename:_files -g "*.rb"'
621 # indent-tabs-mode: nil
624 # vim: ft=zsh sw=2 ts=2 et