LoginSignup
4
4

More than 5 years have passed since last update.

Railsローカルインストールからプロジェクト作成の半自動化

Last updated at Posted at 2014-09-27

RailsすらもBundlerで管理したい場合は、作業が少しばかり面倒です。
その作業をシェルでやれば便利だと思ったので書いてみました。

なお、Ruby環境がまだの方は、Javaエンジニアがいまさら始めるRuby開発環境のrbenvまでを行い、この記事をやるとプロジェクトごとのrails環境が作成できるはずです。

function gi() { curl https://www.gitignore.io/api/$@ ;}

function rails_new(){

    while read -p "Please enter project name: " railsName ; do
        if [ -n "$railsName" ] ; then
            break
        fi
    done

    while read -p "Whether use Test::Unit[y/n] " skipTest ; do
        case $skipTest in
            [Yy]* ) 
                skipTest=''
                break
                ;; 
            [Nn]* ) 
                skipTest='-T'
                break
                ;;
            *)        
            echo "Please answer yes or no."
            ;;
        esac
    done

    read -p "Please enter rails version: " version 

    if [ -n "$version" ] ; then
        version=", \"$version\""
    fi

    cat << EOS > Gemfile
source "http://rubygems.org"
gem "rails"$version
EOS

    bundle install --path vendor/bundle
    bundle exec rails new $railsName --skip-bundle $skipTest 

    rm -f Gemfile
    rm -f Gemfile.lock
    rm -rf .bundle
    rm -rf vendor
    cd $railsName

    if [ "$skipTest" != "" ] ; then
    cat << EOS >> Gemfile
gem 'rspec-rails', group: [:development, :test]
gem 'factory_girl_rails', group: [:development, :test]
EOS
    fi
    bundle install --path vendor/bundle

    if [ "$skipTest" != "" ] ; then
        bundle exec rails g rspec:install
    fi

    gi rails > .gitignore

}

これを .bash_profile に書いて、

$ rails_new

で、対話式に「プロジェクト名」、「railsのバージョン」、「Test::Unitを使うかどうか(使わない場合はRSpecを利用する)」を入力するとRailsプロジェクトが作成されます。
ついでに、https://gitignore.io のapiを使ってrails用の .gitignore も作成しています。

参考
Rails開発環境の構築(rbenvでRuby導入からBundler、Rails導入まで)

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4