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, #get_platform_from_requirements

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=, #extract_gem_name_and_version, #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.



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

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:



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

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

#defaults_strObject

:nodoc:



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

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

#descriptionObject

:nodoc:



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

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



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

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



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

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

#open_editor(path) ⇒ Object



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

def open_editor(path)
  system(*@editor.split(/\s+/) + [path], { chdir: path })
end

#open_gem(name) ⇒ Object



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

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



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

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:



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

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