Sha256: 7a636302db141c65e7f4f9743ff639774efa1ef4f93ecff246390f6c6ce72045

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'rubygems/command'
require 'rubygems/version_option'

class Gem::Commands::ReadmeCommand < Gem::Command

  OPTIONS = {
    :version => Gem::Requirement.default,
    :editor => 'less'
  }

  include Gem::VersionOption

  def initialize
    super 'readme', 'Open a README of an installed gem', OPTIONS

    add_version_option

    add_option('-e', '--editor EDITOR', String,
               'The editor to use to open the gems',
               "Default: #{OPTIONS[:editor]}") do |editor, options|
      options[:editor] = editor
    end
  end

  def arguments # :nodoc:
    "GEMNAME       name of gem to open README"
  end

  def defaults_str # :nodoc:
    "--version '#{OPTIONS[:version]}' --editor #{OPTIONS[:editor]}"
  end

  def usage # :nodoc:
    "#{program_name} [options] GEMNAME [GEMNAME ...]"
  end

  def execute
    version = options[:version] || OPTIONS[:version]

    gem_specs = get_all_gem_names.map { |gem_name| Gem.source_index.find_name(gem_name, version).last }.compact

    if gem_specs.size > 0
      paths = gem_specs.map { |spec| spec.full_gem_path }

      readmes = paths.inject([]) do |result, path|
        result + Dir[File.join(path, '*')].select{ |i| File.basename(i) =~ /^readme/i }
      end

      if readmes.empty?
        say "README not found!"
        exit!
      end

      cmd = "#{options[:editor]} #{readmes.join(' ')}"
      exec cmd unless options[:dryrun]
    else
      say "No gems found for #{get_all_gem_names.join(', ')}"
      raise Gem::SystemExitException, 1
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem_readme-0.1.0 lib/gem_readme/readme_command.rb