brew/Library/Homebrew/cmd/cleanup.rb

66 lines
2.3 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "cleanup"
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2018-12-12 02:02:19 +05:30
def cleanup_args
Homebrew::CLI::Parser.new do
days = Homebrew::EnvConfig::ENVS[:HOMEBREW_CLEANUP_MAX_AGE_DAYS][:default]
2018-12-12 02:02:19 +05:30
usage_banner <<~EOS
`cleanup` [<options>] [<formula>|<cask>]
2018-12-12 02:02:19 +05:30
Remove stale lock files and outdated downloads for all formulae and casks,
2018-12-12 02:02:19 +05:30
and remove old versions of installed formulae. If arguments are specified,
only do this for the given formulae and casks. Removes all downloads more than
#{days} days old. This can be adjusted with `HOMEBREW_CLEANUP_MAX_AGE_DAYS`.
2018-12-12 02:02:19 +05:30
EOS
flag "--prune=",
2019-04-30 08:44:35 +01:00
description: "Remove all cache files older than specified <days>."
2018-12-12 02:02:19 +05:30
switch "-n", "--dry-run",
2019-04-30 08:44:35 +01:00
description: "Show what would be removed, but do not actually remove anything."
2018-12-12 02:02:19 +05:30
switch "-s",
2019-04-30 08:44:35 +01:00
description: "Scrub the cache, including downloads for even the latest versions. "\
2019-12-12 19:04:01 -05:00
"Note downloads for any installed formulae or casks will still not be deleted. "\
2019-04-30 08:44:35 +01:00
"If you want to delete those too: `rm -rf \"$(brew --cache)\"`"
switch "--prune-prefix",
2019-04-30 08:44:35 +01:00
description: "Only prune the symlinks and directories from the prefix and remove no other files."
end
2018-12-12 02:02:19 +05:30
end
def cleanup
2020-07-30 18:40:10 +02:00
args = cleanup_args.parse
2020-08-12 08:22:45 -05:00
if args.prune.present? && !Integer(args.prune, exception: false) && args.prune != "all"
raise UsageError, "--prune= expects an integer or 'all'."
end
cleanup = Cleanup.new(*args.named, dry_run: args.dry_run?, scrub: args.s?, days: args.prune&.to_i)
if args.prune_prefix?
cleanup.prune_prefix_symlinks_and_directories
return
end
2018-08-08 11:20:53 +02:00
cleanup.clean!
2016-09-22 20:12:28 +02:00
2018-08-08 11:20:53 +02:00
unless cleanup.disk_cleanup_size.zero?
disk_space = disk_usage_readable(cleanup.disk_cleanup_size)
if args.dry_run?
ohai "This operation would free approximately #{disk_space} of disk space."
else
ohai "This operation has freed approximately #{disk_space} of disk space."
end
end
2018-08-08 11:20:53 +02:00
return if cleanup.unremovable_kegs.empty?
2017-10-15 02:28:32 +02:00
ofail <<~EOS
Could not cleanup old kegs! Fix your permissions on:
2018-08-08 11:20:53 +02:00
#{cleanup.unremovable_kegs.join "\n "}
EOS
end
end