summaryrefslogtreecommitdiff
path: root/plugins/rails/_rails
blob: ac90d45cc4650b0094cf8348b8e98481b11c3e1c (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#compdef rails
# ------------------------------------------------------------------------------
# Copyright (c) 2016 GitHub zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for Ruby on Rails (http://rubyonrails.org/).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Kazuya Takeshima (https://github.com/mitukiii)
#
# ------------------------------------------------------------------------------


_rails() {
  local context state line curcontext="$curcontext"

  if (( CURRENT > 2 )); then
    (( CURRENT-- ))
    shift words
    _call_function - "_rails_${words[1]}" || _nothing
  else
    __rails_commands
  fi
}

__rails_commands() {
  local context state line curcontext="$curcontext"

  local -a rails_options
  __rails_setup_rails_options

  _arguments -C \
    $rails_options \
    ': :->command'

  case "$state" in
    command)
      local -a commands
      local application_directory
      __rails_setup_application_directory

      if [ -n "$application_directory" ]; then
        commands=(
          {generate,g}'[Generate new code]'
          {console,c}'[Start the Rails console]'
          {server,s}'[Start the Rails server]'
          {dbconsole,db}'[Start a console for the database specified in config/database.yml]'
          application'[Generate the Rails application code]'
          {destroy,d}'[Undo code generated with "generate"]'
          benchmarker'[See how fast a piece of code runs]'
          profiler'[Get profile information from a piece of code]'
          plugin'[Install a plugin]'
          {runner,r}'[Run a piece of code in the application environment]'
          {test,t}'[Run tests]'
        )
      else
        commands=(
          new'[Create a new Rails application]'
        )
      fi

      _values 'command' $commands
      ;;
  esac
}

__rails_setup_application_directory() {
  application_directory="$(pwd)"

  while [ -n "$application_directory" ]; do
    if [ -f "${application_directory}/script/rails" -o -f "${application_directory}/bin/rails" ]; then
      return
    fi
    application_directory="${application_directory%/*}"
  done

  application_directory=
}

__rails_setup_rails_options() {
  rails_options=(
    {-h,--help}'[Show this help message and quit]'
    {-v,--version}'[Show Rails version number and quit]'
  )
}

__rails_setup_runtime_options() {
  runtime_options=(
    '(-f --force)'{-f,--force}'[Overwrite files that already exist]'
    '(-p --pretend)'{-p,--pretend}'[Run but do not make any changes]'
    '(-q --quiet)'{-q,--quiet}'[Suppress status output]'
    '(-s --skip)'{-s,--skip}'[Skip files that already exist]'
  )
}

__rails_setup_generators_options() {
  local -a runtime_options
  __rails_setup_runtime_options

  generators_options=(
    $runtime_options
    --skip-namespace'[Skip namespace (affects only isolated applications)]'
    --old-style-hash"[Force using old style hash (:foo => 'bar') on Ruby >= 1.9]"
  )
}

__rails_setup_model_generators_options() {
  local -a generators_options
  __rails_setup_generators_options

  model_generators_options=(
    $generators_options
    '(-o --orm)'{-o,--orm=}'[Orm to be invoked]:orm'
  )
}

__rails_setup_resource_generators_options() {
  local -a model_generators_options
  __rails_setup_model_generators_options

  resource_generators_options=(
    $model_generators_options
    --force-plural'[Forces the use of a plural ModelName]'
    --resource-route'[Indicates when to generate resource route]: :__rails_boolean'
  )
}

__rails_boolean() {
  _values 'boolean' 'true' 'false'
}

__rails_migration_fields() {
  if compset -P '*:*:'; then
    _values 'index' 'index' 'uniq'
  else
    if compset -P '*:'; then
      _values -s ':' 'type' 'string' 'text' 'integer' 'float' 'decimal' 'datetime' 'timestamp' 'time' 'date' 'binary' 'boolean' 'references'
    else
      _guard '[[:alnum:]_]#' 'field'
    fi
  fi
}

_rails_generate() {
  local context state line curcontext="$curcontext"

  if (( CURRENT > 2 )); then
    (( CURRENT-- ))
    shift words
    _call_function - "_rails_generate_${words[1]}" || _rails_generate_default
  else
    __rails_generate_commands
  fi
}

_rails_g() {
  _rails_generate
}

