-
Notifications
You must be signed in to change notification settings - Fork 0
/
rptr.rb
executable file
·113 lines (95 loc) · 2.67 KB
/
rptr.rb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env ruby1.9
# rptr points to installed versions
# rptr add <ruby dir> <alias>
# open or create usr space
require 'psych'
$home = ENV[ 'HOME' ]
$local = $home + '/.rptr'
$no_local = false
$help_commands = {
add: "rptr add <other ruby bin> <alias>",
where: "rptr where <standard ruby bin>",
use: "rptr use <alias>",
list: "rptr list",
implode: "rptr implode",
help: "rptr help"
}
begin
File.open( $local, "r" ) do |f|
$data = Psych.load( f.read )
end
rescue
$data = { rubies: {}, where: "" }
end
case ARGV[0]
when "add"
if ARGV.length != 3
puts $help[ :add ]
raise "incorrect usage of add"
end
path = ARGV[1]
if File.file?( path )
$data[ :rubies ][ ARGV[2] ] = path
else
raise "ruby:#{path} does not exist or is not a file"
end
when "where"
if ARGV.length != 2
puts $help[ :where ]
raise "incorrect usage of where"
end
where = ARGV[1]
if File.file?( where )
$data[ :where ] = where
else
raise "ruby:#{where} does not exist or is not a file"
end
when "use"
if ARGV.length != 2
puts $help[ :use ]
raise "Incorrect usage of use"
end
where = $data[ :where ]
path = $data[ :rubies ][ ARGV[1] ]
commands = %w{ erb gem irb rake rdoc ri ruby testrb }
if where == ""
puts "call: rptr where <ruby bin dur>"
raise "do not know where ruby is installed"
end
begin
commands.each do | value |
regex = /ruby(?!\/)/
dest = where.sub( regex, value )
source = path.sub( regex, value )
File.delete( dest )
File.symlink( source, dest )
#puts "delete:#{dest}"
#puts "symlink from:#{source} to:#{dest}"
end
rescue
raise "you need to be root"
end
when "list"
where = $data[:where]
if where == ""
puts "where:<ruby bin unknown>"
else
puts "where:#{$data[:where]}"
end
puts "rubies:"
$data[:rubies].each do | key, value |
puts " #{key}:#{value}"
end
when "implode"
File.delete( $local )
$no_local = true
when "help"
$help_commands.each_value do | value |
puts value
end
end
if $no_local == false
File.open( $local, 'w' ) do | f |
f.write Psych.dump( $data )
end
end