Skip to content

Instantly share code, notes, and snippets.

@a0s a0s/brew-cask-cleanup.rb
Last active Nov 5, 2019

Embed
What would you like to do?
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
@a0s

This comment has been minimized.

Copy link
Owner Author

a0s commented May 17, 2015

ruby brew-cask-cleanup.rb
@kaymmm

This comment has been minimized.

Copy link

kaymmm commented May 23, 2015

Thanks! It worked for most of my casks, but not sublime text 3 because it uses the version format "Build 3XXX". Version needs to split on " " (space) and discard the non-numeric portion, but I'm not sure if this would break compatibility with other casks since version numbering is hardly consistent across apps.

@leipert

This comment has been minimized.

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
You can’t perform that action at this time.