__rails_generate_commands() {
  local context curcontext="$curcontext" update_policy

  zstyle -s ":completion:${curcontext}:" cache-policy update_policy
  if [ -z "$update_policy" ]; then
    zstyle ":completion:${curcontext}:" cache-policy _rails_generate_commands_caching_policy
  fi

  local application_directory
  __rails_setup_application_directory
  local cache_name
  cache_name="rails/${application_directory##*/}/all_generators"
  if ! _retrieve_cache ${cache_name}; then
    local -a all_generators
    all_generators=($(_call_program rails_generators rails generate 2> /dev/null | awk '/^  [a-zA-Z_]+/{ print $1 }'))
    _store_cache ${cache_name} all_generators
  fi

  local -a rails_generators
  rails_generators=(${all_generators:#*:*})
  _describe -t rails_generators 'rails generator' rails_generators

  local -a -U namespaces
  local namespace
  local -a generators
  namespaces=(${(R)${(M)all_generators:#*:*}%:*})
  for namespace in $namespaces; do
    generators=(${${(M)all_generators:#${namespace}:*}/:/\\:})
    _describe -t ${namespace}_generators "${namespace/_/ } generator" generators
  done
}

_rails_generate_commands_caching_policy() {
  local application_directory
  __rails_setup_application_directory

  if [ "${application_directory}/Gemfile" -nt "$1" ]; then
    return 0
  fi

  local -a oldp
  oldp=( "$1"(Nmw+1) )
  (( $#oldp ))
}

_rails_generate_default() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    '*:argument'
}

_rails_generate_assets() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    '(-j --javascripts)'{-j,--javascripts}'[Generate JavaScripts]: :__rails_boolean' \
    '(-y --stylesheets)'{-y,--stylesheets}'[Generate Stylesheets]: :__rails_boolean' \
    '(-je --javascript-engine)'{-je,--javascript-engine=}'[Engine for JavaScripts]:javascript engine' \
    '(-se --stylesheet-engine)'{-se,--stylesheet-engine=}'[Engine for Stylesheets]:stylesheet engine' \
    ': :_guard "^-*" "name"'
}

_rails_generate_controller() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
    '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
    --helper'[Indicates when to generate helper]: :__rails_boolean' \
    --assets'[Indicates when to generate assets]: :__rails_boolean' \
    ': :_guard "^-*" "name"' \
    '*: :_guard "^-*" "action"'
}

_rails_generate_generator() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    --namespace'[Namespace generator under lib/generators/name]: :__rails_boolean' \
    ': :_guard "^-*" "name"'
}

_rails_generate_helper() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
    ': :_guard "^-*" "name"' \
}

_rails_generate_integration_test() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    --integration-tool='[Integration tool to be invoke]:integration tool' \
    ': :_guard "^-*" "name"' \
}

_rails_generate_jbuilder() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    ': :_guard "^-*" "name"' \
    '*: :__rails_migration_fields'
}

_rails_generate_mailer() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
    '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
    ': :_guard "^-*" "name"' \
    '*: :_guard "^-*" "method"'
}

_rails_generate_migration() {
  local -a modelgenerators_options
  __rails_setup_model_generators_options

  _arguments \
    $model_generators_options \
    ': :_guard "^-*" "name"' \
    '*: :__rails_migration_fields'
}

_rails_generate_model() {
  _rails_generate_migration
}

_rails_generate_observer() {
  local -a model_generators_options
  __rails_setup_model_generators_options

  _arguments \
    $model_generators_options \
    ': :_guard "^-*" "name"'
}

_rails_generate_performance_test() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    --performance-tool='[Performance tool to be invoked]:performance tool' \
    ': :_guard "^-*" "name"' \
}

_rails_generate_resource() {
  local context state line curcontext="$curcontext"

  local -a resource_generators_options
  __rails_setup_resource_generators_options

  _arguments -C \
    $resource_generators_options \
    '(-c --resource-controller)'{-c,--resource-controller=}'[Resource controller to be invoked]:name' \
    '(-a --actions)'{-a,--actions=}'[Actions for the resource controller]: :->actions' \
    ': :->name' \
    '*: :->fields'

  if (( words[(I)(--actions=*|-a)] > 0 && words[(I)(--actions=*|-a)] == words[(I)-*] )); then
    state=actions
  fi

  case "$state" in
    actions)
      _guard "[[:alnum:]_]#" "actions"
      ;;
    name)
      _guard "^-*" "name"
      ;;
    fields)
      __rails_migration_fields
      ;;
  esac
}

