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 |
Author
a0s
commented
May 17, 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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment