Bundle install via CLI/Ruby system call

2019-04-08 17:25发布

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?

3条回答
Fickle 薄情
2楼-- · 2019-04-08 17:43

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楼-- · 2019-04-08 17:50

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)

查看更多
干净又极端
4楼-- · 2019-04-08 18:02

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

查看更多
登录 后发表回答