使用Bundler的binstub时安装gem的问题

时间:2012-12-14 15:28:55

标签: ruby-on-rails-3 rubygems rvm bundler

  • Bundler v1.2.3
  • RubyGems v1.8.24
  • RVM(最新)
  • Rails v3.2.9

我在我的Rails应用程序中使用binstubs并使用这些默认值(~/bundle/config):

---
BUNDLE_PATH: .bundle
BUNDLE_BIN: .bundle/bin

然后我将.bundle/bin添加到$PATH(通过chwd上的zsh脚本,所以它不是一个巨大的安全漏洞)所以我有正确的gem二进制文件。

除了两个问题之外,这基本上没问题。

第1期

当我进入应用并输入gem list时,我会看到全局安装的宝石列表(不是应用的宝石)。对于应用程序宝石,我需要输入bundle exec gem list并且它有效。我可以忍受。

第2期

我不能安装任何本地(应用程序的本地)宝石,它们位于捆绑包之外(即它们不在Gemfile中)。一个这样的例子是gem-ctags宝石。

理论上我可以将它安装到与所有其他本地宝石相同的目录中:

gem install --install-dir .bundle/ gem-ctags

但我无法使用它,即输入此命令:

☺ gem ctags
ERROR:  While executing gem ... (RuntimeError)
    Unknown command ctags

☹ bundle exec gem ctags
ERROR:  While executing gem ... (RuntimeError)
    Unknown command ctags

有没有办法让它发挥作用?

PS:

  • 当我将gem-ctags安装到全局宝石中然后执行gem ctags时,它可以正常工作)
  • 我知道rubygems-bundler但我宁愿只使用binstubs而不是使用它(除非没有其他办法......)

更新

第3期

gem cleanup不起作用,即使我正确设置了$ GEM_PATH(如@mpapis建议):

☺ gem cleanup                                                                       
Cleaning up installed gems...
Attempting to uninstall rake-10.0.0
Unable to uninstall rake-10.0.0:
    Gem::InstallError: gem "rake" is not installed
Attempting to uninstall ffi-1.1.5
Unable to uninstall ffi-1.1.5:
    Gem::InstallError: gem "ffi" is not installed
Attempting to uninstall dalli-2.2.1
Unable to uninstall dalli-2.2.1:
    Gem::InstallError: gem "dalli" is not installed
Clean Up Complete

当我输入gem install时,我可以看到所有这些宝石都已安装。

2 个答案:

答案 0 :(得分:1)

如果您想使用.bundle/,则需要将gem放入Gemfile

您正尝试在GEM_PATH之外使用rubygems插件,以使其正常工作:

export GEM_PATH="$GEM_PATH:$PWD/.bundle"

第3季更新:

根据帮助:

$ gem help cleanup
...
  Description:
    The cleanup command removes old gems from GEM_HOME.  If an older version is
    installed elsewhere in GEM_PATH the cleanup command won't touch it.

这意味着你需要这个:

export GEM_HOME="$PWD/.bundle"

作为副作用,它应该不再需要--install-dir .bundle/

只是你知道 - 你正在为bundler和rubygems做一些意想不到的事情,而且最终RVM还没有为你的流程做好准备。

答案 1 :(得分:0)

供参考,以下是我动态在zsh中将.bundle/bin添加到$PATH.bundle$GEM_PATH的方式,以使一切正常运行(即解决了上述问题):

export DEFAULT_GEM_HOME=$GEM_HOME

autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_add_binstubs_to_paths

function chpwd_add_binstubs_to_paths {
  # always delete from $OLDPWD (.bundle/bin/ from $PATH and .bundle/ from $GEM_PATH)
  export PATH=${PATH//$OLDPWD\/\.bundle\/bin:}
  export GEM_PATH=${GEM_PATH//$OLDPWD\/\.bundle:}
  export GEM_HOME=$DEFAULT_GEM_HOME

  if [ -r $PWD/Gemfile.lock ] && [ -d $PWD/.bundle/bin ]; then
    # add .bundle/bin to $PATH and .bundle/ to $GEM_PATH (deleting existing entries first)
    export PATH=$PWD/.bundle/bin:${PATH//$PWD\/\.bundle\/bin:}
    export GEM_PATH=$PWD/.bundle:${GEM_PATH//$PWD\/\.bundle:}
    export GEM_HOME=$PWD/.bundle
  fi
}

# initially execute `chpwd_add_binstubs_to_paths` as we might be opening a new shell in Rails project's directory
chpwd_add_binstubs_to_paths
相关问题