mirror of
https://github.com/wavetermdev/homebrew-cask.git
synced 2026-08-05 13:43:24 -07:00
Merge pull request #4980 from rolandwalker/cleanup_refactor
detect cached downloads in doctor
This commit is contained in:
+63
-27
@@ -1,42 +1,78 @@
|
||||
class Cask::CLI::Cleanup
|
||||
|
||||
OUTDATED_DAYS = 10
|
||||
OUTDATED_TIMESTAMP = Time.now - (60 * 60 * 24 * OUTDATED_DAYS)
|
||||
|
||||
def self.run(*_ignored)
|
||||
remove_dead_symlinks
|
||||
remove_cached_downloads
|
||||
remove_all_cache_files
|
||||
end
|
||||
|
||||
def self.cache_symlinks
|
||||
HOMEBREW_CACHE_CASKS.children.select(&:symlink?)
|
||||
end
|
||||
|
||||
def self.dead_symlinks
|
||||
cache_symlinks.reject(&:exist?)
|
||||
end
|
||||
|
||||
def self.cache_incompletes(outdated=nil)
|
||||
cache_symlinks.collect do |symlink|
|
||||
incomplete_file = Dir.chdir HOMEBREW_CACHE_CASKS do
|
||||
f = symlink.readlink
|
||||
f = f.realpath if f.exist?
|
||||
Pathname.new(f.to_s.concat('.incomplete'))
|
||||
end
|
||||
incomplete_file = nil unless incomplete_file.exist?
|
||||
incomplete_file = nil if outdated and incomplete_file and incomplete_file.stat.mtime > OUTDATED_TIMESTAMP
|
||||
incomplete_file
|
||||
end.compact
|
||||
end
|
||||
|
||||
def self.cache_completes(outdated=nil)
|
||||
cache_symlinks.collect do |symlink|
|
||||
file = Dir.chdir HOMEBREW_CACHE_CASKS do
|
||||
f = symlink.readlink
|
||||
f.exist? ? f.realpath : f
|
||||
end
|
||||
file = nil unless file.exist?
|
||||
if outdated and file and file.stat.mtime > OUTDATED_TIMESTAMP
|
||||
file = nil
|
||||
symlink = nil
|
||||
end
|
||||
[ symlink, file ]
|
||||
end.flatten.compact.sort { |x,y| x.to_s.count(File::SEPARATOR) <=> y.to_s.count(File::SEPARATOR) }
|
||||
end
|
||||
|
||||
# will include dead symlinks if they aren't handled separately
|
||||
def self.all_cache_files(outdated=nil)
|
||||
cache_incompletes(outdated) + cache_completes(outdated)
|
||||
end
|
||||
|
||||
def self.space_in_megs(files)
|
||||
bytes = files.map { |f| begin File.size(f); rescue; 0; end }.reduce(&:+) || 0
|
||||
sprintf '%0.2f', bytes / (1024.0 * 1024.0)
|
||||
end
|
||||
|
||||
def self.remove_dead_symlinks
|
||||
ohai "Removing dead symlinks"
|
||||
HOMEBREW_CACHE_CASKS.children.select(&:symlink?).each do |symlink|
|
||||
unless symlink.exist?
|
||||
puts symlink
|
||||
symlink.unlink
|
||||
end
|
||||
to_delete = dead_symlinks
|
||||
puts "Nothing to do" unless to_delete.count > 0
|
||||
to_delete.each do |item|
|
||||
puts item
|
||||
item.unlink
|
||||
end
|
||||
end
|
||||
|
||||
def self.remove_cached_downloads
|
||||
def self.remove_all_cache_files
|
||||
message = "Removing cached downloads"
|
||||
days = 10
|
||||
outdated_timestamp = Time.now - (60 * 60 * 24 * days)
|
||||
if Cask.outdated
|
||||
message.concat(" older than #{days} days old")
|
||||
end
|
||||
message.concat " older than #{OUTDATED_DAYS} days old" if Cask.outdated
|
||||
ohai message
|
||||
HOMEBREW_CACHE_CASKS.children.select(&:symlink?).each do |symlink|
|
||||
file = Dir.chdir HOMEBREW_CACHE_CASKS do
|
||||
symlink.readlink.realpath
|
||||
end
|
||||
if !Cask.outdated or file.stat.mtime < outdated_timestamp
|
||||
puts file
|
||||
file.unlink
|
||||
symlink.unlink
|
||||
end
|
||||
incomplete_file = Pathname.new(file.to_s.concat('.incomplete'))
|
||||
if incomplete_file.exist? and
|
||||
(!Cask.outdated or incomplete_file.stat.mtime < outdated_timestamp)
|
||||
puts incomplete_file
|
||||
incomplete_file.unlink
|
||||
end
|
||||
to_delete = all_cache_files(Cask.outdated)
|
||||
puts "Nothing to do" unless to_delete.count > 0
|
||||
to_delete.each do |item|
|
||||
puts item
|
||||
item.unlink
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class Cask::CLI::Doctor
|
||||
ohai 'Homebrew Origin:', render_with_none_as_error( homebrew_origin )
|
||||
ohai 'Homebrew-cask Version:', render_with_none_as_error( HOMEBREW_CASK_VERSION )
|
||||
ohai 'Homebrew-cask Install Location:', render_install_location( HOMEBREW_CASK_VERSION )
|
||||
ohai 'Homebrew-cask Cached Downloads:', render_cached_downloads
|
||||
ohai 'Homebrew-cask Default Tap Path:', render_tap_paths( fq_default_tap )
|
||||
ohai 'Homebrew-cask Alternate Cask Taps:', render_tap_paths( alt_taps )
|
||||
ohai 'Homebrew-cask Default Tap Cask Count:', render_with_none_as_error( default_cask_count )
|
||||
@@ -160,6 +161,18 @@ class Cask::CLI::Doctor
|
||||
copy
|
||||
end
|
||||
|
||||
def self.render_cached_downloads
|
||||
files = Cask::CLI::Cleanup.all_cache_files
|
||||
count = files.count
|
||||
space = Cask::CLI::Cleanup.space_in_megs files
|
||||
[
|
||||
HOMEBREW_CACHE,
|
||||
HOMEBREW_CACHE_CASKS,
|
||||
count.to_s.concat(" files").concat(count == 0 ? '' : %Q{ #{error_string %Q{warning: run "brew cask cleanup"}}}),
|
||||
space.to_s.concat(" megs").concat(count == 0 ? '' : %Q{ #{error_string %Q{warning: run "brew cask cleanup"}}}),
|
||||
]
|
||||
end
|
||||
|
||||
def self.help
|
||||
"checks for configuration issues"
|
||||
end
|
||||
|
||||
@@ -8,7 +8,9 @@ describe Cask::CLI::Cleanup do
|
||||
end
|
||||
out.must_equal <<-OUTPUT.undent
|
||||
==> Removing dead symlinks
|
||||
Nothing to do
|
||||
==> Removing cached downloads older than 10 days old
|
||||
Nothing to do
|
||||
OUTPUT
|
||||
end
|
||||
|
||||
@@ -19,6 +21,6 @@ describe Cask::CLI::Cleanup do
|
||||
out, err = capture_io do
|
||||
Cask::CLI::Cleanup.run
|
||||
end
|
||||
out.must_match(/^==> Removing dead symlinks\n==> Removing cached downloads\n\//)
|
||||
out.must_match(/^==> Removing dead symlinks\nNothing to do\n==> Removing cached downloads\n/)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user