LoginSignup
262
261

More than 5 years have passed since last update.

いちいちbundle execしたくない 決定版

Last updated at Posted at 2015-06-05

はじめに

Gemfileを使ってgemを管理してるプロジェクトで、日常的にbundle exec 何々ってやってて、alias be='bundle exec'とかしてるけど、beすら打つのが面倒、って状況よくありますよね。

結論から

2013/12/26にリリースされたRubyGems 2.2.0以降であれば、環境変数でRUBYGEMS_GEMDEPS=-とすることでbundle execを使わなくてもGemfileを読んでくれる。

少し追記

RUBYGEMS_GEMDEPSは"gem dependency file"へのパスを指定する機能。RUBYGEMS_GEMDEPS=MyGemfileであればカレントディレクトリからMyGemfileを読むことになる。カレントディレクトリからの相対パス、絶対パスともに使用可能。存在しないファイルを指定してもエラーは出ないので注意。

"-"だけが特別な指定方法でこれの場合、カレントディレクトリからgem.deps.rb,Gemfile,Isolateのいずれかを探し、見つからなければ親ディレクトリからこの3つを、まだなければ親の親から、と祖先のディレクトリをたどって探す。

またbundlerのGemfileで使えていたすべての記法が使えるわけではない?みたいなのでそこもご注意。

以下蛇足

rvm環境を作り直してて、bundle exec省く設定どうするんだっけとググると、
.rvm/hooks/after_cd_bundlerとbundle install --binstubsを組み合わせる方法が出てくる。
ところがこれやってると、bundlerからhttps://github.com/mpapis/rubygems-bundler 使えって言われた。

早速見に行くと一番上に、

Generally, this gem is not needed on RubyGems >= 2.2.0. Simply set the RUBYGEMS_GEMDEPS environment variable to either the path of your Gemfile or - to auto-discover in parent directories.

とあった。

RUBYGEMS_GEMDEPSをググる。

RubyGems can check for gem dependencies files (gem.deps.rb or Gemfile) when rubygems executables are started and uses the found dependencies. This means rake will work similar to bundle exec rake. To enable this set the RUBYGEMS_GEMDEPS environment variable to the location of your dependencies file.

If you set it to be RUBYGEMS_GEMDEPS=- (minus sign), it will look for a gemfile in the current directory (and deeper directories too, I think).

2013/12/26あたりに導入された機能らしい。日本語情報はびっくりするほどなかった。

早速試してみる。

gem -v
echo "puts 'hello gemfile.'" > Gemfile
ruby -e '' # なんもでない
RUBYGEMS_GEMDEPS=- ruby -e '' # hello gemfile.てでるから読まれてる
mkdir a
cd a
RUBYGEMS_GEMDEPS=- ruby -e '' # rakeみたいに親からも読まれるのでhello gemfile.がでる

ということで、どうやら読まれているらしいという結論に至る。RubyGemsの機能なのでrvmとかrbenvに依存しない、と思う。

262
261
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
262
261