_rails_generate_scaffold() {
  local -a resource_generators_options
  __rails_setup_resource_generators_options

  _arguments \
    $resource_generators_options \
    '(-y --stylesheets)'{-y,--stylesheets}'[Generate Stylesheets]: :__rails_boolean' \
    '(-se --stylesheet-engine)'{-se,--stylesheet-engine=}'[Engine for Stylesheets]:stylesheet engine' \
    '(-c --scaffold-controller)'{-c,--scaffold-controller=}'[Scaffold controller to be invoked]:name' \
    --assets'[Indicates when to generate assets]:boolean:(true false)' \
    ': :_guard "^-*" "name"' \
    '*: :__rails_migration_fields'
}

_rails_generate_scaffold_controller() {
  local -a model_generators_options
  __rails_setup_model_generators_options

  _arguments \
    $model_generators_options \
    '(-e --template-engine)'{-e,--template-engine=}'[Template engine to be invoked]:template engine' \
    '(-t --test-framework)'{-t,--test-framework=}'[Test framework to be invoked]:test framework' \
      --helper'[Indicates when to generate helper]: :__rails_boolean' \
    ': :_guard "^-*" "name"'
}

_rails_generate_session_migration() {
  local -a model_generators_options
  __rails_setup_model_generators_options

  _arguments \
    $model_generators_options \
    ': :_guard "^-*" "name"'
}

_rails_generate_task() {
  local -a generators_options
  __rails_setup_generators_options

  _arguments \
    $generators_options \
    ': :_guard "^-*" "name"' \
    '*: :_guard "^-*" "action"'
}

_rails_console() {
  _arguments \
    '(- *)'{-h,--help}'[Show this help message]' \
    '(-s --sandbox)'{-s,--sandbox}'[Rollback database modifications on exit]' \
    --debugger'[Enable ruby-debugging for the console]'
}

_rails_c() {
  _rails_console
}

_rails_server() {
  _arguments \
    '(- *)'{-h,--help}'[Show this help message]' \
    '(-p --port)'{-p,--port=}'[Runs Rails on the specified port]: :_guard "[[\:digit\:]]#" "port"' \
    '(-b --binding)'{-b,--binding=}'[Binds Rails to the specified ip]:ip:_hosts' \
    '(-c --config)'{-c,--config=}'[Use custom rackup configuration file]:file:_files -g "*.ru"' \
    '(-d --daemon)'{-d,--daemon}'[Make server run as a Daemon]' \
    '(-u --debugger)'{-u,--debugger}'[Enable ruby-debugging for the server]' \
    '(-e --environment)'{-e,--environment=}'[Specifies the environment to run this server under (test/development/production)]:name:(test development production)' \
    '(-P --pid)'{-P,--pid=}'[Specifies the PID file]:pid:_files -g "*.pid"'
}

_rails_s() {
  _rails_server
}

_rails_dbconsole() {
  _arguments \
    '(- *)'--help'[Show this help message]' \
    '(-p --include-password)'{-p,--include-password}'[Automatically provide the password from database.yml]' \
    --mode'[Automatically put the sqlite3 database in the specified mode (html, list, line, column)]:mode:(html list line column)' \
    --header
}

_rails_new() {
  local context state line curcontext="$curcontext"

  local _a rails_options runtime_options
  __rails_setup_rails_options
  __rails_setup_runtime_options

  _arguments -C \
    $rails_options \
    $runtime_options \
    '(-r --ruby)'{-r,--ruby=}'[Path to the Ruby binary of your choice]:path' \
    '(-b --builder)'{-b,--builder=}'[Path to a application builder (can be a filesystem path or URL)]: :->path_or_url' \
    '(-m --template)'{-m,--template=}'[Path to an application template (can be a filesystem path or URL)]: :->path_or_url' \
    --skip-gemfile"[Don't create a Gemfile]" \
    --skip-bundle"[Don't run bundle install]" \
    '(-G --skip-git)'{-G,--skip-git}'[Skip Git ignores and keeps]' \
    '(-O --skip-active-record)'{-O,--skip-active-record}'[Skip Active Record files]' \
    '(-S --skip-sprockets)'{-S,--skip-sprockets}'[Skip Sprockets files]' \
    '(-d --database)'{-d,--database=}'[Preconfigure for selected database]:database:(mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc)' \
    '(-j --javascript)'{-j,--javascript=}'[Preconfigure for selected JavaScript library]:javascript' \
    '(-J --skip-javascript)'{-J,--skip-javascript}'[Skip JavaScript files]' \
    --dev'[Setup the application with Gemfile pointing to your Rails checkout]' \
    --edge'[Setup the application with Gemfile pointing to Rails repository]' \
    '(-T --skip-test-unit)'{-T,--skip-test-unit}'[Skip Test::Unit files]' \
    --old-style-hash"[Force using old style hash (:foo => 'bar') on Ruby >= 1.9]" \
    ':app path:_directories'

  case "$state" in
    path_or_url)
      _alternative \
        'files:path:_files -g "*.rb"' \
        'url:url:_urls'
      ;;
  esac
}

