-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgrade-all.rb
executable file
·588 lines (445 loc) · 10.4 KB
/
upgrade-all.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/usr/bin/env ruby
#encoding: utf-8
#frozen_string_literal: true
#
# upgrade-all.rb
# binfiles
#
# Created by Stephen H. Gerstacker on 2021-12-14.
# Copyright © 2021 Stephen H. Gerstacker. All rights reserved.
#
require 'json'
require 'net/http'
require 'plist'
require 'uri'
# Helper for determining platform
module OS
# Is the platform Windows?
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
# Is the platform macOS?
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
# Is the platform UNIX-like?
def OS.unix?
!OS.windows?
end
# Is the platform Linux?
def OS.linux?
OS.unix? and not OS.mac?
end
end
# Helper for finding executables
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end
class Update
def self.can_run?
return false
end
def description
raise NotImplementedError
end
def icon
raise NotImplementedError
end
def execute(command)
result = system(command)
result == true
end
def run
raise NotImplementedError
end
def puts_bold(line)
puts "\e[1m#{line}\e[0m"
end
def puts_green(line)
puts "\e[32m#{line}\e[0m"
end
def puts_red(line)
puts "\e[31m#{line}\e[0m"
end
def puts_yellow(line)
puts "\e[33m#{line}\e[0m"
end
def puts_announcement
puts_bold "#{icon} #{description}"
end
def puts_failure(line)
puts_red "💥 #{line}"
end
def puts_success(line)
puts_green "👍 #{line}"
end
def puts_warning(line)
puts_yellow "⚠️ #{line}"
end
end
class AptUpdate < Update
def self.can_run?
return false unless OS.linux?
apt_command = which('apt') || which('apt-get')
return false if apt_command.nil?
true
end
def description
'Apt updates'
end
def icon
'🍄'
end
def run
apt_command = which('apt') || which('apt-get')
unless execute("sudo #{apt_command} update")
puts_failure 'Failed to update Apt'
return false
end
unless execute("sudo #{apt_command} upgrade")
puts_failure 'Failed to upgrade Apt'
return false
end
unless execute("sudo #{apt_command} autoremove")
puts_failure 'Failed to clean Apt'
return false
end
true
end
end
class HomebrewUpdate < Update
def self.can_run?
return false if which('brew').nil?
true
end
def description
'Homebrew updates'
end
def icon
'🍻'
end
def run
unless execute('brew update')
puts_failure 'Failed to update Homebrew'
return false
end
unless execute('brew upgrade')
puts_failure 'Failed to upgrade Homebrew'
return false
end
unless execute('brew upgrade --cask')
puts_failure 'Failed to upgrade Homebrew Casks'
return false
end
unless execute('brew cleanup')
puts_failure 'Failed to clean up Homebrew'
return false
end
true
end
end
class MacAppStoreUpdate < Update
def self.can_run?
return false unless OS.mac?
return false if which('mas').nil?
true
end
def description
'Mac App Store updates'
end
def icon
'📱'
end
def run
unless execute('mas outdated')
puts_failure 'Failed to update Mac App Store'
return false
end
unless execute('mas upgrade')
puts_failure 'Failed to upgrade Mac App Store'
return false
end
true
end
end
class MacOSUpdate < Update
def self.can_run?
return false unless OS.mac?
return false if which('softwareupdate').nil?
true
end
def description
'macOS updates'
end
def icon
'🍎'
end
def run
unless execute('sudo softwareupdate -i -a')
puts_failure 'Failed to update macOS'
return false
end
true
end
end
class RubyGemsUpdate < Update
def self.can_run?
return false if which('gem').nil?
true
end
def description
'Ruby Gem updates'
end
def icon
'💎'
end
def run
unless execute('gem update --system')
puts_failure 'Failed to update Ruby Gem system'
return false
end
unless execute('gem update')
puts_failure 'Failed to update Ruby Gems'
return false
end
true
end
end
class RubyUpdate < Update
def self.can_run?
true
end
def description
'Ruby updates'
end
def icon
'♦️ '
end
def run
uri = URI.parse 'https://www.ruby-lang.org/en/downloads/releases/'
response = Net::HTTP.get_response uri
if response.code != '200'
puts_failure "Failed to get Ruby versions: #{response.code}"
return false
end
in_releases = false
releases = []
current = {}
ruby_regex = /Ruby (\d+\.\d+\.\d+)-?([^<]+)?/.freeze
date_regex = />([^<]+)/.freeze
link_regex = /href="([^"]+)"/.freeze
response.body.each_line do |line|
if in_releases
if line.include?('</table>')
in_releases = false
break
elsif line.include?('<tr')
current = {}
elsif line.include?('</tr>')
next if current.empty?
releases << current
current = {}
elsif current[:version].nil?
current ||= {}
line.match(ruby_regex) do |m|
current[:version] = m[1]
current[:version_extra] = m[2]
end
elsif current[:date].nil?
line.match(date_regex) do |m|
current[:date] = Date.parse(m[1])
end
elsif current[:link].nil?
line.match(link_regex) do |m|
current[:link] = "https://www.ruby-lang.org" + m[1]
end
end
else
if line.include?('<table') && line.include?('release-list')
in_releases = true
end
end
end
full_releases = releases
.select { |x| x[:version_extra].nil? || (!x[:version_extra].include?('preview') && !x[:version_extra].include?('rc')) }
.sort_by { |x| x[:version] }
.reverse
latest_release = full_releases.first
if latest_release.nil?
puts_failure 'Failed to parse Ruby versions'
return false
end
if latest_release[:version] != RUBY_VERSION
puts_warning 'Ruby is not up to date!'
unless latest_release[:link].nil?
puts "🔗 News: #{latest_release[:link]}"
end
else
puts_success 'Ruby is up to date'
end
true
end
end
class RustUpdate < Update
def self.can_run?
return false if which('rustup').nil?
true
end
def description
'Rust updates'
end
def icon
'🦀'
end
def run
unless execute('rustup self update')
puts_failure 'Failed to update Rustup'
return false
end
unless execute('rustup update')
puts_failure 'Failed to update Rust'
return false
end
true
end
end
class SnapUpdate < Update
def self.can_run?
return false unless OS.linux?
return false if which('snap').nil?
true
end
def description
'Snap updates'
end
def icon
'🤌 '
end
def run
unless execute('sudo snap refresh')
puts_failure 'Failed to refresh Snap'
return false
end
true
end
end
class XcodeUpdate < Update
def self.can_run?
return unless OS.mac?
return unless Dir.exist?('/Applications/Xcode.app')
true
end
def description
'Xcode version'
end
def icon
'⚒️ '
end
def run
# Get the current installed version
xcode_path = '/Applications/Xcode.app/Contents/version.plist'
current_version_data = Plist.parse_xml xcode_path
current_version = current_version_data['ProductBuildVersion']
# Get the known released versions
uri = URI.parse 'https://xcodereleases.com/data.json'
response = Net::HTTP.get_response uri
if response.code != '200'
puts_failure 'Failed to get Xcode Releases API'
return false
end
releases = JSON.parse response.body
full_releases = releases.filter_map do |release|
version = release['version']
next if version.nil?
build = version['build']
next if build.nil?
number = version['number']
next if number.nil?
release_dict = version['release']
next if release_dict.nil?
release_value = release_dict['release']
next if release_value.nil?
links = release['links']
release_notes_url = nil
download_url = nil
unless links.nil?
unless links['notes'].nil?
unless links['notes']['url'].nil?
release_notes_url = links['notes']['url']
end
end
unless links['download'].nil?
unless links['download']['url'].nil?
download_url = links['download']['url']
end
end
end
if release_value
{ build: build, build_number: build.to_i(16), number: number, download: download_url, release_notes: release_notes_url }
end
end
latest_release = full_releases.first
if latest_release.nil?
puts_failure 'Cannot find the latest release'
return false
end
# Do the actual check
if current_version != latest_release[:build]
puts_warning 'Xcode is not up to date!'
unless latest_release[:download].nil?
puts "🔗 Download: #{latest_release[:download]}"
end
unless latest_release[:release_notes].nil?
puts "📃 Release Notes: #{latest_release[:release_notes]}"
end
else
puts_success 'Xcode is up to date'
end
true
end
end
class ZypperUpdate < Update
def self.can_run?
return false unless OS.linux?
zypper_command = which('zypper')
return false if zypper_command.nil?
true
end
def description
'Zypper updates'
end
def icon
'🦎️'
end
def run
zypper_command = which('zypper')
unless execute("sudo #{zypper_command} refresh")
puts_failure 'Failed to refresh zypper'
return false
end
unless execute("sudo #{zypper_command} up")
puts_failure 'Failed to update zypper'
return false
end
true
end
end
updates = [
'MacOS', 'Xcode', 'MacAppStore', 'Homebrew', 'Apt', 'Zypper', 'Snap', 'Ruby', 'RubyGems', 'Rust'
]
updates.each do |update|
clazz = Object.const_get("#{update}Update")
next unless clazz.can_run?
update = clazz.new
puts
update.puts_announcement
success = update.run
exit 1 unless success
end