| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 19:39:07 +01:00
										 |  |  | require "cask/artifact/relocated" | 
					
						
							| 
									
										
										
										
											2023-04-03 20:21:37 -05:00
										 |  |  | require "cask/quarantine" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 08:29:14 +02:00
										 |  |  | module Cask | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |   module Artifact | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |     # Superclass for all artifacts that are installed by moving them to the target location. | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     class Moved < Relocated | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |       sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.english_description | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |         "#{english_name}s" | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def install_phase(**options) | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |         move(**options) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       def uninstall_phase(**options) | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |         move_back(**options) | 
					
						
							| 
									
										
										
										
											2017-04-06 00:33:31 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def summarize_installed | 
					
						
							|  |  |  |         if target.exist? | 
					
						
							|  |  |  |           "#{printable_target} (#{target.abv})" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Formatter.error(printable_target, label: "Missing #{self.class.english_name}") | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											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
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-26 11:14:30 +02:00
										 |  |  |       def move(adopt: false, auto_updates: false, force: false, verbose: false, predecessor: nil, reinstall: false, | 
					
						
							| 
									
										
										
										
											2023-04-04 11:36:27 -05:00
										 |  |  |                command: nil, **options) | 
					
						
							| 
									
										
										
										
											2022-10-21 23:28:51 -04:00
										 |  |  |         unless source.exist? | 
					
						
							|  |  |  |           raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         if Utils.path_occupied?(target) | 
					
						
							| 
									
										
										
										
											2023-04-24 10:59:01 -05:00
										 |  |  |           if target.directory? && target.children.empty? && matching_artifact?(predecessor) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |             # An upgrade removed the directory contents but left the directory itself (see below). | 
					
						
							|  |  |  |             unless source.directory? | 
					
						
							|  |  |  |               if target.parent.writable? && !force | 
					
						
							|  |  |  |                 target.rmdir | 
					
						
							|  |  |  |               else | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |                 Utils.gain_permissions_remove(target, command:) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             if adopt | 
					
						
							|  |  |  |               ohai "Adopting existing #{self.class.english_name} at '#{target}'" | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-26 13:41:22 +02:00
										 |  |  |               unless auto_updates | 
					
						
							|  |  |  |                 source_plist = Pathname("#{source}/Contents/Info.plist") | 
					
						
							|  |  |  |                 target_plist = Pathname("#{target}/Contents/Info.plist") | 
					
						
							|  |  |  |                 same = if source_plist.size? && | 
					
						
							|  |  |  |                           (source_bundle_version = Homebrew::BundleVersion.from_info_plist(source_plist)) && | 
					
						
							|  |  |  |                           target_plist.size? && | 
					
						
							|  |  |  |                           (target_bundle_version = Homebrew::BundleVersion.from_info_plist(target_plist)) | 
					
						
							|  |  |  |                   if source_bundle_version.short_version == target_bundle_version.short_version | 
					
						
							|  |  |  |                     if source_bundle_version.version == target_bundle_version.version | 
					
						
							|  |  |  |                       true | 
					
						
							|  |  |  |                     else | 
					
						
							|  |  |  |                       onoe "The bundle version of #{source} is #{source_bundle_version.version} but " \ | 
					
						
							|  |  |  |                            "is #{target_bundle_version.version} for #{target}!" | 
					
						
							|  |  |  |                       false | 
					
						
							|  |  |  |                     end | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |                   else | 
					
						
							| 
									
										
										
										
											2024-09-26 13:41:22 +02:00
										 |  |  |                     onoe "The bundle short version of #{source} is #{source_bundle_version.short_version} but " \ | 
					
						
							|  |  |  |                          "is #{target_bundle_version.short_version} for #{target}!" | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |                     false | 
					
						
							|  |  |  |                   end | 
					
						
							|  |  |  |                 else | 
					
						
							| 
									
										
										
										
											2024-09-26 13:41:22 +02:00
										 |  |  |                   command.run( | 
					
						
							|  |  |  |                     "/usr/bin/diff", | 
					
						
							|  |  |  |                     args:         ["--recursive", "--brief", source, target], | 
					
						
							|  |  |  |                     verbose:, | 
					
						
							|  |  |  |                     print_stdout: verbose, | 
					
						
							|  |  |  |                   ).success? | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-26 13:41:22 +02:00
										 |  |  |                 unless same | 
					
						
							|  |  |  |                   raise CaskError, | 
					
						
							|  |  |  |                         "It seems the existing #{self.class.english_name} is different from " \ | 
					
						
							|  |  |  |                         "the one being installed." | 
					
						
							|  |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |               end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               # Remove the source as we don't need to move it to the target location | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |               FileUtils.rm_r(source) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |               return post_move(command) | 
					
						
							| 
									
										
										
										
											2022-10-21 23:28:51 -04:00
										 |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |             message = "It seems there is already #{self.class.english_article} " \ | 
					
						
							|  |  |  |                       "#{self.class.english_name} at '#{target}'" | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |             raise CaskError, "#{message}." if !force && !adopt | 
					
						
							| 
									
										
										
										
											2022-10-21 23:28:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |             opoo "#{message}; overwriting." | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |             delete(target, force:, command:, **options) | 
					
						
							| 
									
										
										
										
											2022-10-21 23:28:51 -04:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-03-10 09:33:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 15:21:24 -05:00
										 |  |  |         ohai "Moving #{self.class.english_name} '#{source.basename}' to '#{target}'" | 
					
						
							| 
									
										
										
										
											2023-01-13 16:15:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         Utils.gain_permissions_mkpath(target.dirname, command:) unless target.dirname.exist? | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         if target.directory? && Quarantine.app_management_permissions_granted?(app: target, command:) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |           if target.writable? | 
					
						
							| 
									
										
										
										
											2023-05-11 12:47:37 -05:00
										 |  |  |             source.children.each { |child| FileUtils.move(child, target/child.basename) } | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2024-06-17 21:17:10 -07:00
										 |  |  |             command.run!("/bin/cp", args: ["-pR", *source.children, target], | 
					
						
							|  |  |  |                                     sudo: true) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           Quarantine.copy_xattrs(source, target, command:) | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |           FileUtils.rm_r(source) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |         elsif target.dirname.writable? | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |           FileUtils.move(source, target) | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2023-01-13 18:10:21 -08:00
										 |  |  |           # default sudo user isn't necessarily able to write to Homebrew's locations | 
					
						
							|  |  |  |           # e.g. with runas_default set in the sudoers (5) file. | 
					
						
							| 
									
										
										
										
											2024-06-17 21:17:10 -07:00
										 |  |  |           command.run!("/bin/cp", args: ["-pR", source, target], sudo: true) | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |           FileUtils.rm_r(source) | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-21 23:28:51 -04:00
										 |  |  |         post_move(command) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Performs any actions necessary after the source has been moved to the target location. | 
					
						
							|  |  |  |       def post_move(command) | 
					
						
							| 
									
										
										
										
											2019-10-24 15:15:40 +02:00
										 |  |  |         FileUtils.ln_sf target, source | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         add_altname_metadata(target, source.basename, command:) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 10:59:01 -05:00
										 |  |  |       def matching_artifact?(cask) | 
					
						
							|  |  |  |         return false unless cask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cask.artifacts.any? do |a| | 
					
						
							|  |  |  |           a.instance_of?(self.class) && instance_of?(a.class) && a.target == target | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |       def move_back(skip: false, force: false, adopt: false, command: nil, **options) | 
					
						
							| 
									
										
										
										
											2019-10-24 15:15:40 +02:00
										 |  |  |         FileUtils.rm source if source.symlink? && source.dirname.join(source.readlink) == target | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |         if Utils.path_occupied?(source) | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           message = "It seems there is already #{self.class.english_article} " \ | 
					
						
							|  |  |  |                     "#{self.class.english_name} at '#{source}'" | 
					
						
							| 
									
										
										
										
											2024-03-14 10:31:31 +00:00
										 |  |  |           raise CaskError, "#{message}." if !force && !adopt | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |           opoo "#{message}; overwriting." | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           delete(source, force:, command:, **options) | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         unless target.exist? | 
					
						
							| 
									
										
										
										
											2018-04-28 19:17:28 +10:00
										 |  |  |           return if skip || force | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |           raise CaskError, "It seems the #{self.class.english_name} source '#{target}' is not there." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 15:21:24 -05:00
										 |  |  |         ohai "Backing #{self.class.english_name} '#{target.basename}' up to '#{source}'" | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |         source.dirname.mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 21:17:10 -07:00
										 |  |  |         # We need to preserve extended attributes between copies. | 
					
						
							| 
									
										
										
										
											2024-11-22 22:16:44 +00:00
										 |  |  |         # This may fail and need sudo if the source has files with restricted permissions. | 
					
						
							|  |  |  |         [!source.parent.writable?, true].uniq.each do |sudo| | 
					
						
							|  |  |  |           result = command.run( | 
					
						
							|  |  |  |             "/bin/cp", | 
					
						
							|  |  |  |             args:         ["-pR", target, source], | 
					
						
							|  |  |  |             must_succeed: sudo, | 
					
						
							|  |  |  |             sudo:, | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |           break if result.success? | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-01-21 19:10:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         delete(target, force:, command:, **options) | 
					
						
							| 
									
										
										
										
											2017-11-16 10:40:32 -03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 16:13:12 -05:00
										 |  |  |       def delete(target, force: false, successor: nil, command: nil, **_) | 
					
						
							| 
									
										
										
										
											2021-01-26 15:21:24 -05:00
										 |  |  |         ohai "Removing #{self.class.english_name} '#{target}'" | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  |         raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if undeletable?(target) | 
					
						
							| 
									
										
										
										
											2016-09-20 15:11:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |         return unless Utils.path_occupied?(target) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 11:13:23 -05:00
										 |  |  |         if target.directory? && matching_artifact?(successor) && Quarantine.app_management_permissions_granted?( | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           app: target, command:, | 
					
						
							| 
									
										
										
										
											2023-05-26 11:13:23 -05:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |           # If an app folder is deleted, macOS considers the app uninstalled and removes some data. | 
					
						
							|  |  |  |           # Remove only the contents to handle this case. | 
					
						
							|  |  |  |           target.children.each do |child| | 
					
						
							| 
									
										
										
										
											2024-03-15 03:26:21 +00:00
										 |  |  |             Utils.gain_permissions_remove(child, command:) | 
					
						
							| 
									
										
										
										
											2023-04-03 17:13:57 -05:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-04-01 01:53:29 +02:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |           Utils.gain_permissions_remove(target, command:) | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def undeletable?(target); end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "extend/os/cask/artifact/moved" |