2016-03-11 19:25:16 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
# A workaround to upgrade installed casks
|
|
|
|
# Update Homebrew Cask and verify installed applications versions with Casks to perform an upgrade
|
|
|
|
# The app prompts you to update only if the Cask contain a version number and the installed app version can be retrieved.
|
|
|
|
# Therefore casks that use "latest" will not be updated
|
|
|
|
#
|
|
|
|
# NOTE: THIS SCRIPT COMPLETELY REMOVES OUT-OF-DATE CASKS & ADDS THE LATEST VERSION.
|
|
|
|
|
2016-03-16 16:37:11 +00:00
|
|
|
require 'optparse'
|
2016-03-11 19:25:16 +00:00
|
|
|
|
2016-03-16 16:37:11 +00:00
|
|
|
options = {}
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: #{opts.program_name()} <options> <package name>"
|
2016-03-11 19:25:16 +00:00
|
|
|
|
2016-03-16 16:37:11 +00:00
|
|
|
opts.on("-y", "Assume yes") do |y|
|
|
|
|
options[:auto] = y
|
|
|
|
end
|
|
|
|
end.parse!
|
2016-03-11 19:25:16 +00:00
|
|
|
|
2016-03-16 16:37:11 +00:00
|
|
|
options[:package] = ARGV[0]
|
2016-03-11 19:25:16 +00:00
|
|
|
|
|
|
|
# UPGRADE SPECIFIC APP
|
2016-03-16 16:37:11 +00:00
|
|
|
if options[:package]
|
|
|
|
app = options[:package]
|
2016-03-11 19:25:16 +00:00
|
|
|
if system("brew cask info #{app} &>/dev/null")
|
2016-03-16 16:37:11 +00:00
|
|
|
print "Do you want to reinstall #{app}? " + '[y/N]'
|
|
|
|
if options[:auto]
|
|
|
|
update = 'y'
|
|
|
|
else
|
|
|
|
update = STDIN.gets.strip
|
|
|
|
end
|
2016-03-11 19:25:16 +00:00
|
|
|
if update =~ /[yY]/
|
2016-03-16 17:42:50 +00:00
|
|
|
puts "Upgrading #{app}"
|
|
|
|
system("brew cask uninstall #{app} --force && brew cask install #{app} --force")
|
2016-03-11 19:25:16 +00:00
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Cancelling update...'
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Unknown app to update'
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
exit!
|
|
|
|
end
|
|
|
|
|
|
|
|
# UPDATE HOMEBREW
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Updating homebrew cask, please wait...'
|
2016-03-11 19:25:16 +00:00
|
|
|
system('brew cask update')
|
|
|
|
if $CHILD_STATUS == 1
|
2016-03-16 17:42:50 +00:00
|
|
|
puts 'Cask update failed'
|
2016-03-11 19:25:16 +00:00
|
|
|
exit!
|
|
|
|
end
|
|
|
|
|
|
|
|
# BREW CASK LIST
|
|
|
|
IO.popen('brew cask list') do |apps|
|
|
|
|
apps.each do |app|
|
|
|
|
app.chomp!
|
2016-03-16 16:37:11 +00:00
|
|
|
infosstr = app
|
2016-03-11 19:25:16 +00:00
|
|
|
infos = `brew cask info #{app}`
|
2016-03-16 16:37:11 +00:00
|
|
|
if vmatch = /: ([A-z0-9\._]+)/.match(infos.lines.first)
|
2016-03-11 19:25:16 +00:00
|
|
|
version, build = vmatch[1].split(/[_-]/)
|
2016-03-16 16:37:11 +00:00
|
|
|
infosstr += " version: #{version}"
|
|
|
|
infosstr += " (#{build})" if build
|
2016-03-11 19:25:16 +00:00
|
|
|
|
|
|
|
print infosstr.to_s
|
2016-03-16 16:37:11 +00:00
|
|
|
column1length = 80 - infosstr.gsub(/\e\[[0-9;]+[m]?/, '').length
|
2016-03-11 19:25:16 +00:00
|
|
|
|
2016-03-16 16:37:11 +00:00
|
|
|
if appname = /(.+) \(app\)/.match(infos)
|
2016-03-11 19:25:16 +00:00
|
|
|
# APPLICATION FILENAME
|
|
|
|
appname = appname[1].strip
|
2016-03-16 16:37:11 +00:00
|
|
|
|
|
|
|
fullpath = `mdfind -onlyin /opt/homebrew-cask/Caskroom -onlyin /Applications -onlyin ~/Applications "#{appname}" | grep "#{appname}"`.strip
|
|
|
|
if File.exist?(fullpath)
|
2016-03-11 19:25:16 +00:00
|
|
|
# GET APPLICATION VERSION WITH SPOTLIGHT
|
2016-03-16 16:37:11 +00:00
|
|
|
if currentmatch = /([A-z0-9\.]+)/.match(`mdls "#{fullpath}" -name kMDItemVersion -raw`)
|
2016-03-11 19:25:16 +00:00
|
|
|
current = currentmatch[1].chomp
|
2016-03-16 16:37:11 +00:00
|
|
|
|
2016-03-11 19:25:16 +00:00
|
|
|
# COMPARE INSTALLED VERSION WITH CASK VERSION
|
2016-03-16 16:37:11 +00:00
|
|
|
begin
|
|
|
|
if Gem::Version.new(version) <= Gem::Version.new(current)
|
|
|
|
puts 'Up-to-date'.rjust(column1length)
|
|
|
|
elsif current != '(null)' && version != 'latest'
|
|
|
|
puts 'Update available'.rjust(column1length)
|
2016-03-16 17:42:50 +00:00
|
|
|
print "Do you want to update #{app} to version #{version} (current #{current})? " + '[y/N] '
|
2016-03-16 16:37:11 +00:00
|
|
|
if options[:auto]
|
|
|
|
update = 'y'
|
|
|
|
else
|
|
|
|
update = STDIN.gets.strip
|
|
|
|
end
|
|
|
|
# UPDATE CURRENT APPLICAION
|
|
|
|
if update =~ /[yY]/
|
|
|
|
puts "Updating #{app} to version #{version}"
|
2016-03-16 17:42:50 +00:00
|
|
|
system("brew cask uninstall #{app} --force && brew cask install #{app} --force")
|
2016-03-16 16:37:11 +00:00
|
|
|
else
|
|
|
|
puts 'Cancelling update'
|
|
|
|
end
|
2016-03-11 19:25:16 +00:00
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Update undefined'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
2016-03-16 16:37:11 +00:00
|
|
|
rescue
|
|
|
|
puts 'Cannot determine latest version'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Unknown installed version'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Not found'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
else
|
2016-03-16 16:37:11 +00:00
|
|
|
puts 'Unknown app'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
print infosstr.to_s
|
2016-03-16 16:37:11 +00:00
|
|
|
column1length = 80 - infosstr.length
|
|
|
|
puts 'Unknown version'.rjust(column1length)
|
2016-03-11 19:25:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|