Sha256: b67151a5628d94d770ded26ee010af86fd4fb6e62582b8c455cd7cc232637f6d

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'rubygems/command'
require 'rubygems/version_option'
require 'open_gem/common_options'

# OpenCommand will open a gem's source path
class Gem::Commands::OpenCommand < Gem::Command
  include ::OpenGem::CommonOptions
  include Gem::VersionOption

  def initialize
    super 'open', "Opens the gem's source directory with $GEM_OPEN_EDITOR or $EDITOR",
      :command => nil,
      :version=>  Gem::Requirement.default,
      :latest=>   false

    add_command_option
    add_latest_version_option
    add_version_option
    add_exact_match_option
  end

  def arguments # :nodoc:
    "GEMNAME       gem to open"
  end

  def execute
    name = get_one_gem_name
    path = get_path(name)

    open_gem(path) if path
  end

  def get_path(name)
    if spec = get_spec(name)
      spec.full_gem_path
    end
  end

  def open_gem(path)
    editor = options[:command] || ENV['GEM_OPEN_EDITOR'] || ENV['VISUAL'] || ENV['EDITOR']
    if !editor
      say "Either set $GEM_OPEN_EDITOR, $VISUAL, $EDITOR, or use -c <command_name>"
    else
      command_parts = Shellwords.shellwords(editor)
      command_parts << path
      success = system(*command_parts)
      if !success
        raise Gem::CommandLineError, "Could not run '#{editor} #{path}', exit code: #{$?.exitstatus}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
open_gem-1.5.0 lib/rubygems/commands/open_command.rb