Permalink
Browse files

Moved rails CLI helper functions script from contrib to scripts/extras.

  • Loading branch information...
wayneeseguin committed Mar 3, 2011
1 parent af7d8a2 commit e4f2b6811e2ed3748dd4154b6bbc00a06f6929cd
Showing with 0 additions and 158 deletions.
  1. +0 −158 contrib/rails
View
@@ -1,158 +0,0 @@
-#!/usr/bin/env bash
-
-# Copyright (c) 2011 Wayne E. Seguin <wayneeseguin@gmail.com>
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-#
-# To use the functions provided below source this script in your start up
-# profiles. For example, if RVM is installed to $HOME/.rvm:
-#
-# [[ -s "$HOME/.rvm/contrib/rails" ]] && . "$HOME/.rvm/contrib/rails"
-#
-rails_routes()
-{
- # Usage:
- #
- # rails_routes [shell glob or search string]
- #
- # Examples:
- #
- # rails_routes admin
- # /admin/reports/agent/:id(.:format) {:controller=>"reports", :action=>"agent"}
- #
- # rails_routes "P@(OS|U)T"
- # POST /current(.:format) {:action=>"create", :controller=>"current"}
- # PUT /current/:id(.:format) {:action=>"update", :controller=>"current"}
- #
- if [[ "$1" = "--trace" ]]; then
- set -o xtrace
- export PS4='+[${BASH_SOURCE}] : ${LINENO} : ${FUNCNAME[0]:+${FUNCNAME[0]}() $ }'
- shift
- fi
-
- if [[ -n "${ZSH_VERSION:-}" ]] ; then
- setopt extendedglob
- else
- if [[ -n "${BASH_VERSION:-}" ]] ; then
- shopt -s extglob # Extended globs
- else
- printf "%s\n" "What the heck kind of shell are you running here???"
- fi
- fi
-
- local md5_current md5_cached cache_file routes search_string
-
- search_string="${1:-}" ; shift
- search_string="*${search_string//\|/*|*}*"
-
- cache_file="tmp/.rake_routes"
-
- if ! command -v rake >/dev/null 2>&1 ; then
- printf "ERROR: rake not found.\n" >&2
- return 1
- fi
-
- case $(uname) in
- Linux) md5="md5sum" ;;
- Darwin) md5="/sbin/md5 -q" ;;
- *) printf "ERROR: Unknown OS Type.\n" >&2 ; return 1 ;;
- esac
-
- md5_current=$($md5 config/routes.rb)
- md5_current=${md5%% *}
-
- if [[ -s "$cache_file" && -s "$cache_file.md5" ]]; then
- md5_cached=$(cat -v "${cache_file}.md5")
- md5_cached=${md5%% *}
- else
- md5_cached=""
- fi
-
- if [[ -s "$cache_file" && $md5_current = $md5_cached ]]; then
- routes=$(cat -v "$cache_file")
- else
- routes=$(rake routes 2>/dev/null)
- if [[ $? -gt 0 ]]; then
- printf "ERROR: There was an error running 'rake routes', does your application load console properly?" >&2
- return 1
- else
- printf "$md5_current" > "${cache_file}.md5"
- printf "$routes" > "$cache_file"
- fi
- fi
- routes="${routes#\|\(in *\)}"
-
- (IFS="
-"
- for route in ${routes} ; do
- eval "case '$route' in (${search_string}) printf \"\$route\\n\" ;; esac"
- done
- )
-}
-
-#
-# r - Rails scripts helper function.
-#
-r() {
- local action args
-
- action="$1" ; shift
-
- args="$@"
-
- case "$action" in
-
- c)
- action=console
- ;;
-
- db)
- action=dbconsole
- ;;
-
- g)
- action=generate
- ;;
-
- routes|r)
- rails_routes $*
- return $?
- ;;
-
- h|help)
- action="-h"
- ;;
-
- esac
-
- if [[ -s ./script/rails ]] ; then
-
- ruby ./script/rails $args
-
- else
-
- if [[ -s ./script/$action ]] ; then
-
- ruby ./script/$action $args
-
- else
-
- printf "ERROR: Missing script ./script/$action !!!"
-
- fi
-
- fi
-
- return 0
-}

0 comments on commit e4f2b68

Please sign in to comment.