文章出處

bundle在ruby的世界里是個好東西,它可以用來管理應用程序的依賴庫。它能自動的下載和安裝指定的gem,也可以隨時更新指定的gem。

rvm則是一個命令行工具,能幫助你輕松的安裝,管理多個ruby環境。每個環境可以指定一系列的gem。它允許你為每一個項目指定其ruby的版本,需要的gem的版本。這能最大限度的避免由于ruby環境的差異,或者不同版本的gem造成的各種問題。

當我在項目中引入了rvm后,使用rake命令時,每次都會出現這樣的異常。

1
2
3
4
5
6
7
8
rake aborted!
You have already activated rake 10.0.0, but your Gemfile requires rake 0.9.2.2. Using bundle exec may solve this.
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.3/lib/bundler/runtime.rb:31:in `block in setup'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.3/lib/bundler/runtime.rb:17:in `setup'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.3/lib/bundler.rb:116:in `setup'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.3/lib/bundler/setup.rb:7:in `<top (required)>'
/Users/twer/sourcecode/octopress/Rakefile:2:in `<top (required)>'
(See full trace by running task with --trace)

從這個異常中我們可以看到,由于我在自己機器上已經安裝了rake的10.0.0版本,但是這個項目中配置的rake版本卻是0.9.2.2,所以在執行rake命令時應該使用Gemfile的。

bundle exec可以在當前bundle的上下文中執行一段腳本。通過它可以調用本項目中指定的rake版本來執行命令。

下面是官方文檔的說明。

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle. However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

所以我們只要每次執行命令的時候加上bundle exec的前綴就可以額。但是這樣搞的很煩人,試想每天都要敲上百次這樣的字符,而且還常常忘記。

有一個方法可以避免每次都要鍵入bundle exec作為前綴。

安裝rubygems-bundler

1
$ gem install rubygems-bundler

然后運行:

1
$ gem regenerate_binstubs

啟用RVM hook:

1
2
$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler

為自己的項目創建bundler stubs.

1
2
$ cd your_project_path
$ bundle install --binstubs=./bundler_stubs

最后重新打開terminal就可以直接執行ruby命令,不需要加上bundler exec前綴。

如果想禁用次bundler的話,只需要在命令前面加上NOEXEC_DISABLE=1前綴。更詳細的信息可以看[rubygems-bundler]的文檔


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()