If I have a local gem (my_gem.gem) how can I get the name and version information from code or the command line without installing the gem. This has nothing to do with Bundler or the Gemfile. A gem has a gemspec which is required to list the name and version and it is what Rubygems uses when installing so there must be APIs to get this information.
Reason: I'm installing a user gem to validate it and want to uninstall it to clean up. User gem is not something I control so I can't depend on naming conventions.
CodePudding user response:
You can see locked version in Gemfile.lock
cat Gemfile.lock | grep gem-name
Other option, but need bundle install
first
bundle exec gem dependency | grep gem-name
CodePudding user response:
A Gemfile
does not (necessarily) specify an exact version of a dependency. It might specify nothing (i.e. "any version is fine"), or ~> 1.0
(i.e. >= 1.0
and < 2.0
), or whatever. Also, dependency constraints might further restrict the valid range of versions.
I'm assuming that this isn't what you meant by your question. Instead, you'd like to know what exact versions of dependencies will be installed by running bundle install
, given a Gemfile.lock
.
One way to achieve this reliably (i.e. rather than using grep
and eyeballing which line(s) are most relevant) is by parsing the Gemfile.lock
:
require 'bundler'
lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
puts lockfile.specs.find { |spec| spec.name == 'the-gem-you-want-to-check' }.version