Class: Homebrew::DevCmd::GenerateZap Private
- Inherits:
-
AbstractCommand
- Object
- AbstractCommand
- Homebrew::DevCmd::GenerateZap
- Includes:
- SystemCommand::Mixin
- Defined in:
- dev-cmd/generate-zap.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/generate_zap.rbi
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Defined Under Namespace
Classes: Args
Constant Summary collapse
- USER_TRASH_PATHS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
[ "Desktop", "Documents", "Library", "Library/Application Scripts", "Library/Application Support", "Library/Application Support/CrashReporter", "Library/Application Support/com.apple.sharedfilelist/" \ "com.apple.LSSharedFileList.ApplicationRecentDocuments", "Library/Caches", "Library/Caches/com.apple.helpd/Generated", "Library/Caches/com.apple.helpd/SDMHelpData/Other/English/HelpSDMIndexFile", "Library/Containers", "Library/Cookies", "Library/Group Containers", "Library/HTTPStorages", "Library/Internet Plug-Ins", "Library/LaunchAgents", "Library/Logs", "Library/Logs/DiagnosticReports", "Library/PreferencePanes", "Library/Preferences", "Library/Preferences/ByHost", "Library/Saved Application State", "Library/WebKit", "Music", ].freeze
- SYSTEM_DELETE_PATHS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
[ "/Library/Application Support", "/Library/Caches", "/Library/Frameworks", "/Library/LaunchAgents", "/Library/LaunchDaemons", "/Library/Logs", "/Library/PreferencePanes", "/Library/Preferences", "/Library/PrivilegedHelperTools", "/Library/Screen Savers", "/Library/ScriptingAdditions", "/Library/Services", "/Users/Shared", "/etc/newsyslog.d", ].freeze
- RMDIR_EXCLUSIONS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
[ "Library/Application Support/CrashReporter", "Library/Application Support/com.apple.sharedfilelist/" \ "com.apple.LSSharedFileList.ApplicationRecentDocuments", "/Library/Application Support", "/Library/Caches", "/Library/Preferences", ].freeze
- UUID_PATTERN =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
/[0-9A-F]{8}(-[0-9A-F]{4}){3}-[0-9A-F]{12}/i- SHARED_FILELIST_PATTERN =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
Keep in sync with
RuboCop::Cop::Cask::SharedFilelistGlob. /\.sfl\d\z/
Instance Method Summary collapse
- #args ⇒ Homebrew::DevCmd::GenerateZap::Args private
- #collapse_to_wildcards(paths) ⇒ Array<String> private
- #derive_rmdir_candidates(paths) ⇒ Array<String> private
- #each_readable_child(dir, &block) ⇒ void private
- #format_stanza(trash:, delete:, rmdir:) ⇒ String private
- #glob_shared_filelists(paths) ⇒ Array<String> private
- #normalize_path(path) ⇒ String private
- #patterns_from_app_paths(app_paths) ⇒ Array<String> private
- #replace_uuids(paths) ⇒ Array<String> private
- #resolve_patterns_from_cask(cask) ⇒ Array<String> private
- #run ⇒ void private
- #scan_directories(directories, home_relative:, patterns:) ⇒ Array<String> private
- #scan_home_root(patterns) ⇒ Array<String> private
Methods included from SystemCommand::Mixin
#system_command, #system_command!
Methods inherited from AbstractCommand
command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?
Methods included from Utils::Output::Mixin
#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning
Constructor Details
This class inherits a constructor from Homebrew::AbstractCommand
Instance Method Details
#args ⇒ Homebrew::DevCmd::GenerateZap::Args
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
10 |
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/generate_zap.rbi', line 10 def args; end |
#collapse_to_wildcards(paths) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'dev-cmd/generate-zap.rb', line 243 def collapse_to_wildcards(paths) grouped = paths.group_by { |p| File.dirname(p) } result = [] grouped.each_value do |entries| if entries.size == 1 result << entries.first next end basenames = entries.map { |e| File.basename(e) } wildcarded = find_wildcard_groups(basenames) dir = File.dirname(entries.fetch(0)) wildcarded.each do |name| result << File.join(dir, name) end end result.uniq.sort end |
#derive_rmdir_candidates(paths) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'dev-cmd/generate-zap.rb', line 276 def derive_rmdir_candidates(paths) home = Dir.home candidates = [] paths.each do |path| = path.start_with?("~") ? File.join(home, path[2..]) : path parent = File.dirname() next unless parent.match?(%r{/(Application Support|Containers|Group Containers)/}) normalized = normalize_path(parent) next if RMDIR_EXCLUSIONS.any? { |excl| normalized == "~/#{excl}" || normalized == excl } candidates << normalized unless paths.include?(normalized) end candidates.uniq.sort end |
#each_readable_child(dir, &block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
235 236 237 238 239 240 |
# File 'dev-cmd/generate-zap.rb', line 235 def each_readable_child(dir, &block) Dir.each_child(dir, &block) rescue Errno::EPERM, Errno::EACCES # Skip directories we lack permission to read, e.g. macOS-protected paths. nil end |
#format_stanza(trash:, delete:, rmdir:) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
309 310 311 312 313 314 315 316 317 |
# File 'dev-cmd/generate-zap.rb', line 309 def format_stanza(trash:, delete:, rmdir:) directives = [] directives << format_directive("trash", trash) unless trash.empty? directives << format_directive("delete", delete) unless delete.empty? directives << format_directive("rmdir", rmdir) unless rmdir.empty? directives.join(",\n") .prepend("zap ") end |
#glob_shared_filelists(paths) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
271 272 273 |
# File 'dev-cmd/generate-zap.rb', line 271 def glob_shared_filelists(paths) paths.map { |p| p.sub(SHARED_FILELIST_PATTERN, ".sfl*") }.uniq.sort end |
#normalize_path(path) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
297 298 299 300 |
# File 'dev-cmd/generate-zap.rb', line 297 def normalize_path(path) home = Dir.home path.start_with?(home) ? path.sub(home, "~") : path end |
#patterns_from_app_paths(app_paths) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'dev-cmd/generate-zap.rb', line 175 def patterns_from_app_paths(app_paths) app_paths.flat_map do |app_path| patterns = [app_path.basename(".app").to_s] info_plist = app_path/"Contents/Info.plist" next patterns if !info_plist.exist? || !info_plist.readable? plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist]).plist bundle_identifier = plist["CFBundleIdentifier"] patterns << bundle_identifier if bundle_identifier.is_a?(String) patterns end.uniq end |
#replace_uuids(paths) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
266 267 268 |
# File 'dev-cmd/generate-zap.rb', line 266 def replace_uuids(paths) paths.map { |p| p.gsub(UUID_PATTERN, "*") }.uniq.sort end |
#resolve_patterns_from_cask(cask) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'dev-cmd/generate-zap.rb', line 133 def resolve_patterns_from_cask(cask) app_artifact = cask.artifacts.find { |a| a.is_a?(Cask::Artifact::App) } if app_artifact return patterns_from_app_paths([app_artifact.target]) end token_pattern = cask.token.tr("-", " ").split.map(&:capitalize).join(" ") pkgutil_receipt_patterns = T.let([], T::Array[String]) cask.artifacts.each do |artifact| next unless artifact.is_a?(Cask::Artifact::AbstractUninstall) pkgutil = artifact.directives[:pkgutil] if pkgutil.is_a?(String) pkgutil_receipt_patterns << pkgutil elsif pkgutil.is_a?(Array) pkgutil.each { |pattern| pkgutil_receipt_patterns << pattern if pattern.is_a?(String) } end end pkg_app_paths = pkgutil_receipt_patterns.uniq.flat_map do |receipt_pattern| Cask::Pkg.all_matching(receipt_pattern, SystemCommand).flat_map do |pkg| pkg.pkgutil_bom_all.filter_map do |path| match = path.to_s.match(%r{\A(?<app_path>.*?\.app)(?:/|\z)}i) next if match.nil? Pathname.new(match[:app_path]) end end end.uniq.select(&:directory?) unless pkg_app_paths.empty? ohai "No app artifact found in cask \"#{cask.token}\"; using installed package receipts." patterns = patterns_from_app_paths(pkg_app_paths) patterns << token_pattern unless patterns.any? { |pattern| pattern.casecmp?(token_pattern) } return patterns end ohai "No app artifact or installed package app found in cask \"#{cask.token}\"; using token as app name." [token_pattern] end |
#run ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'dev-cmd/generate-zap.rb', line 95 def run patterns = if args.name? [args.named.fetch(0)] else resolve_patterns_from_cask(args.named.to_casks.fetch(0)) end ohai "Scanning for files matching #{format_patterns(patterns)}..." begin trash_paths = scan_directories(USER_TRASH_PATHS, home_relative: true, patterns:) + scan_home_root(patterns) delete_paths = scan_directories(SYSTEM_DELETE_PATHS, home_relative: false, patterns:) rescue Errno::EACCES, Errno::EPERM => e = "Unable to generate a complete zap stanza: #{e.}" unless Cask::Utils.full_disk_access_enabled? += " Please enable Full Disk Access for your terminal under " \ "#{Cask::Utils.privacy_security_preference_pane("Full Disk Access")}." end odie end trash_paths = glob_shared_filelists(replace_uuids(collapse_to_wildcards(trash_paths))) delete_paths = glob_shared_filelists(replace_uuids(collapse_to_wildcards(delete_paths))) rmdir_paths = derive_rmdir_candidates(trash_paths + delete_paths) if trash_paths.empty? && delete_paths.empty? opoo "No files found matching #{format_patterns(patterns)}." puts "# No zap stanza required" return end puts format_stanza(trash: trash_paths, delete: delete_paths, rmdir: rmdir_paths) end |
#scan_directories(directories, home_relative:, patterns:) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'dev-cmd/generate-zap.rb', line 195 def scan_directories(directories, home_relative:, patterns:) home = Dir.home downcased_patterns = patterns.map(&:downcase) matches = [] directories.each do |dir| full_dir = home_relative ? File.join(home, dir) : dir next unless File.directory?(full_dir) each_readable_child(full_dir) do |entry| downcased_entry = entry.downcase next unless downcased_patterns.any? { |pattern| downcased_entry.include?(pattern) } full_path = File.join(full_dir, entry) matches << normalize_path(full_path) end end matches.uniq.sort end |
#scan_home_root(patterns) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'dev-cmd/generate-zap.rb', line 217 def scan_home_root(patterns) home = Dir.home downcased_patterns = patterns.map(&:downcase) matches = [] each_readable_child(home) do |entry| next unless entry.start_with?(".") downcased_entry = entry.downcase next unless downcased_patterns.any? { |pattern| downcased_entry.include?(pattern) } matches << normalize_path(File.join(home, entry)) end matches.sort end |