Sha256: b6294751a4f5915e58ca7eb418c9b72a836089146d4b3439b16192d20e422260

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

class Gem::Commands::OpenCommand < Gem::Command
  def description
    "Open a gem into your favorite editor"
  end

  def arguments
    "GEM       gem's name"
  end

  def usage
    "#{program_name} GEM"
  end

  def initialize
    super "open", description
  end

  def execute
    gemname = options[:args].first

    unless gemname
      say "Usage: #{usage}"
      return terminate_interaction
    end

    spec = search(gemname)

    if spec
      open(spec)
    else
      say "The #{gemname.inspect} gem couldn't be found"
      return terminate_interaction
    end
  end

  def dirs
    if Gem::Specification.respond_to?(:dirs)
      Gem::Specification.dirs
    else
      Gem::SourceIndex.installed_spec_directories
    end
  end

  def search(gemname)
    regex = /^(.*?)(-+[\d.]+[\w]*)?$/
    _, required_name, required_version = *gemname.match(regex)

    gemspecs = Dir["{#{dirs.join(",")}}/*.gemspec"].select do |gemspec|
      basename = File.basename(gemspec).gsub(/\.gemspec$/, "")

      if required_version
        basename == gemname
      else
        _, name, version = *basename.match(regex)
        name == gemname
      end
    end

    gemspec = gemspecs.sort.last

    return unless gemspec

    Gem::Specification.load(gemspec)
  end

  def open(spec)
    editor = ENV["GEM_EDITOR"] || ENV["EDITOR"]

    if editor
      system "#{editor} #{spec.full_gem_path}"
    else
      say "You must set your editor in your .bash_profile or equivalent:"
      say "  export GEM_EDITOR='mate'"
      say "or"
      say "  export EDITOR='mate'"
      return terminate_interaction
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem-open-0.1.6 lib/rubygems/commands/open.rb