Programmatically determine gem's path using bundler

前端 未结 4 1427
借酒劲吻你
借酒劲吻你 2021-02-13 02:37

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?<

4条回答
  •  伪装坚强ぢ
    2021-02-13 03:35

    Update: starting with Bundler v1.3.0, there is a public interface for obtaining a Gem's path:

    Bundler.rubygems.find_name('json').first.full_gem_path
    # => "/opt/src/foo/my_app/vendor/bundle/ruby/2.0.0/gems/json-1.8.0"
    

    Pre-v1.3.0, you may want to use the original solution shared (a private interface):

    Better yet, you can use Bundler::CLI#locate_gem directly:

    require "bundler/cli"
    Bundler::CLI.new.send(:locate_gem, "json")
    # => "/opt/src/foo/my_app/vendor/bundle/ruby/1.9.1/gems/json-1.7.3"
    

提交回复
热议问题