]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/rails/_rails
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / rails / _rails
1 #compdef rails
2 # ------------------------------------------------------------------------------
3 # Copyright (c) 2016 Github zsh-users - http://github.com/zsh-users
4 # All rights reserved.
5 #
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.
16 #
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 # ------------------------------------------------------------------------------
28 # Description
29 # -----------
30 #
31 #  Completion script for Ruby on Rails (http://rubyonrails.org/).
32 #
33 # ------------------------------------------------------------------------------
34 # Authors
35 # -------
36 #
37 #  * Kazuya Takeshima (https://github.com/mitukiii)
38 #
39 # ------------------------------------------------------------------------------
40
41
42 _rails() {
43   local context state line curcontext="$curcontext"
44
45   if (( CURRENT > 2 )); then
46     (( CURRENT-- ))
47     shift words
48     _call_function - "_rails_${words[1]}" || _nothing
49   else
50     __rails_commands
51   fi
52 }
53
54 __rails_commands() {
55   local context state line curcontext="$curcontext"
56
57   local -a rails_options
58   __rails_setup_rails_options
59
60   _arguments -C \
61     $rails_options \
62     ': :->command'
63
64   case "$state" in
65     command)
66       local -a commands
67       local application_directory
68       __rails_setup_application_directory
69
70       if [ -n "$application_directory" ]; then
71         commands=(
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]'
82           {test,t}'[Run tests]'
83         )
84       else
85         commands=(
86           new'[Create a new Rails application]'
87         )
88       fi
89
90       _values 'command' $commands
91       ;;
92   esac
93 }
94
95 __rails_setup_application_directory() {
96   application_directory="$(pwd)"
97
98   while [ -n "$application_directory" ]; do
99     if [ -f "${application_directory}/script/rails" -o -f "${application_directory}/bin/rails" ]; then
100       return
101     fi
102     application_directory="${application_directory%/*}"
103   done
104
105   application_directory=
106 }
107
108 __rails_setup_rails_options() {
109   rails_options=(
110     {-h,--help}'[Show this help message and quit]'
111     {-v,--version}'[Show Rails version number and quit]'
112   )
113 }
114
115 __rails_setup_runtime_options() {
116   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]'
121   )
122 }
123
124 __rails_setup_generators_options() {
125   local -a runtime_options
126   __rails_setup_runtime_options
127
128   generators_options=(
129     $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]"
132   )
133 }
134
135 __rails_setup_model_generators_options() {
136   local -a generators_options
137   __rails_setup_generators_options
138
139   model_generators_options=(
140     $generators_options
141     '(-o --orm)'{-o,--orm=}'[Orm to be invoked]:orm'
142   )
143 }
144
145 __rails_setup_resource_generators_options() {
146   local -a model_generators_options
147   __rails_setup_model_generators_options
148
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'
153   )
154 }
155
156 __rails_boolean() {
157   _values 'boolean' 'true' 'false'
158 }
159
160 __rails_migration_fields() {
161   if compset -P '*:*:'; then
162     _values 'index' 'index' 'uniq'
163   else
164     if compset -P '*:'; then
165       _values -s ':' 'type' 'string' 'text' 'integer' 'float' 'decimal' 'datetime' 'timestamp' 'time' 'date' 'binary' 'boolean' 'references'
166     else
167       _guard '[[:alnum:]_]#' 'field'
168     fi
169   fi
170 }
171
172 _rails_generate() {
173   local context state line curcontext="$curcontext"
174
175   if (( CURRENT > 2 )); then
176     (( CURRENT-- ))
177     shift words
178     _call_function - "_rails_generate_${words[1]}" || _rails_generate_default
179   else
180     __rails_generate_commands
181   fi
182 }
183
184 _rails_g() {
185   _rails_generate
186 }
187
188 __rails_generate_commands() {
189   local context curcontext="$curcontext" update_policy
190
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
194   fi
195
196   local application_directory
197   __rails_setup_application_directory
198   local cache_name
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
204   fi
205
206   local -a rails_generators
207   rails_generators=(${all_generators:#*:*})
208   _describe -t rails_generators 'rails generator' rails_generators
209
210   local -a -U namespaces
211   local namespace
212   local -a generators
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
217   done
218 }
219
220 _rails_generate_commands_caching_policy() {
221   local application_directory
222   __rails_setup_application_directory
223
224   if [ "${application_directory}/Gemfile" -nt "$1" ]; then
225     return 0
226   fi
227
228   local -a oldp
229   oldp=( "$1"(Nmw+1) )
230   (( $#oldp ))
231 }
232
233 _rails_generate_default() {
234   local -a generators_options
235   __rails_setup_generators_options
236
237   _arguments \
238     $generators_options \
239     '*:argument'
240 }
241
242 _rails_generate_assets() {
243   local -a generators_options
244   __rails_setup_generators_options
245
246   _arguments \
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"'
253 }
254
255 _rails_generate_controller() {
256   local -a generators_options
257   __rails_setup_generators_options
258
259   _arguments \
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"'
267 }
268
269 _rails_generate_generator() {
270   local -a generators_options
271   __rails_setup_generators_options
272
273   _arguments \
274     $generators_options \
275     --namespace'[Namespace generator under lib/generators/name]: :__rails_boolean' \
276     ': :_guard "^-*" "name"'
277 }
278
279 _rails_generate_helper() {
280   local -a generators_options
281   __rails_setup_generators_options
282
283   _arguments \
284     $generators_options \
285     '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
286     ': :_guard "^-*" "name"' \
287 }
288
289 _rails_generate_integration_test() {
290   local -a generators_options
291   __rails_setup_generators_options
292
293   _arguments \
294     $generators_options \
295     --integration-tool='[Integration tool to be invoke]:integration tool' \
296     ': :_guard "^-*" "name"' \
297 }
298
299 _rails_generate_jbuilder() {
300   local -a generators_options
301   __rails_setup_generators_options
302
303   _arguments \
304     $generators_options \
305     ': :_guard "^-*" "name"' \
306     '*: :__rails_migration_fields'
307 }
308
309 _rails_generate_mailer() {
310   local -a generators_options
311   __rails_setup_generators_options
312
313   _arguments \
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"'
319 }
320
321 _rails_generate_migration() {
322   local -a modelgenerators_options
323   __rails_setup_model_generators_options
324
325   _arguments \
326     $model_generators_options \
327     ': :_guard "^-*" "name"' \
328     '*: :__rails_migration_fields'
329 }
330
331 _rails_generate_model() {
332   _rails_generate_migration
333 }
334
335 _rails_generate_observer() {
336   local -a model_generators_options
337   __rails_setup_model_generators_options
338
339   _arguments \
340     $model_generators_options \
341     ': :_guard "^-*" "name"'
342 }
343
344 _rails_generate_performance_test() {
345   local -a generators_options
346   __rails_setup_generators_options
347
348   _arguments \
349     $generators_options \
350     --performance-tool='[Performance tool to be invoked]:performance tool' \
351     ': :_guard "^-*" "name"' \
352 }
353
354 _rails_generate_resource() {
355   local context state line curcontext="$curcontext"
356
357   local -a resource_generators_options
358   __rails_setup_resource_generators_options
359
360   _arguments -C \
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' \
364     ': :->name' \
365     '*: :->fields'
366
367   if (( words[(I)(--actions=*|-a)] > 0 && words[(I)(--actions=*|-a)] == words[(I)-*] )); then
368     state=actions
369   fi
370
371   case "$state" in
372     actions)
373       _guard "[[:alnum:]_]#" "actions"
374       ;;
375     name)
376       _guard "^-*" "name"
377       ;;
378     fields)
379       __rails_migration_fields
380       ;;
381   esac
382 }
383
384 _rails_generate_scaffold() {
385   local -a resource_generators_options
386   __rails_setup_resource_generators_options
387
388   _arguments \
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'
396 }
397
398 _rails_generate_scaffold_controller() {
399   local -a model_generators_options
400   __rails_setup_model_generators_options
401
402   _arguments \
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"'
408 }
409
410 _rails_generate_session_migration() {
411   local -a model_generators_options
412   __rails_setup_model_generators_options
413
414   _arguments \
415     $model_generators_options \
416     ': :_guard "^-*" "name"'
417 }
418
419 _rails_generate_task() {
420   local -a generators_options
421   __rails_setup_generators_options
422
423   _arguments \
424     $generators_options \
425     ': :_guard "^-*" "name"' \
426     '*: :_guard "^-*" "action"'
427 }
428
429 _rails_console() {
430   _arguments \
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]'
434 }
435
436 _rails_c() {
437   _rails_console
438 }
439
440 _rails_server() {
441   _arguments \
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"'
450 }
451
452 _rails_s() {
453   _rails_server
454 }
455
456 _rails_dbconsole() {
457   _arguments \
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)' \
461     --header
462 }
463
464 _rails_new() {
465   local context state line curcontext="$curcontext"
466
467   local _a rails_options runtime_options
468   __rails_setup_rails_options
469   __rails_setup_runtime_options
470
471   _arguments -C \
472     $rails_options \
473     $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'
490
491   case "$state" in
492     path_or_url)
493       _alternative \
494         'files:path:_files -g "*.rb"' \
495         'url:url:_urls'
496       ;;
497   esac
498 }
499
500 _rails_application() {
501   _rails_new
502 }
503
504 _rails_db() {
505   _rails_dbconsole
506 }
507
508 _rails_destroy() {
509   _rails_generate
510 }
511
512 _rails_d() {
513   _rails_destroy
514 }
515
516 _rails_benchmarker() {
517   _arguments \
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"'
523 }
524
525 _rails_profiler() {
526   _arguments \
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"'
533 }
534
535 _rails_plugin() {
536   local context state line curcontext="$curcontext"
537
538   if (( CURRENT > 2 )); then
539     (( CURRENT-- ))
540     shift words
541     _call_function - "_rails_plugin_${words[1]}" || _nothing
542   else
543     __rails_plugin_commands
544   fi
545 }
546
547 __rails_plugin_commands() {
548   _values 'plugin command' \
549     install'[Install plugin(s) from known repositories or URLs]' \
550     remove'[Uninstall plugins]' \
551     new
552 }
553
554 _rails_plugin_install() {
555   _arguments \
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]" \
562     '*:plugin:_urls'
563 }
564
565 _rails_plugin_remove() {
566   local -a plugins
567
568   plugins=($(_call_program rails_plugins ls -1 vendor/plugins))
569
570   _describe -t plugins 'plugin' plugins
571 }
572
573 _rails_plugin_new() {
574   _rails_new
575 }
576
577 _rails_runner() {
578   local context state line curcontext="$curcontext"
579
580   _arguments -C \
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)' \
583     ': :->code_or_path'
584
585   case "$state" in
586     code_or_path)
587       _alternative \
588         'files:filename:_files -g "*.rb"' \
589         'codes:ruby code:_guard "^-*" "ruby code"'
590       ;;
591   esac
592 }
593
594 _rails_r() {
595   _rails_runner
596 }
597
598 _rails_test() {
599   local context state line curcontext="$curcontext"
600
601   _arguments -C \
602     ': :->path'
603
604   case "$state" in
605     path)
606       _alternative \
607         'files:filename:_files -g "*.rb"'
608       ;;
609   esac
610 }
611
612 _rails_t() {
613   _rails_test
614 }
615
616 _rails "$@"
617
618 # Local Variables:
619 # mode: Shell-Script
620 # sh-indentation: 2
621 # indent-tabs-mode: nil
622 # sh-basic-offset: 2
623 # End:
624 # vim: ft=zsh sw=2 ts=2 et