Skip to content

Instantly share code, notes, and snippets.

@epsimatic
Forked from mkc188/brew_cask_upgrade.rb
Last active December 10, 2015 21:31
Show Gist options
  • Save epsimatic/d3854b4079fa2056973f to your computer and use it in GitHub Desktop.
Save epsimatic/d3854b4079fa2056973f to your computer and use it in GitHub Desktop.
# Location of casks
CASKROOM = '/opt/homebrew-cask/Caskroom/'
TAPS = '/usr/local/Library/Taps/caskroom/'
MIN_AGE = 30 # days
require 'date'
require 'simple_progressbar'
i=1
SimpleProgressbar.new.show('Updating') do
Dir.foreach(CASKROOM) do |cask_name|
i = i+1
progress 100 * i / Dir.entries(CASKROOM).length
# Ignore hidden files/directories ('..' and '.' too)
next if cask_name.chr == '.'
# Get the info for the cask and find the newest version.
cask_location = File.join(CASKROOM, cask_name)
# Get the list of installed versions from cask directory
cask_dir_contents = %x(ls #{cask_location}).split.map(&:strip)
installed_versions = cask_dir_contents
installed_versions.reject! { |file_or_dir| file_or_dir.chr == '.' }
cask_formula_location = nil
Dir.foreach(TAPS) do |cask_repo|
next if cask_repo.chr == '.'
cask_formula_location_maybe = File.join(TAPS, cask_repo, 'Casks', cask_name + '.rb')
cask_formula_location = cask_formula_location_maybe if File.exists?(cask_formula_location_maybe)
end
print "\r", cask_name, " is DELETED from repository! \n" unless cask_formula_location
next unless cask_formula_location
uses_pkg = system('grep', '^[[:space:]]*pkg[[:space:]]', cask_formula_location, :out => '/dev/null')
# uses_app = system('grep', '^[[:space:]]*app[[:space:]]', cask_formula_location, :out => '/dev/null')
uses_font= system('grep', '^[[:space:]]*font[[:space:]]', cask_formula_location, :out => '/dev/null')
# uses_ql = system('grep', '^[[:space:]]*qlplugin[[:space:]]', cask_formula_location, :out => '/dev/null')
if installed_versions.include?('latest')
age = (Date.today - File.mtime(cask_location).to_date).round
next if age < MIN_AGE
# Fonts are updated way less often, so they use 4×MIN_AGE
next if uses_font and age < MIN_AGE * 4
print "\r", cask_name, ' — ', age, 'd ago'
else
# Find the newest version.
newest_version = %x(brew cask info #{cask_name}).lines.first.split.map(&:strip).last
#BAD:
# newest_version = open(cask_formula_location).each_line.find { |line| line.split.first == 'version' }.split.last.gsub(/[\'\"\`]/, '')
next if installed_versions.include?(newest_version)
print "\r", cask_name, ' ', installed_versions.join(", "), ' -> ', newest_version
# print "\n" ; next
end
if installed_versions.include?('latest')
if uses_pkg
print ", uses .pkg"
# next
end
# unless uses_app || uses_font || uses_ql
# print ', is tricky: ', cask_formula_location, "\n"
# next
# end
end
print ' … ', 'uninstalling ', installed_versions.join(", "), ":\n"
unless system('brew cask uninstall --force ' + cask_name)
$stderr.puts "Oops, I've tried to remove #{cask_name} #{installed_versions} and cask gone haywire. I'm sorry!"
$stderr.puts " Offending cask: #{cask_formula_location}"
$stderr.puts " Failed install command: brew cask uninstall --force #{cask_name}"
exit 1
end
print 'installing ', newest_version, ":\n"
unless system('brew cask install ' + cask_name)
$stderr.puts "Oops, I've removed #{cask_name} #{installed_versions} and couldn't reinstall it. I'm sorry!"
$stderr.puts " Offending cask: #{cask_formula_location}"
$stderr.puts " Failed install command: brew cask install #{cask_name}"
exit 1
end
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment