Add cask cleanup and per-formula cache cleanup.

This commit is contained in:
Markus Reiter 2018-08-08 22:23:55 +02:00
parent 232de9d9da
commit 78658e4302
2 changed files with 43 additions and 14 deletions

View File

@ -1,3 +1,4 @@
require "hbc/cask"
require "uri" require "uri"
module Hbc module Hbc

View File

@ -3,6 +3,8 @@ require "formula"
require "hbc/cask_loader" require "hbc/cask_loader"
module CleanupRefinement module CleanupRefinement
LATEST_CASK_DAYS = 7
refine Pathname do refine Pathname do
def incomplete? def incomplete?
extname.end_with?(".incomplete") extname.end_with?(".incomplete")
@ -23,7 +25,7 @@ module CleanupRefinement
def stale?(scrub = false) def stale?(scrub = false)
return false unless file? return false unless file?
stale_formula?(scrub) stale_formula?(scrub) || stale_cask?(scrub)
end end
private private
@ -35,13 +37,16 @@ module CleanupRefinement
begin begin
Utils::Bottles.resolve_version(self) Utils::Bottles.resolve_version(self)
rescue rescue
self.version nil
end end
else
self.version
end end
version ||= basename.to_s[/\A.*--(.*?)#{Regexp.escape(extname)}/, 1]
return false unless version return false unless version
version = Version.parse(version)
return false unless (name = basename.to_s[/\A(.*?)\-\-?(?:#{Regexp.escape(version)})/, 1]) return false unless (name = basename.to_s[/\A(.*?)\-\-?(?:#{Regexp.escape(version)})/, 1])
formula = begin formula = begin
@ -62,6 +67,29 @@ module CleanupRefinement
false false
end end
def stale_cask?(scrub)
return false unless name = basename.to_s[/\A(.*?)\-\-/, 1]
cask = begin
Hbc::CaskLoader.load(name)
rescue Hbc::CaskUnavailableError
return false
end
unless basename.to_s.match?(/\A#{Regexp.escape(name)}\-\-#{Regexp.escape(cask.version)}\b/)
return true
end
return true if scrub && !cask.versions.include?(cask.version)
if cask.version.latest?
# TODO: Replace with ActiveSupport's `.days.ago`.
return mtime < ((@time ||= Time.now) - LATEST_CASK_DAYS * 60 * 60 * 24)
end
false
end
end end
end end
@ -114,19 +142,18 @@ module Homebrew
end end
end end
def update_disk_cleanup_size(path_size)
@disk_cleanup_size += path_size
end
def unremovable_kegs def unremovable_kegs
@unremovable_kegs ||= [] @unremovable_kegs ||= []
end end
def cleanup_formula(formula) def cleanup_formula(formula)
formula.eligible_kegs_for_cleanup.each(&method(:cleanup_keg)) formula.eligible_kegs_for_cleanup.each(&method(:cleanup_keg))
cleanup_cache(Pathname.glob(cache/"#{formula.name}--*"))
end end
def cleanup_cask(cask); end def cleanup_cask(cask)
cleanup_cache(Pathname.glob(cache/"Cask/#{cask.token}--*"))
end
def cleanup_keg(keg) def cleanup_keg(keg)
cleanup_path(keg) { keg.uninstall } cleanup_path(keg) { keg.uninstall }
@ -144,9 +171,10 @@ module Homebrew
end end
end end
def cleanup_cache def cleanup_cache(entries = nil)
return unless cache.directory? entries ||= [cache, cache/"Cask"].select(&:directory?).flat_map(&:children)
cache.children.each do |path|
entries.each do |path|
next cleanup_path(path) { path.unlink } if path.incomplete? next cleanup_path(path) { path.unlink } if path.incomplete?
next cleanup_path(path) { FileUtils.rm_rf path } if path.nested_cache? next cleanup_path(path) { FileUtils.rm_rf path } if path.nested_cache?
@ -159,7 +187,7 @@ module Homebrew
next next
end end
next cleanup_path(path) { path.unlink } if path.stale?(ARGV.switch?("s")) next cleanup_path(path) { path.unlink } if path.stale?(scrub?)
end end
end end
@ -173,7 +201,7 @@ module Homebrew
yield yield
end end
update_disk_cleanup_size(disk_usage) @disk_cleanup_size += disk_usage
end end
def cleanup_lockfiles def cleanup_lockfiles