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

a 夏天 提交于 2019-12-04 08:30:50

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

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;

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)

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