Skip to content

Instantly share code, notes, and snippets.

@a0s
Last active November 5, 2019 14:14
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@a0s
Copy link
Author

a0s commented May 17, 2015

ruby brew-cask-cleanup.rb

@kaymmm
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
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