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, #deprecated?, 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, 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.


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

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:


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

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

#defaults_strObject

:nodoc:


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

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

#descriptionObject

:nodoc:


29
30
31
32
33
34
35
36
# File 'lib/rubygems/commands/open_command.rb', line 29

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


49
50
51
52
53
54
55
56
# File 'lib/rubygems/commands/open_command.rb', line 49

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


42
43
44
45
46
47
# File 'lib/rubygems/commands/open_command.rb', line 42

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

#open_editor(path) ⇒ Object


71
72
73
74
75
# File 'lib/rubygems/commands/open_command.rb', line 71

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

#open_gem(name) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubygems/commands/open_command.rb', line 58

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


77
78
79
80
81
82
83
# File 'lib/rubygems/commands/open_command.rb', line 77

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:


38
39
40
# File 'lib/rubygems/commands/open_command.rb', line 38

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