mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Port Homebrew::Cmd::Cleanup
This commit is contained in:
parent
a8f8c65d93
commit
adf47bb11b
@ -1,75 +1,72 @@
|
||||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "cleanup"
|
||||
require "cli/parser"
|
||||
|
||||
module Homebrew
|
||||
module_function
|
||||
module Cmd
|
||||
class CleanupCmd < AbstractCommand
|
||||
cmd_args do
|
||||
days = Homebrew::EnvConfig::ENVS[:HOMEBREW_CLEANUP_MAX_AGE_DAYS][:default]
|
||||
description <<~EOS
|
||||
Remove stale lock files and outdated downloads for all formulae and casks,
|
||||
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`.
|
||||
EOS
|
||||
flag "--prune=",
|
||||
description: "Remove all cache files older than specified <days>. " \
|
||||
"If you want to remove everything, use `--prune=all`."
|
||||
switch "-n", "--dry-run",
|
||||
description: "Show what would be removed, but do not actually remove anything."
|
||||
switch "-s",
|
||||
description: "Scrub the cache, including downloads for even the latest versions. " \
|
||||
"Note that downloads for any installed formulae or casks will still not be deleted. " \
|
||||
"If you want to delete those too: `rm -rf \"$(brew --cache)\"`"
|
||||
switch "--prune-prefix",
|
||||
description: "Only prune the symlinks and directories from the prefix and remove no other files."
|
||||
|
||||
sig { returns(CLI::Parser) }
|
||||
def cleanup_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
days = Homebrew::EnvConfig::ENVS[:HOMEBREW_CLEANUP_MAX_AGE_DAYS][:default]
|
||||
description <<~EOS
|
||||
Remove stale lock files and outdated downloads for all formulae and casks,
|
||||
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`.
|
||||
EOS
|
||||
flag "--prune=",
|
||||
description: "Remove all cache files older than specified <days>. " \
|
||||
"If you want to remove everything, use `--prune=all`."
|
||||
switch "-n", "--dry-run",
|
||||
description: "Show what would be removed, but do not actually remove anything."
|
||||
switch "-s",
|
||||
description: "Scrub the cache, including downloads for even the latest versions. " \
|
||||
"Note that downloads for any installed formulae or casks will still not be deleted. " \
|
||||
"If you want to delete those too: `rm -rf \"$(brew --cache)\"`"
|
||||
switch "--prune-prefix",
|
||||
description: "Only prune the symlinks and directories from the prefix and remove no other files."
|
||||
named_args [:formula, :cask]
|
||||
end
|
||||
|
||||
named_args [:formula, :cask]
|
||||
end
|
||||
end
|
||||
sig { override.void }
|
||||
def run
|
||||
days = args.prune.presence&.then do |prune|
|
||||
case prune
|
||||
when /\A\d+\Z/
|
||||
prune.to_i
|
||||
when "all"
|
||||
0
|
||||
else
|
||||
raise UsageError, "`--prune` expects an integer or `all`."
|
||||
end
|
||||
end
|
||||
|
||||
sig { void }
|
||||
def cleanup
|
||||
args = cleanup_args.parse
|
||||
cleanup = Cleanup.new(*args.named, dry_run: args.dry_run?, scrub: args.s?, days:)
|
||||
if args.prune_prefix?
|
||||
cleanup.prune_prefix_symlinks_and_directories
|
||||
return
|
||||
end
|
||||
|
||||
days = args.prune.presence&.then do |prune|
|
||||
case prune
|
||||
when /\A\d+\Z/
|
||||
prune.to_i
|
||||
when "all"
|
||||
0
|
||||
else
|
||||
raise UsageError, "`--prune` expects an integer or `all`."
|
||||
cleanup.clean!(quiet: args.quiet?, periodic: false)
|
||||
|
||||
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
|
||||
|
||||
return if cleanup.unremovable_kegs.empty?
|
||||
|
||||
ofail <<~EOS
|
||||
Could not cleanup old kegs! Fix your permissions on:
|
||||
#{cleanup.unremovable_kegs.join "\n "}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
cleanup = Cleanup.new(*args.named, dry_run: args.dry_run?, scrub: args.s?, days:)
|
||||
if args.prune_prefix?
|
||||
cleanup.prune_prefix_symlinks_and_directories
|
||||
return
|
||||
end
|
||||
|
||||
cleanup.clean!(quiet: args.quiet?, periodic: false)
|
||||
|
||||
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
|
||||
|
||||
return if cleanup.unremovable_kegs.empty?
|
||||
|
||||
ofail <<~EOS
|
||||
Could not cleanup old kegs! Fix your permissions on:
|
||||
#{cleanup.unremovable_kegs.join "\n "}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
@ -1,8 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "cmd/cleanup"
|
||||
require "cmd/shared_examples/args_parse"
|
||||
|
||||
RSpec.describe "brew cleanup" do
|
||||
RSpec.describe Homebrew::Cmd::CleanupCmd do
|
||||
before do
|
||||
FileUtils.mkdir_p HOMEBREW_LIBRARY/"Homebrew/vendor/"
|
||||
FileUtils.touch HOMEBREW_LIBRARY/"Homebrew/vendor/portable-ruby-version"
|
||||
|
Loading…
x
Reference in New Issue
Block a user