| 
									
										
										
										
											2023-08-08 13:54:59 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2015-12-29 12:57:48 +01:00
										 |  |  | require "cleanup" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class CleanupCmd < AbstractCommand | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							| 
									
										
										
										
											2024-07-23 11:26:12 +01:00
										 |  |  |         days = Homebrew::EnvConfig::ENVS[:HOMEBREW_CLEANUP_MAX_AGE_DAYS]&.dig(:default) | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2025-01-27 14:21:27 +00:00
										 |  |  |           #{days} days old. This can be adjusted with `$HOMEBREW_CLEANUP_MAX_AGE_DAYS`. | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         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." | 
					
						
							| 
									
										
										
										
											2024-06-03 10:49:23 +02:00
										 |  |  |         switch "-s", "--scrub", | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |                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." | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         named_args [:formula, :cask] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2018-12-12 02:02:19 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2015-11-17 16:51:56 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         cleanup.clean!(quiet: args.quiet?, periodic: false) | 
					
						
							| 
									
										
										
										
											2020-08-12 08:22:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         return if cleanup.unremovable_kegs.empty? | 
					
						
							| 
									
										
										
										
											2016-09-22 20:12:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 16:23:10 -07:00
										 |  |  |         ofail <<~EOS | 
					
						
							|  |  |  |           Could not cleanup old kegs! Fix your permissions on: | 
					
						
							|  |  |  |             #{cleanup.unremovable_kegs.join "\n  "} | 
					
						
							|  |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2018-08-08 11:20:53 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-11-17 16:51:56 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-03-21 04:13:13 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-08-10 16:33:47 -04:00
										 |  |  | end |