通过CLI/Ruby系统调用捆绑安装

eas*_*yjo 5 ruby bundler gemfile

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

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

例如,命令是:

"cd /some/other/project && bundle install && gem list && rspec spec"
Run Code Online (Sandbox Code Playgroud)

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

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

"cd /some/other/project && pwd"
Run Code Online (Sandbox Code Playgroud)

它确实提供了正确的路径.但是,如果我捆绑安装&& gem环境,它似乎安装当前文件夹的gem,并且不使用其他项目的Gemfile,随后rspec规范不起作用.

总结一下,运行'rspec spec'的最佳方法是什么,对于rakefile中的另一个项目,还确保相关的gem可用?

小智 6

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

Bundler.with_clean_env do
  system "shell out"
end    
Run Code Online (Sandbox Code Playgroud)

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


lea*_*eat 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
Run Code Online (Sandbox Code Playgroud)

原帖:我对此感到抓狂。我认为这与启动应用程序时的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 ;