Sha256: 6c9fcf4434ba0cbaeee57c6a25c3eccce51d63e0ed58a315e5db41d95a8efbb2

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

class Gem::Commands::PathCommand < Gem::Command
  def description
    'Find the path for a given gem or require path'
  end

  def initialize
    super('path', description)
  end

  def execute
    name, version = extract!(options[:args])

    # find the exact gem
    if path_gem = find_path_gem(name)
      say(path_gem)

    # find the require file
    elsif path = Gem.find_files("#{name}.rb").first
      # favor gem first (e.g. rake gem)
      if path_gem = Gem.path.find{ |p|
           break $1 if path =~ %r{(#{p}(/[^/]+){2})}
         }
        say(path_gem)
      else
        say(path)
      end
    else
      alert_error("#{name} not found")
      terminate_interaction(1)
    end
  end

  private
  def extract! args
    gem(*args) if args.size > 1 # only gem has versions
    args
  rescue LoadError => e
    alert_error(e.message)
    terminate_interaction(1)
  end

  def find_path_gem name
    path_gem = Gem.path.find do |base|
      path_gem = $LOAD_PATH.find do |path|
                   path_gem = path[%r{#{base}/gems/#{name}\-[^/-]+/}]
                   break path_gem if path_gem
                 end
      break path_gem if path_gem
    end
    path_gem[0...-1] if path_gem
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem-path-0.5.0 lib/rubygems/commands/path_command.rb