cleanup.rb 2.32 KB
Newer Older
Markus Reiter's avatar
Markus Reiter committed
1
# typed: true
2
3
# frozen_string_literal: true

4
require "cleanup"
5
require "cli/parser"
6

7
module Homebrew
Markus Reiter's avatar
Markus Reiter committed
8
9
  extend T::Sig

10
11
  module_function

Markus Reiter's avatar
Markus Reiter committed
12
  sig { returns(CLI::Parser) }
13
14
  def cleanup_args
    Homebrew::CLI::Parser.new do
15
      days = Homebrew::EnvConfig::ENVS[:HOMEBREW_CLEANUP_MAX_AGE_DAYS][:default]
16
      usage_banner <<~EOS
17
        `cleanup` [<options>] [<formula>|<cask>]
18

19
        Remove stale lock files and outdated downloads for all formulae and casks,
20
        and remove old versions of installed formulae. If arguments are specified,
21
22
        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`.
23
24
      EOS
      flag   "--prune=",
Mike McQuaid's avatar
Mike McQuaid committed
25
             description: "Remove all cache files older than specified <days>."
26
      switch "-n", "--dry-run",
Mike McQuaid's avatar
Mike McQuaid committed
27
             description: "Show what would be removed, but do not actually remove anything."
28
      switch "-s",
Mike McQuaid's avatar
Mike McQuaid committed
29
             description: "Scrub the cache, including downloads for even the latest versions. "\
30
                          "Note downloads for any installed formulae or casks will still not be deleted. "\
Mike McQuaid's avatar
Mike McQuaid committed
31
                          "If you want to delete those too: `rm -rf \"$(brew --cache)\"`"
32
      switch "--prune-prefix",
Mike McQuaid's avatar
Mike McQuaid committed
33
             description: "Only prune the symlinks and directories from the prefix and remove no other files."
34
    end
35
36
37
  end

  def cleanup
Markus Reiter's avatar
Markus Reiter committed
38
    args = cleanup_args.parse
39

40
41
42
43
    if args.prune.present? && !Integer(args.prune, exception: false) && args.prune != "all"
      raise UsageError, "--prune= expects an integer or 'all'."
    end

44
    cleanup = Cleanup.new(*args.named, dry_run: args.dry_run?, scrub: args.s?, days: args.prune&.to_i)
45
46
47
48
    if args.prune_prefix?
      cleanup.prune_prefix_symlinks_and_directories
      return
    end
49
50

    cleanup.clean!
Markus Reiter's avatar
Markus Reiter committed
51

52
53
54
55
56
57
58
    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
59
    end
60

61
62
    return if cleanup.unremovable_kegs.empty?

Markus Reiter's avatar
Markus Reiter committed
63
    ofail <<~EOS
64
      Could not cleanup old kegs! Fix your permissions on:
65
        #{cleanup.unremovable_kegs.join "\n  "}
66
67
    EOS
  end
68
end