user2490003
user2490003

Reputation: 11900

How to run `bundle check` and `bundle install` from inside a script without using the command line?

I can run the following two commands from my bash shell -

 bundle check --gemfile=/path/to/some/Gemfile
 bundle install --gemfile=/path/to/some/Gemfile

How do I run these from inside a Ruby script without shelling out (e.g. without using ``, system(), exec(), etc..)?

Specifically, does Bundler offer some API like Bundler.check() where I can call this process and receive output on whether it was successful or not?

Thanks!

EDIT I'm aware that bundler/inline provides a way to specify a gemfile block within the same file, but the scope of this question is how to run check and install on any arbitrary Gemfile

Upvotes: 2

Views: 1223

Answers (1)

MichaelC
MichaelC

Reputation: 357

This should do the trick, from within a Ruby Script

  • the definition line loads the default Gemfile
  • then we try to resolve all the dependencies locally, with resolve_with_cache
  • if necessary, we try to resolve the dependencies remotely, which does mean installing necessary gems
require 'bundler'
definition = Bundler.definition
begin
  definition.resolve_with_cache!
rescue Bundler::GemNotFound => e
  puts "missing dependencies, let's try remotely"
  begin
    definition.resolve_remotely!
  rescue
    puts "damn ! could not satisfy dependencies !"
  end
end

Upvotes: 1

Related Questions