Class: Gem::Commands::OpenCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/open.rb

Instance Method Summary collapse

Constructor Details

#initializeOpenCommand

Returns a new instance of OpenCommand.



16
17
18
# File 'lib/rubygems/commands/open.rb', line 16

def initialize
  super "open", description
end

Instance Method Details

#argumentsObject



8
9
10
# File 'lib/rubygems/commands/open.rb', line 8

def arguments
  "GEM       gem's name"
end

#descriptionObject



4
5
6
# File 'lib/rubygems/commands/open.rb', line 4

def description
  "Open a gem into your favorite editor"
end

#dirsObject



38
39
40
41
42
43
44
# File 'lib/rubygems/commands/open.rb', line 38

def dirs
  if Gem::Specification.respond_to?(:dirs)
    Gem::Specification.dirs
  else
    Gem::SourceIndex.installed_spec_directories
  end
end

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubygems/commands/open.rb', line 20

def execute
  gemname = options[:args].first

  unless gemname
    say "Usage: #{usage}"
    return terminate_interaction
  end

  spec = search(gemname)

  if spec
    open(spec)
  else
    say "The #{gemname.inspect} gem couldn't be found"
    return terminate_interaction
  end
end

#open(spec) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubygems/commands/open.rb', line 68

def open(spec)
  editor = ENV["GEM_EDITOR"] || ENV["EDITOR"]

  if editor
    path = spec.full_gem_path
    Dir.chdir(path) do
      system(*Shellwords.split(editor), path)
    end
  else
    say "You must set your editor in your .bash_profile or equivalent:"
    say "  export GEM_EDITOR='mate'"
    say "or"
    say "  export EDITOR='mate'"
    return terminate_interaction
  end
end

#search(gemname) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubygems/commands/open.rb', line 46

def search(gemname)
  regex = /^(.*?)(-+[\d.]+[\w]*)?$/
  _, required_name, required_version = *gemname.match(regex)

  gemspecs = Dir["{#{dirs.join(",")}}/*.gemspec"].select do |gemspec|
    basename = File.basename(gemspec).gsub(/\.gemspec$/, "")

    if required_version
      basename == gemname
    else
      _, name, version = *basename.match(regex)
      name == gemname
    end
  end

  gemspec = gemspecs.sort.last

  return unless gemspec

  Gem::Specification.load(gemspec)
end

#usageObject



12
13
14
# File 'lib/rubygems/commands/open.rb', line 12

def usage
  "#{program_name} GEM"
end