Search code examples
rubybundler

Programmatically determine gem's path using bundler


I know you can do

bundle show gem_name

to show the path of some gem.

How do you do that from within the code using the Bundler object?


Solution

  • Have a look at how they do it in cli.rb

    def locate_gem(name)
      spec = Bundler.load.specs.find{|s| s.name == name }
      raise GemNotFound, "Could not find gem '#{name}' in the current bundle." unless spec
      if spec.name == 'bundler'
        return File.expand_path('../../../', __FILE__)
      end
      spec.full_gem_path
    end