Programmatically check if gem in bundle?

Jared Beck

At runtime, after bundler setup is complete, and groups are applied, what is the best way to programmatically check if a given gem_name is in the bundle?

By reading the source, I've discovered Bundler.definition, e.g.

gem_name = 'banana'
Bundler.definition.dependencies.map(&:name).include?(gem_name)

but unable to find documentation1, I don't know if this is the recommended usage.

Update: It looks like Bundler::Definition#dependencies returns all dependencies, irrespective of groups. As an alternative, I've discovered Bundler::Runtime#dependencies_for which takes groups as arguments.

Bundler.load.dependencies_for(:default, Rails.env).map(&:name).include?(gem_name)

However, it seems like a bad idea to duplicate the "group lists" at every call site. Ideally, I'd like a method in Bundler that doesn't require me to specify the current groups.

1 The bundler website and man page are focused on command-line usage. I have not found any documentation on the gem's public ruby API. Comments in the source are helpful, but focused on data types, etc.

matt

Bundler is used to set up the applications gems, so you can use the Gem API rather than Bundler:

Gem.loaded_specs.has_key? gem_name

Bundler will have set things up so that any gems in the bundle (in the appropriate groups) have been activated (so they will have entries in loaded_specs) and any other (non-bundle) gems will be prevented from being loaded.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Crazy gem and bundle trouble

From Dev

Ruby programmatically installing a gem

From Dev

Ruby -- Programmatically Install Gem

From Dev

gem install on bundle folder (vendor/bundle)

From Dev

Bundle Install will not update eventmachine gem

From Dev

'Bundle' issues with ruby gem 'gamebox'

From Dev

gem install bundler, bundle not working

From Java

Obtain bundle identifier programmatically in Swift?

From Dev

Programmatically changing OSGi bundle imports

From Dev

Cocoon Gem Add Item Programmatically

From Dev

Cocoon Gem Add Item Programmatically

From Java

Bundler: can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException) during bundle install with gem

From Dev

How to check if a gem is installed?

From Dev

bundle vs npm approach or why does bundle/gem installs globally?

From Dev

bundle vs npm approach or why does bundle/gem installs globally?

From Dev

/bundle install' giving error after running 'bundle gem [gemname]'

From Dev

Rails: gem file/ bundle install with gem 'bootstrap-sass'

From Dev

bundle gem: rspec says 'No examples found'

From Dev

Gem install pg works but bundle install fails

From Dev

What is the difference between "bundle update" and "gem update"?

From Dev

Bundle Install Fails with unicorn gem on kgio install

From Dev

Why does `gem install` work but not `bundle install`

From Dev

bundle update seems to be deleting my .gem file

From Dev

can't bundle install json gem in ubuntu

From Dev

bundle gem: rspec says 'No examples found'

From Dev

Gem Debugger won't install with bundle install

From Dev

event machine gem bundle install is not working

From Dev

Why does `gem install` work but not `bundle install`

From Dev

Error installing hitimes gem as part of Rails bundle

Related Related

  1. 1

    Crazy gem and bundle trouble

  2. 2

    Ruby programmatically installing a gem

  3. 3

    Ruby -- Programmatically Install Gem

  4. 4

    gem install on bundle folder (vendor/bundle)

  5. 5

    Bundle Install will not update eventmachine gem

  6. 6

    'Bundle' issues with ruby gem 'gamebox'

  7. 7

    gem install bundler, bundle not working

  8. 8

    Obtain bundle identifier programmatically in Swift?

  9. 9

    Programmatically changing OSGi bundle imports

  10. 10

    Cocoon Gem Add Item Programmatically

  11. 11

    Cocoon Gem Add Item Programmatically

  12. 12

    Bundler: can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException) during bundle install with gem

  13. 13

    How to check if a gem is installed?

  14. 14

    bundle vs npm approach or why does bundle/gem installs globally?

  15. 15

    bundle vs npm approach or why does bundle/gem installs globally?

  16. 16

    /bundle install' giving error after running 'bundle gem [gemname]'

  17. 17

    Rails: gem file/ bundle install with gem 'bootstrap-sass'

  18. 18

    bundle gem: rspec says 'No examples found'

  19. 19

    Gem install pg works but bundle install fails

  20. 20

    What is the difference between "bundle update" and "gem update"?

  21. 21

    Bundle Install Fails with unicorn gem on kgio install

  22. 22

    Why does `gem install` work but not `bundle install`

  23. 23

    bundle update seems to be deleting my .gem file

  24. 24

    can't bundle install json gem in ubuntu

  25. 25

    bundle gem: rspec says 'No examples found'

  26. 26

    Gem Debugger won't install with bundle install

  27. 27

    event machine gem bundle install is not working

  28. 28

    Why does `gem install` work but not `bundle install`

  29. 29

    Error installing hitimes gem as part of Rails bundle

HotTag

Archive