RailsエンジニアのBashrc。開発環境を公開するよ

# この記事の対象
RailsをMacのコンソールから開発する人向けです

# 開発環境
次のものは適宜読み替えてください。
 - `$APP`はプロジェクトパス
 - `$EDITOR`はエディタを開くコマンド
 
```bash:~/.bashrc
# .bashrc
export APP=~/Desktop/SomeProject
export EDITOR=emacs
export PS1="\w >>"

alias h="history"
alias a="clear && cd $APP && git status && git log --oneline -5 && git stash list"
alias s='clear; find `pwd` -type f -print | xargs grep $1'
alias z='source ~/.bashrc'
alias zz='$EDITOR ~/.bashrc && z'
alias gb='git blame -w $1'
alias ru='rubocop -a'
alias dt="tail -f $APP/log/development.log"

# 危険ゾーン
alias db='mysql.server restart && pg_ctl restart'
alias re='bundle && rake db:migrate && rubocop -a && rspec spec'
alias xx="ps -ef | grep /Applications/Xcode.app/Contents/MacOS/Xcode | head -1 | awk '{print $2}' | xargs kill"
alias xxr="git add . && git reset --hard"
alias ppp='git checkout develop && git pull --rebase origin develop && bundle && rake db:migrate && git add . && git reset --hard'

# Rails専用。ディレクトリ飛ぶエイリアスを定義
#  e user active_admin => ActiveAdmin::Userのエイリアスが作られる
function e(){
    controller=undefined
    namespace=$2
    controllers=`ls $APP/app/views/$namespace`
    for v in ${controllers[@]}; do
        if [[ "${v}" == $1 ]]; then
            controller=$namespace/$v
        fi
    done
    if [[ $controller == undefined ]]; then
        for v in ${controllers[@]}; do
            if [[ "${v}" =~ $1 ]]; then
                controller=$namespace/$v
            fi
        done
    fi
    echo "controller ${controller}"

    model=undefined
    models=`ls $APP/app/models`
    for v in ${models[@]}; do
        if [[ "${v}" =~ $1 ]]; then
            if [[ "${v}" =~ $namespace ]]; then
                model=${v/%.*/}
            fi
        fi
    done
    echo "model ${model}"

    echo -en "\033];${controller}\007"

    # controllers
    alias f='$EDITOR  $APP/app/controllers/${controller}_controller.rb'
    alias ff='cd    $APP/app/controllers'
    # views
    alias d='cd     $APP/app/views/${controller}'
    alias dd='cd    $APP/app/views'
    # javascripts
    alias j='$EDITOR  $APP/app/assets/javascripts/${controller}.coffee'
    alias jj='cd    $APP/app/assets/javascripts'
    # stylesheets
    alias k='$EDITOR  $APP/app/assets/stylesheets/${controller}.scss'
    alias kk='cd    $APP/app/assets/stylesheets'
    # locals
    alias l='$EDITOR  $APP/config/locales/01_models/${model}.ja.yml'
    alias ll='cd    $APP/config/locales/01_models'
    # models
    alias m='$EDITOR  $APP/app/models/${model}.rb'
    alias mm='cd    $APP/app/models'
    alias c='cd     $APP/app/models/concerns'
    # decorators
    alias n='$EDITOR  $APP/app/decorators/${model}_decorator.rb'
    alias nn='cd    $APP/app/decorators'
    # spec
    alias sc='$EDITOR $APP/spec/controllers/${controller}_controller_spec.rb'
    alias sm='$EDITOR $APP/spec/models/${model}_spec.rb'
    alias sf='$EDITOR $APP/spec/factories/${controller}.rb'

    # task
    alias b='cd $APP/lib/tasks/'

    branchs=`cd $APP && git branch`
    for b in ${branchs[@]}; do
        if [[ "${b}" =~ ${controller:0:4} ]] ;then
            echo "branch     $b"
        fi
    done

    migrations=`ls $APP/db/migrate | grep $controller`;
    for v in ${migrations[@]}
    do
        if [[ "${v}" =~ ${controller:0:4} ]]; then
            echo "migrations $APP/db/migrate/$v"
            alias b='$EDITOR $APP/db/migrate/$v'
        fi
    done
}
```
e user active_admin