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.



14
15
16
# File 'lib/rubygems/commands/open.rb', line 14

def initialize
  super "open", description
end

Instance Method Details

#argumentsObject



6
7
8
# File 'lib/rubygems/commands/open.rb', line 6

def arguments
  "GEM       gem's name"
end

#descriptionObject



2
3
4
# File 'lib/rubygems/commands/open.rb', line 2

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

#dirsObject



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

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

#executeObject



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubygems/commands/open.rb', line 66

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

  if editor
    system editor, spec.full_gem_path
  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



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

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



10
11
12
# File 'lib/rubygems/commands/open.rb', line 10

def usage
  "#{program_name} GEM"
end