How to bundle install via a CLI/Ruby system call?

泪湿孤枕 提交于 2020-01-01 11:56:10

问题


Is it possible to run bundle install from a ruby system call?

I'm trying to install gems and run tests for a project under another path...

For example the command is:

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

Ideally I want to just run the tests via a rake file in one project whilst making sure the relevant gems for that project are install.

The cd seems to be working correctly, if I run:

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

It does give the correct path. But if I do bundle install && gem environment, it seems to install the gems for the current folder and doesn't use the Gemfile from the other project, and subsequently the rspec spec doesn't work.

To summarize, what's the best way to run 'rspec spec' for example, for another project within a rakefile also ensuring the relevant gems are available?


回答1:


Actually it looks that the official way to achieve this behavior is like this:

Bundler.with_clean_env do
  system "shell out"
end    

I found the answer over at google groups: https://groups.google.com/d/msg/ruby-bundler/UufhzrliWfo/d51B_zARksUJ




回答2:


Edit: I think I have it figure out. see if this works for you:

#@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

Original Post: I am tearing my hair out with this. I think it has something to do with bundle exec|install|update setting environment variables when you start up the application, I have tried

bash -c "cd ../other/; bundle install; and it fails" I have tried using open3.popen("bundle install", :chdir=>"../other")

if it is any consolation you are not crazy, but I to can't seem to figure out how to fix it.

I also tried open3.popen("bundle install", {:chdir=>"../other", :unsetenv_others => false}) but this ends up up removing RVM from the system path;




回答3:


In addition to kangguru's answer, you might need to do

bundle install --deployment

So that the Bundler.with_clean_env doesn't get messed up by rvm. This installs copies of all your gems to .vendor/bundle in the root of your project, which is then picked up by the Bundler.with_clean_env command.

(Would have put this as a comment but I don't have 50+ reputation)



来源:https://stackoverflow.com/questions/8226617/how-to-bundle-install-via-a-cli-ruby-system-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!