rails 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2011 Wayne E. Seguin <wayneeseguin@gmail.com>
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # This feature is opt-in, in order to use the functions provided below source
  17. # this script in your start up from your profile(s).
  18. #
  19. # For example, if RVM is installed to $HOME/.rvm, you can put this in your
  20. # ~/.bash_profile:
  21. #
  22. # [[ -s "$HOME/.rvm/scripts/extras/rails" ]] && . "$HOME/.rvm/scripts/extras/rails"
  23. #
  24. enable_extendglob() {
  25. if [[ -n "${ZSH_VERSION:-}" ]] ; then
  26. setopt extendedglob
  27. else
  28. if [[ -n "${BASH_VERSION:-}" ]] ; then
  29. shopt -s extglob # Extended globs
  30. else
  31. printf "%b" "What the heck kind of shell are you running here???\n"
  32. fi
  33. fi
  34. }
  35. rails_routes()
  36. {
  37. # Usage:
  38. #
  39. # rails_routes <or list, space separated> [options]
  40. #
  41. # include string is a valid shell glob: http://mywiki.wooledge.org/glob
  42. #
  43. # options are among
  44. #
  45. # * a valid shell glob pattern
  46. # * -a <and glob>
  47. # * -o <or glob>
  48. # * -e <exclude (not) glob>
  49. #
  50. # Processing occurs as ((or list) AND (and list)) NOT (exclude list)
  51. #
  52. # Examples:
  53. #
  54. # rails_routes admin
  55. # /admin/reports/agent/:id(.:format) {:controller=>"reports", :action=>"agent"}
  56. #
  57. # rails_routes "P@(OS|U)T"
  58. # POST /current(.:format) {:action=>"create", :controller=>"current"}
  59. # PUT /current/:id(.:format) {:action=>"update", :controller=>"current"}
  60. #
  61. # or, equivalently, rails_routes POST -o PUT
  62. #
  63. # List all routes containing admin but not test
  64. # rails_routes admin -e test
  65. #
  66. # rails_routes
  67. enable_extendglob
  68. \typeset md5_current md5_cached cache_file routes
  69. \typeset -a ands ors nots
  70. while [[ $# -gt 0 ]] ; do
  71. token="$1" ; shift
  72. case "$token" in
  73. --trace)
  74. set -o xtrace
  75. export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()} \${LINENO} > "
  76. ;;
  77. -a|and)
  78. ands=(${ands[@]} "$1")
  79. shift
  80. ;;
  81. -o|or)
  82. ors=(${ors[@]} "$1")
  83. shift
  84. ;;
  85. -e|exclude)
  86. nots=(${exclude[@]} "$1")
  87. shift
  88. ;;
  89. *) # Default is or.
  90. ors=(${ors[@]} "$token")
  91. ;;
  92. esac
  93. done
  94. [[ -d tmp/rake ]] || mkdir -p "tmp/rake"
  95. cache_file="tmp/rake/routes"
  96. if ! builtin command -v rake >/dev/null 2>&1 ; then
  97. printf "%b" "ERROR: rake not found.\n" >&2
  98. return 1
  99. fi
  100. case ${_system_type} in
  101. Linux) md5="md5sum" ;;
  102. Darwin) md5="/sbin/md5 -q" ;;
  103. *) printf "%b" "ERROR: Unknown OS Type.\n" >&2 ; return 1 ;;
  104. esac
  105. md5_current=$($md5 config/routes.rb)
  106. md5_current=${md5%% *}
  107. if [[ -s "$cache_file" && -s "$cache_file.md5" ]]; then
  108. md5_cached=$(\command \cat -v "${cache_file}.md5")
  109. md5_cached=${md5%% *}
  110. else
  111. md5_cached=""
  112. fi
  113. if [[ -s "$cache_file" && $md5_current == $md5_cached ]]; then
  114. routes=$(\command \cat -v "$cache_file")
  115. else
  116. routes=$(rake routes 2>/dev/null)
  117. if [[ $? -gt 0 ]]; then
  118. printf "%b" "ERROR: There was an error running 'rake routes', does your application load console properly?" >&2
  119. return 1
  120. else
  121. printf "%b" "$md5_current" > "${cache_file}.md5"
  122. printf "%b" "$routes" > "$cache_file"
  123. fi
  124. fi
  125. routes="${routes#\|\(in *\)}"
  126. orig_ifs="$IFS"
  127. condition="${ors[@]}"
  128. condition="*${condition// /*|*}*"
  129. IFS="
  130. "
  131. # ORs
  132. results=()
  133. for route in ${routes} ; do
  134. eval "case '${route}' in (${condition}) results=(\"${results[@]}\" \"${route}\") ;; esac"
  135. done
  136. routes=(${results[@]})
  137. # ANDs
  138. results=()
  139. for condition in "${ands[@]}" ; do
  140. for route in ${routes[@]} ; do
  141. if [[ ${route} == ${condition} ]]; then
  142. results=(${results[@]} ${route})
  143. fi
  144. done
  145. routes=(${results[@]})
  146. results=()
  147. done
  148. # NOTs
  149. results=()
  150. for condition in "${nots[@]}" ; do
  151. for route in ${routes[@]} ; do
  152. if [[ ${route} != *${condition}* ]]; then
  153. results=(${results[@]} ${route})
  154. fi
  155. done
  156. routes=(${results[@]})
  157. results=()
  158. done
  159. for route in ${routes[@]} ; do
  160. printf "%b" "${route}\n"
  161. done
  162. IFS="$orig_ifs"
  163. }
  164. #
  165. # r - Rails scripts helper function.
  166. #
  167. r() {
  168. \typeset action args
  169. action="$1" ; shift
  170. args="$@"
  171. enable_extendglob
  172. case "$action" in
  173. c|console)
  174. action=console
  175. ;;
  176. d|db)
  177. action=dbconsole
  178. ;;
  179. g|generate)
  180. action=generate
  181. ;;
  182. d|destroy)
  183. action=destroy
  184. ;;
  185. s|server)
  186. action=server
  187. ;;
  188. r|routes)
  189. rails_routes $args
  190. return $?
  191. ;;
  192. n|new)
  193. rails_version=$(rails -v)
  194. rails_version=${rails_version##* }
  195. rails_version=${rails_version%%.*}
  196. if [[ $rails_version -ge 3 ]]; then
  197. rails new $args
  198. elif [[ $rails_version == 1 || $rails_version == 2 ]] ; then
  199. rails $args
  200. fi
  201. ;;
  202. (*(-)@(h|?|help))
  203. action="-h"
  204. ;;
  205. (*(-)@(v|V|version))
  206. action="-h"
  207. ;;
  208. usage)
  209. printf "%b" "
  210. r - Rails shell convenience function
  211. Usage:
  212. r [action]
  213. Actions:
  214. c - Start a Rails Console
  215. d - Start a database console
  216. c - rails generate
  217. s - rails server
  218. r - rails routes <include filter glob> [-e <exclude filter glob>]
  219. \n"
  220. ;;
  221. esac
  222. if [[ -s ./bin/rails ]] ; then # For Rails4 or bundler's binstubs
  223. ruby ./bin/rails $action $args
  224. elif [[ -s ./script/rails ]] ; then
  225. ruby ./script/rails $action $args
  226. elif [[ -s ./script/$action ]] ; then
  227. ruby ./script/$action $args
  228. else
  229. printf "%b" "ERROR: rails $action not found!!!"
  230. fi
  231. return 0
  232. }