_rails_application() {
  _rails_new
}

_rails_db() {
  _rails_dbconsole
}

_rails_destroy() {
  _rails_generate
}

_rails_d() {
  _rails_destroy
}

_rails_benchmarker() {
  _arguments \
    '(- *)'{-h,--help}'[Show this help message]' \
    '(-r --runs)'{-r,--runs}'[Number of runs]: :_guard "[[\:digit\:]]#" "number"' \
    '(-o --output)'{-o,--output}'[Directory to use when writing the results]:directory:_directories' \
    '(-m --metrics)'{-m,--metrics}'[Metrics to use]: :_values -s "," "metrics" "wall_time" "memory" "objects" "gc_runs" "gc_time"' \
    '*: :_guard "^-*" "ruby code"'
}

_rails_profiler() {
  _arguments \
    '(- *)'{-h,--help}'[Show this help message]' \
    '(-r --runs)'{-r,--runs}'[Number of runs]: :_guard "[[\:digit\:]]#" "number"' \
    '(-o --output)'{-o,--output}'[Directory to use when writing the results]:directory:_directories' \
    '(-m --metrics)'{-m,--metrics}'[Metrics to use]: :_values -s "," "metrics" "process_time" "memory" "objects"' \
    '(-f --formats)'{-f,--formats}'[Formats to output to]: :_values -s "," "formats" "flat" "graph" "html" "call_tree" "call_stack"' \
    '*: :_guard "^-*" "ruby code"'
}

_rails_plugin() {
  local context state line curcontext="$curcontext"

  if (( CURRENT > 2 )); then
    (( CURRENT-- ))
    shift words
    _call_function - "_rails_plugin_${words[1]}" || _nothing
  else
    __rails_plugin_commands
  fi
}

__rails_plugin_commands() {
  _values 'plugin command' \
    install'[Install plugin(s) from known repositories or URLs]' \
    remove'[Uninstall plugins]' \
    new
}

_rails_plugin_install() {
  _arguments \
    '(-x --externals)'{-x,--externals}'[Use svn:externals to grab the plugin. Enables plugin updates and plugin versioning]' \
    '(-o --checkout)'{-o,--checkout}'[Use svn checkout to grab the plugin. Enables updating but does not add a svn:externals entry]' \
    '(-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]' \
    '(-q --quiet)'{-q,--quiet}'[Suppresses the output from installation. Ignored if -v is passed (rails plugin -v install ...)]' \
    '(-r --revision)'{-r,--revision=}'[Checks out the given revision from subversion or git. Ignored if subversion/git is not used]:revision' \
    '(-f --force)'{-f,--force}"[Reinstalls a plugin if it's already installed]" \
    '*:plugin:_urls'
}

_rails_plugin_remove() {
  local -a plugins

  plugins=($(_call_program rails_plugins ls -1 vendor/plugins))

  _describe -t plugins 'plugin' plugins
}

_rails_plugin_new() {
  _rails_new
}

_rails_runner() {
  local context state line curcontext="$curcontext"

  _arguments -C \
    '(- *)'{-h,--help}'[Show this help message]' \
    '(-e --environment)'{-e,--environment=}'[Specifies the environment for the runner to operate under (test/development/production)]:name:(test development production)' \
    ': :->code_or_path'

  case "$state" in
    code_or_path)
      _alternative \
        'files:filename:_files -g "*.rb"' \
        'codes:ruby code:_guard "^-*" "ruby code"'
      ;;
  esac
}

_rails_r() {
  _rails_runner
}

_rails_test() {
  local context state line curcontext="$curcontext"

  _arguments -C \
    ': :->path'

  case "$state" in
    path)
      _alternative \
        'files:filename:_files -g "*.rb"'
      ;;
  esac
}

_rails_t() {
  _rails_test
}

_rails "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et