Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Last active May 25, 2022 01:59
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozy4324/5719555 to your computer and use it in GitHub Desktop.
Save kozy4324/5719555 to your computer and use it in GitHub Desktop.
Bundlerめも

bundler

install

Gemfile(or Gemfile.lock)の記述に従って依存gemをシステムにインストール.

$ bundle install

システムではなく任意のパスにgemをインストール.

$ bundle install --path vendor/bundle

任意のパスにインストールした場合、ロードパス解決はbundlerによって行われる. コマンド実行する場合はbundle execを利用.

$ bundle exec rspec

Rubyコード上でロードパスを追加する場合.

require 'bundler/setup'
require 'nokogiri'

任意のグループのみ対象とする場合はBundler.setupメソッドを利用する.

require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'

グループまとめてrequireするBundler.requireメソッドもある.

require 'bundler'
Bundler.require(:default, Rails.env)

--path指定でinstallした後は設定が保存され、以降のコマンドは該当パスに対して実行される. システムへのインストールに戻す--systemオプションもある.

$ bundle install --system

なお保存された設定はbundle configコマンドで確認することができ、実体は.bundle/configに書き出される.

$ bundle config
Settings are listed in order of priority. The top value will be used.

path
Set for your local app (/path/to/project/.bundle/config): "vendor/bundle"

disable_shared_gems
Set for your local app (/path/to/project/.bundle/config): "1"

bin
Set for your local app (/path/to/project/.bundle/config): "./bin"

$ cat .bundle/config 
---
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_BIN: ./bin

なお.bundleフォルダはバージョン管理には含めずGemfileGemfile.lockはバージョン管理する.

update

installしたgemはGemfile.lockでバージョンが固定される. それを無視して最新のgemを取得するにはupdateを利用する.

$ bundle update          # update all gems
$ bundle update nokogiri # nokogiri gem only

deploying

Gemfile.lockの内容に従って、システムや他アプリケーションとは隔離して依存gemをインストールする.

$ bundle install --deployment

$ bundle packageしておくとその環境でインストールした.gemvender/cacheにぶち込んでくれて、--deployment時はcacheからインストールを行う.

$ bundle package
$ git add vendor/cache

Capistranoを利用しているのであればdeploy.rbに以下を記述して$ cap deployでOK.

require 'bundler/capistrano'

Gemfile

autorequiringではGemfileに記述したgem名がrequireされる. gem名がrack-cacheだけど実際にrequireするのがrake/cacheな場合は:requireオプションを利用.

gem 'rack-cache', :require => 'rack/cache'

また複数requireしなければならないケースではArrayで指定すればよい.

gem "redis", :require => ["redis/connection/hiredis", "redis"]

autorequiringされなくするにはfalseを指定すればよい.

gem "webmock", :require => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment