Class: Gem::Commands::EditCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption
Defined in:
lib/gemedit/edit_command.rb

Constant Summary collapse

EDITOR_ENV_VARS =
%w( GEMEDITOR BUNDLER_EDITOR VISUAL EDITOR )
OPTIONS =
{
  :version => Gem::Requirement.default,
  :verbose => false,
  :dryrun => false,
  :editor => EDITOR_ENV_VARS.map { |var| ENV[var] }.compact.first || 'vi'
}

Instance Method Summary collapse

Constructor Details

#initializeEditCommand

Returns a new instance of EditCommand.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gemedit/edit_command.rb', line 16

def initialize
  super 'edit', 'Open an installed gem in your favorite editor', OPTIONS

  add_version_option

  add_option('-e', '--editor EDITOR', String,
             'The editor to use to open the gems',
             'GEMEDITOR, BUNDLER_EDITOR, VISUAL and EDITOR',
             'environment variables are used to find the default',
             "Default: #{OPTIONS[:editor]}") do |editor, options|
    options[:editor] = editor
  end

  add_option('-d', '--[no-]dry-run',
             'Shows what command would be run without running it',
             'Turns on verbose logging', "Default: #{OPTIONS[:dryrun]}") do |dryrun, options|
    Gem.configuration.verbose = true if dryrun
    options[:dryrun] = dryrun
  end
end

Instance Method Details

#argumentsObject

:nodoc:



37
38
39
# File 'lib/gemedit/edit_command.rb', line 37

def arguments # :nodoc:
  "GEMNAME       name of gem to open in your favorite editor"
end

#defaults_strObject

:nodoc:



41
42
43
# File 'lib/gemedit/edit_command.rb', line 41

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

#executeObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gemedit/edit_command.rb', line 49

def execute
  version = options[:version] || OPTIONS[:version]
  begin
    # Gem -v >= 1.8
    if Gem::Version.create(Gem::VERSION) < Gem::Version.create("1.8")
      # Old Gem find_by_name syntax
      spec = Gem.source_index.find_name(get_one_gem_name, version).last
      raise "Gem not found error." unless spec
    else
      # New Gem find_by_name syntax
      spec = Gem::Specification.find_by_name(get_one_gem_name, version)
    end
    say "Opening the #{spec.name} gem, version #{spec.version}, with #{options[:editor]} from #{spec.full_gem_path}" if Gem.configuration.verbose
    cmd = "#{options[:editor]} ."
    Dir.chdir(spec.full_gem_path) do
      exec cmd
    end unless options[:dryrun]
  rescue Exception => e
    raise Gem::CommandLineError, "No gems found for #{options[:args].join(', ')}. #{e.message}"
  end
end

#usageObject

:nodoc:



45
46
47
# File 'lib/gemedit/edit_command.rb', line 45

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