Jeremiah
Jeremiah

Reputation: 13

Programmatically get gem versions from Bundler

Over in this question, an answer was given to analyze the Bundler dependency list. That works great, but it doesn't give you the list of packages and versions actually being used, because of ">=" dependencies. Is there a way to get the list of packages and versions actually being used rather than just what the dependencies are?

Upvotes: 1

Views: 1654

Answers (2)

Joseph Ravenwolfe
Joseph Ravenwolfe

Reputation: 6600

This code was extracted from the Bundler codebase and will do the exact same thing as bundle list from within a Rails console.

Bundler.load.specs.sort_by(&:name).each{|s| puts "  * #{s.name} (#{s.version}#{s.git_version})"}; nil

If you just want an array of the dependencies, this will also suffice.

Bundler.load.specs.map{|s| "#{s.name} (#{s.version}#{s.git_version})"}

Upvotes: 2

MrEvil
MrEvil

Reputation: 8083

Looks like the way to do this is similar to what was posted in the other question:

 Rails.logger.debug "Type is " + Bundler.environment.specs.class.to_s
 Rails.logger.debug "Value is " + Bundler.environment.specs.to_hash.to_s

Produces:

    Type is Bundler::SpecSet
    Value is {"activemodel"=>[#<Gem::Specification name=activemodel version=3.1.3>],
              "actionpack"=>[#<Gem::Specification name=actionpack version=3.1.3>],
              "actionmailer"=>[#<Gem::Specification name=actionmailer version=3.1.3>]}

This code will print out all of the gems and versions being used in your current environment. One thing to note about the answer in that other question is that it will return all of the dependencies, even those that aren't in your current rails environment (for example, the ones that are in your "test" gem grouping).

Upvotes: 2

Related Questions