mernen / completion-ruby

Command-line completion for Ruby-related commands under Bash: rake, gem, rails, ruby, jruby

This URL has Read+Write access

completion-ruby / completion-rails
100644 111 lines (98 sloc) 2.855 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
#! bash
# bash completion for the `rails` command and rails scripts.
#
# Copyright (c) 2008 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() {
  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
}
 
__rails_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
}
 
__rails_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
}
 
__rails_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) == "" ]]; then
__rails_complete_generators "$cur"
  fi
}
 
# returns the first non-option argument
__rails_get_command() {
  local i
  for ((i=1; i < $COMP_CWORD; ++i)); do
local arg=${COMP_WORDS[$i]}
 
    if [[ $arg != -* ]]; then
echo $arg
      return
fi
done
}
 
__rails_complete_environments() {
  # hardcoded list of environments... for now?
  local environments="development test production"
  COMPREPLY=($(compgen -W "$environments" -- "$1"))
}
 
__rails_complete_generators() {
  local generators="
controller integration_test mailer migration model observer
plugin resource scaffold session_migration"
  COMPREPLY=($(compgen -W "$generators" -- "$1"))
}
 
complete -F __rails -o default rails
complete -F __rails_script_server -o default script/server ./script/server
complete -F __rails_script_console -o default script/console ./script/console
complete -F __rails_script_generate -o default script/generate ./script/generate
# vim: ai ft=sh sw=2 sts=2 et