-
Notifications
You must be signed in to change notification settings - Fork 1
/
gembr
executable file
·67 lines (54 loc) · 1.46 KB
/
gembr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
#
# A little shortcut for browsing source for gems.
# !sequitur : 2006.12.15
require 'rubygems'
GEMS_ROOT = File.join Gem.dir, 'gems'
GEMS_DIR = Dir.new GEMS_ROOT
GEM_NOT_FOUND = "Gem not found in #{GEMS_ROOT}"
# Verifies that the given gem name exists.
# If a version is not specified, grabs the most recent.
def find(gem)
GEMS_DIR.entries.select { |name| name.include? gem }.last
end
# If a valid name with a bad version is passed,
# return a list of known versions.
def find_similar(gem)
gem_name = /^([^-]+)/.match(gem)[1] # everything before the first '-'
GEMS_DIR.entries.select { |g| g =~ /^#{gem_name}/ }
end
# Puts a numbered list of gems.
def list(similar)
similar.each_with_index { |gem,i| puts "#{i + 1}) #{gem}" }
end
# Prompts the user to select from a list of similar
# gem names, and returns the selected name.
def get(similar)
valid = 1..similar.size
puts "Invalid selection. Enter #{valid.first} - #{valid.last}" while ! valid.include?(selection = gets.to_i)
similar[selection-1]
end
# Launches the default text editor, passing the path
# to the gem as the only argument.
def browse!(gem)
system "#{ENV['EDITOR']} #{File.join GEMS_ROOT, gem}"
end
###
if ARGV.empty?
puts 'USAGE: gembr gemname'
exit
end
gem = if gem_arg = ARGV.shift
find gem_arg
else
puts GEM_NOT_FOUND
unless (similar = find_similar GEM_ARG).empty?
list similar
get similar
end
end
if gem
browse! gem
else
puts GEM_NOT_FOUND
end