Class: Gem::Commands::OpenCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption, GemToolbox::CommonOptions
Defined in:
lib/rubygems/commands/open_command.rb

Overview

OpenCommand will open a gem’s source path This command is the same as open in open_gem

Instance Method Summary collapse

Methods included from GemToolbox::CommonOptions

#add_command_option, #add_exact_match_option, #add_latest_version_option, #get_path, #get_spec, #show

Constructor Details

#initializeOpenCommand

Returns a new instance of OpenCommand.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubygems/commands/open_command.rb', line 9

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

Instance Method Details

#argumentsObject

:nodoc:



21
22
23
# File 'lib/rubygems/commands/open_command.rb', line 21

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

#executeObject



25
26
27
28
29
30
# File 'lib/rubygems/commands/open_command.rb', line 25

def execute
  name = get_one_gem_name
  path = get_path(name)
  
  open_gem(path) if path
end

#open_gem(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubygems/commands/open_command.rb', line 32

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