This repository has been archived by the owner on Aug 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemtool.rb
251 lines (208 loc) · 6.44 KB
/
gemtool.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/ruby
GEMTOOL_VERSION = "1.0.0"
require 'optparse'
class String
# Returns the color with given code
def code(code)
"\033[#{code.to_s}m#{self}\033[0m"
end
# Returns the string in red color
def red
self.code 31
end
# Returns the string in green color
def green
self.code 32
end
# Returns the string in yellow color
def yellow
self.code 33
end
# Return the string in bold
def bold
self.code 1
end
end
def status
print "#{($?.success? ? "[DONE]".green : "[FAIL]".red)}\n"
end
trap("SIGINT") do
puts "\nShutdown signal sent. Quitting ...".red
$stdout.flush
sleep(2)
exit
end
options = {}
opts = OptionParser.new do |p|
p.banner = "Usage: gemtool [actions | [options] commands]\n\n Commands:\n"
p.on('-i', '--install FILE', "Install the gems from the given gem list") do |file|
options[:command] = "install"
options[:file] = file
end
p.on('-u', '--uninstall FILE', "Uninstall the gems from the given gem list") do |file|
options[:command] = "uninstall"
options[:file] = file
end
p.on('-t', '--update', "Updates all the outdated gems") do
options[:command] = "update"
end
p.on('-p', '--prune', "Removes all the older versions of gems while keeping that latest intact") do
options[:command] = "prune"
end
p.on('-c', '--clean FILE', "Make your gem list equal to given gem list\n\n Actions:") do |file|
options[:command] = "clean"
options[:file] = file
end
p.on('-v', '--version', 'Version of gemtool') do
puts "Gemtool version: #{GEMTOOL_VERSION}"
exit
end
p.on('-h', '--help', "Display this screen\n\n Options:") do
puts p
exit
end
options[:doc] = false
p.on('-d', '--[no-]doc', "Install documentation (default false)") do |n|
options[:doc] = n
end
options[:source] = nil
p.on('-s', '--source URL', "Use URL as the remote source for gems") do |url|
options[:source] = url
end
end
begin
opts.parse! ARGV
rescue OptionParser::MissingArgument
puts opts
exit
else
if options[:command].nil?
puts opts
exit
else
gemopts = ""
gemopts << " --no-rdoc --no-ri" unless options[:doc]
gemopts << " --source #{options[:source]}" unless options[:source].nil?
case options[:command]
when "install"
target = File.open(options[:file])
while gem = target.gets
gem =~ /(.+) \((.*)\)\n/
name = $1
versions = $2.split(', ')
versions.each do |ver|
print "Checking #{name}-#{ver} ".ljust(50)
gem_installed = `gem list #{name} -i -v #{ver} 2>/dev/null`
if gem_installed.chomp == "true"
print "\t["+"Installed".green+"]\n"
else
print "\t["+"Not Installed".red+"]\n"+"\tInstalling ...... ".yellow
$stdout.flush
install = `gem install #{name} -v #{ver}#{gemopts} 2>/dev/null`
status
end
end
end
when "uninstall"
target = File.open(options[:file])
while gem = target.gets
gem =~ /(.+) \((.*)\)\n/
name = $1
versions = $2.split(', ')
versions.each do |ver|
print "Checking #{name}-#{ver} ".ljust(50)
gem_installed = `gem list #{name} -i -v #{ver} 2>/dev/null`
if gem_installed.chomp == "false"
print "\t["+"Not Installed".green+"]\n"
else
print "\t["+"Installed".red+"]\n"+"\tUninstalling ...... ".yellow
$stdout.flush
uninstall = `gem uninstall -a -I -x #{name} -v #{ver} 2>/dev/null`
status
end
end
end
when "clean"
print "Building target list \t "
target = File.open(options[:file])
target_list = []
while gem = target.gets
gem =~ /(.+) \((.*)\)\n/
name = $1
versions = $2.split(', ')
versions.each do |ver|
target_list << "#{name} -v #{ver}"
end
end
print "[DONE]\n".green
print "Getting gem list \t "
$stdout.flush
source = `gem list 2>/dev/null`
source_list = []
status
source.each do |gem|
gem =~ /(.+) \((.*)\)\n/
name = $1
versions = $2.split(', ')
versions.each do |ver|
source_list << "#{name} -v #{ver}"
end
end
to_install = target_list - source_list
puts "Installing required gems" unless to_install.empty?
to_install.each do |gem|
print "\tInstalling #{gem.split(' -v ')[0]}-#{gem.split(' -v ')[1]} ...".ljust(50).yellow
$stdout.flush
install = `gem install #{gem}#{gemopts} 2>/dev/null`
status
end
to_uninstall = source_list - target_list
puts "Uninstalling unrequired gems" unless to_uninstall.empty?
to_uninstall.each do |gem|
print "\tUninstalling #{gem.split(' -v ')[0]}-#{gem.split(' -v ')[1]} ...".ljust(50).yellow
$stdout.flush
uninstall = `gem uninstall -a -I -x #{gem} 2>/dev/null`
status
end
if to_install.empty? && to_uninstall.empty?
puts "\nNo changes to be made"
end
when "update"
print "Getting outdated gem list \t "
$stdout.flush
target = `gem outdated 2>/dev/null`
status
target.each do |gem|
gem =~ /(.+) \((.*)\)/
name = $1
versions = $2.split(' < ')
print "\tUpdating #{name} from #{versions[0]} to #{versions[1]}".ljust(50).yellow
$stdout.flush
update = `gem install #{name} -v #{versions[1]}#{gemopts} 2>/dev/null`
status
end
when "prune"
print "Getting gem list \t "
$stdout.flush
target = `gem list 2>/dev/null`
status
target.each do |gem|
gem =~ /(.+) \((.*)\)\n/
name = $1
versions = $2.split(', ')
print "Checking #{name} ...".ljust(50)
if versions.count==1
print "\t["+"No old versions".green+"]\n"
else
print "\t["+"#{versions.count-1} old versions".rjust(15).red+"]\n"
versions.slice(1..-1).each do |ver|
print "\tPruning #{ver} ...... ".yellow
$stdout.flush
prune = `gem uninstall -a -I -x #{name} -v #{ver} 2>/dev/null`
status
end
end
end
end
end
end