| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 19:52:21 +02:00
										 |  |  | require "cask/macos" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2020-08-24 23:43:40 +02:00
										 |  |  |   # Helper class for uninstalling `.pkg` installers. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api private | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |   class Pkg | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { params(regexp: String, command: T.class_of(SystemCommand)).returns(T::Array[Pkg]) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def self.all_matching(regexp, command) | 
					
						
							| 
									
										
										
										
											2016-10-23 14:44:14 +02:00
										 |  |  |       command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map do |package_id| | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         new(package_id.chomp, command) | 
					
						
							| 
									
										
										
										
											2016-10-23 14:44:14 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     attr_reader :package_id | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { params(package_id: String, command: T.class_of(SystemCommand)).void } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def initialize(package_id, command = SystemCommand) | 
					
						
							|  |  |  |       @package_id = package_id | 
					
						
							|  |  |  |       @command = command | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { void } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def uninstall | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  |       unless pkgutil_bom_files.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg files" | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |         @command.run!( | 
					
						
							|  |  |  |           "/usr/bin/xargs", | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:07 -07:00
										 |  |  |           args:         ["-0", "--", "/bin/rm", "--"], | 
					
						
							|  |  |  |           input:        pkgutil_bom_files.join("\0"), | 
					
						
							|  |  |  |           sudo:         true, | 
					
						
							|  |  |  |           sudo_as_root: true, | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       unless pkgutil_bom_specials.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg symlinks and special files" | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |         @command.run!( | 
					
						
							|  |  |  |           "/usr/bin/xargs", | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:07 -07:00
										 |  |  |           args:         ["-0", "--", "/bin/rm", "--"], | 
					
						
							|  |  |  |           input:        pkgutil_bom_specials.join("\0"), | 
					
						
							|  |  |  |           sudo:         true, | 
					
						
							|  |  |  |           sudo_as_root: true, | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       unless pkgutil_bom_dirs.empty? | 
					
						
							|  |  |  |         odebug "Deleting pkg directories" | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |         rmdir(deepest_path_first(pkgutil_bom_dirs)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-03-08 03:03:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |       rmdir(root) unless MacOS.undeletable?(root) | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       forget | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { void } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def forget | 
					
						
							|  |  |  |       odebug "Unregistering pkg receipt (aka forgetting)" | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:07 -07:00
										 |  |  |       @command.run!( | 
					
						
							|  |  |  |         "/usr/sbin/pkgutil", | 
					
						
							|  |  |  |         args:         ["--forget", package_id], | 
					
						
							|  |  |  |         sudo:         true, | 
					
						
							|  |  |  |         sudo_as_root: true, | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def pkgutil_bom_files | 
					
						
							|  |  |  |       @pkgutil_bom_files ||= pkgutil_bom_all.select(&:file?) - pkgutil_bom_specials | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def pkgutil_bom_specials | 
					
						
							|  |  |  |       @pkgutil_bom_specials ||= pkgutil_bom_all.select(&method(:special?)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def pkgutil_bom_dirs | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |       @pkgutil_bom_dirs ||= pkgutil_bom_all.select(&:directory?) - pkgutil_bom_specials | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def pkgutil_bom_all | 
					
						
							| 
									
										
										
										
											2017-04-21 15:50:39 +02:00
										 |  |  |       @pkgutil_bom_all ||= @command.run!("/usr/sbin/pkgutil", args: ["--files", package_id]) | 
					
						
							|  |  |  |                                    .stdout | 
					
						
							|  |  |  |                                    .split("\n") | 
					
						
							|  |  |  |                                    .map { |path| root.join(path) } | 
					
						
							| 
									
										
										
										
											2017-08-06 12:30:40 +02:00
										 |  |  |                                    .reject(&MacOS.public_method(:undeletable?)) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { returns(Pathname) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def root | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |       @root ||= Pathname.new(info.fetch("volume")).join(info.fetch("install-location")) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     def info | 
					
						
							| 
									
										
										
										
											2017-04-21 15:50:39 +02:00
										 |  |  |       @info ||= @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id]) | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |                         .plist | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { params(path: Pathname).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def special?(path) | 
					
						
							|  |  |  |       path.symlink? || path.chardev? || path.blockdev? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |     # Helper script to delete empty directories after deleting `.DS_Store` files and broken symlinks. | 
					
						
							|  |  |  |     # Needed in order to execute all file operations with `sudo`. | 
					
						
							| 
									
										
										
										
											2021-03-30 01:09:01 +02:00
										 |  |  |     RMDIR_SH = (HOMEBREW_LIBRARY_PATH/"cask/utils/rmdir.sh").freeze | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |     private_constant :RMDIR_SH | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sig { params(path: T.any(Pathname, T::Array[Pathname])).void } | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def rmdir(path) | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |       @command.run!( | 
					
						
							|  |  |  |         "/usr/bin/xargs", | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:07 -07:00
										 |  |  |         args:         ["-0", "--", RMDIR_SH.to_s], | 
					
						
							|  |  |  |         input:        Array(path).join("\0"), | 
					
						
							|  |  |  |         sudo:         true, | 
					
						
							|  |  |  |         sudo_as_root: true, | 
					
						
							| 
									
										
										
										
											2021-03-16 02:07:06 +01:00
										 |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 03:39:48 +01:00
										 |  |  |     sig { params(paths: T::Array[Pathname]).returns(T::Array[Pathname]) } | 
					
						
							| 
									
										
										
										
											2017-03-09 22:01:46 +01:00
										 |  |  |     def deepest_path_first(paths) | 
					
						
							|  |  |  |       paths.sort_by { |path| -path.to_s.split(File::SEPARATOR).count } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |