5

是否可以从 ruby​​ 系统调用运行 bundle install ?

我正在尝试为另一个路径下的项目安装 gem 并运行测试......

例如命令是:

"cd /some/other/project && bundle install && gem list && rspec spec"

理想情况下,我只想通过一个项目中的 rake 文件运行测试,同时确保安装了该项目的相关 gem。

如果我运行,cd 似乎工作正常:

"cd /some/other/project && pwd"

它确实给出了正确的路径。但是,如果我执行 bundle install && gem 环境,它似乎会为当前文件夹安装 gem,并且不使用来自其他项目的 Gemfile,并且随后 rspec 规范不起作用。

总而言之,例如,对于 rakefile 中的另一个项目,运行“rspec spec”的最佳方式是什么,同时确保相关的 gem 可用?

4

3 回答 3

6

实际上看起来实现这种行为的官方方法是这样的:

Bundler.with_clean_env do
  system "shell out"
end    

我在 google 群组中找到了答案:https ://groups.google.com/d/msg/ruby-bundler/UufhzrliWfo/d51B_zARksUJ

于 2012-09-20T14:08:03.437 回答
0

除了 kangguru 的回答,你可能需要做

bundle install --deployment

这样 Bundler.with_clean_env 就不会被 rvm 搞砸了。这会将所有 gem 的副本安装到项目根目录中的 .vendor/bundle 中,然后由 Bundler.with_clean_env 命令获取。

(会将此作为评论,但我没有 50+ 的声誉)

于 2014-01-31T14:02:25.680 回答
0

编辑:我想我已经弄清楚了。看看这是否适合你:

#@pwd is the "working directory of the execution...

Dir.chdir @pwd do
  so = ""
  vars = {
         "BUNDLE_GEMFILE" => nil,
         "BUNDLE_BIN_PATH" => nil,
         "RUBYOPT" => nil,
         "rvm_" => nil,
         "RACK_ENV" => nil,
         "RAILS_ENV" => nil,
         "PWD" => @pwd 
       }
  options = {
            :chdir=>@pwd
          }
  Open3.popen3(vars, cmd, options) do |stdin, stdout, stderr|
    stdin.close_write
    so = stdout.read
    so = stderr.read if so.nil? || so == ""
  end

  so
end

原帖:我用这个把头发扯掉了。我认为它与启动应用程序时 bundle exec|install|update 设置环境变量有关,我试过了

bash -c "cd ../other/; bundle install; and it fails" 我试过使用 open3.popen("bundle install", :chdir=>"../other")

如果有什么安慰的话,你并没有发疯,但我似乎无法弄清楚如何解决它。

我也试过 open3.popen("bundle install", {:chdir=>"../other", :unsetenv_others => false}) 但这最终会从系统路径中删除 RVM;

于 2012-08-17T06:22:57.227 回答