Last active
November 5, 2019 14:14
-
-
Save a0s/26a8752ae54a169b8516 to your computer and use it in GitHub Desktop.
Clean (move to trash) old brew cask versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are a user of maid, you could use this gist which was greatly inspired by this one.