Skip to content

Instantly share code, notes, and snippets.

@a0s
Last active November 5, 2019 14:14
Show Gist options
  • Save a0s/26a8752ae54a169b8516 to your computer and use it in GitHub Desktop.
Save a0s/26a8752ae54a169b8516 to your computer and use it in GitHub Desktop.
Clean (move to trash) old brew cask versions
#!ruby
# Requirements:
# brew install trash
casks_path = '/opt/homebrew-cask/Caskroom'
class Version < Array
def initialize s
super(s.split('.').map { |e| e.to_i })
end
def < x
(self <=> x) < 0
end
def > x
(self <=> x) > 0
end
def == x
(self <=> x) == 0
end
end
if `brew list | grep trash`.empty?
puts 'Please make "brew install trash" before'
exit!
end
Dir[File.join(casks_path, '*')].each do |cask_path|
versions = Dir[File.join(cask_path,'*')].map{|i| File.basename(i)}
next if versions.include?('latest')
next if versions.size == 1
versions.sort! do |b, a|
a, b = Version.new(a), Version.new(b)
if a == b
0
elsif a < b
-1
else
1
end
end
versions.shift
versions.each do |version|
path = File.join(cask_path, version)
puts `trash #{path}`
end
end
@leipert
Copy link

leipert commented Jul 19, 2015

If you are a user of maid, you could use this gist which was greatly inspired by this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment