Class: Gem::Commands::OpenCommand

Inherits:
Gem::Command show all
Includes:
VersionOption
Defined in:
lib/rubygems/commands/open_command.rb

Instance Attribute Summary

Attributes inherited from Gem::Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from VersionOption

#add_platform_option, #add_prerelease_option, #add_version_option

Methods inherited from Gem::Command

add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #begins?, build_args, build_args=, #check_deprecated_options, common_options, #deprecate_option, extra_args, extra_args=, #get_all_gem_names, #get_all_gem_names_and_versions, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #invoke_with_build_args, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, #when_invoked

Methods included from UserInteraction

#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction, #verbose

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Constructor Details

#initializeOpenCommand

Returns a new instance of OpenCommand.



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

def initialize
  super 'open', 'Open gem sources in editor'

  add_option('-e', '--editor COMMAND', String,
             "Prepends COMMAND to gem path. Could be used to specify editor.") do |command, options|
    options[:editor] = command || get_env_editor
  end
  add_option('-v', '--version VERSION', String,
             "Opens specific gem version") do |version|
    options[:version] = version
  end
end

Instance Method Details

#argumentsObject

:nodoc:



24
25
26
# File 'lib/rubygems/commands/open_command.rb', line 24

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

#defaults_strObject

:nodoc:



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

def defaults_str # :nodoc:
  "-e #{get_env_editor}"
end

#descriptionObject

:nodoc:



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

def description # :nodoc:
  <<-EOF
      The open command opens gem in editor and changes current path
      to gem's source directory.
      Editor command can be specified with -e option, otherwise rubygems
      will look for editor in $EDITOR, $VISUAL and $GEM_EDITOR variables.
  EOF
end

#executeObject



52
53
54
55
56
57
58
59
# File 'lib/rubygems/commands/open_command.rb', line 52

def execute
  @version = options[:version] || Gem::Requirement.default
  @editor  = options[:editor] || get_env_editor

  found = open_gem(get_one_gem_name)

  terminate_interaction 1 unless found
end

#get_env_editorObject



45
46
47
48
49
50
# File 'lib/rubygems/commands/open_command.rb', line 45

def get_env_editor
  ENV['GEM_EDITOR'] ||
    ENV['VISUAL'] ||
    ENV['EDITOR'] ||
    'vi'
end

#open_editor(path) ⇒ Object



74
75
76
77
78
# File 'lib/rubygems/commands/open_command.rb', line 74

def open_editor(path)
  Dir.chdir(path) do
    system(*@editor.split(/\s+/) + [path])
  end
end

#open_gem(name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubygems/commands/open_command.rb', line 61

def open_gem(name)
  spec = spec_for name

  return false unless spec

  if spec.default_gem?
    say "'#{name}' is a default gem and can't be opened."
    return false
  end

  open_editor(spec.full_gem_path)
end

#spec_for(name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rubygems/commands/open_command.rb', line 80

def spec_for(name)
  spec = Gem::Specification.find_all_by_name(name, @version).first

  return spec if spec

  say "Unable to find gem '#{name}'"
end

#usageObject

:nodoc:



41
42
43
# File 'lib/rubygems/commands/open_command.rb', line 41

def usage # :nodoc:
  "#{program_name} GEMNAME [-e COMMAND]"
end