LoginSignup
1
0

Gemfileに追記せずに、自分だけ使うgemを追加

Last updated at Posted at 2024-04-17

Railsのプロジェクトで、
peformance測定とか、debugのgemを入れたいとか...
チームの方針でirbになっているが、自分はpryを使いたいときとか...

方法1. .pryrcからinlineで呼び出す

~/.pryrc
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'awesome_print'
  gem 'pry-doc'
end

AwesomePrint.pry!

方法2. Gemfile.localを作成する方法

1の方法だと、rails serverを立ち上げたときなどは、gemが呼び出されません:cry:
なので方法2を紹介しますが、ちょっと複雑なので、基本的に方法1をおすすめします

1. プロジェクトのルートディレクトリにGemfile.localを作成

Gemfile.local
gem 'awesome_print'
gem 'pry-doc'

eval_gemfile 'Gemfile'

2. bundleコマンドを上書き

Gemfile.localがあった場合、BUNDLE_GEMFILE=Gemfile.localの環境変数が追加します
しかし、Gemfile.local.lockが作成され、Gemfile.lockが管理されなくなります
対策として、bundle install時は2回行います
bundle update時は、Gemfileだけを見に行きます

~/.zshrc
bundle() {
  if [ -f Gemfile.local ]; then
    if [[ $1 == install || -z $@ ]]; then
      bundle "$@"
      BUNDLE_GEMFILE=Gemfile.local command bundle "$@"
    elif [[ $1 == update ]]; then
      command bundle "$@"
    else
      BUNDLE_GEMFILE=Gemfile.local command bundle "$@"
    fi
  else
    command bundle "$@"
  fi
}

3. .gitignoreに追加

~/.config/git/ignore
Gemfile.local
Gemfile.local.lock

最後に

運用歴浅いので、コメントしてってーーー

関連:

1
0
1

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
1
0