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
- #bundle_identifiers(app_artifact) ⇒ Array<String> 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
- #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 |
#bundle_identifiers(app_artifact) ⇒ 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.
146 147 148 149 150 151 152 153 154 155 |
# File 'dev-cmd/generate-zap.rb', line 146 def bundle_identifiers(app_artifact) info_plist = app_artifact.target/"Contents/Info.plist" return [] if !info_plist.exist? || !info_plist.readable? plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist]).plist bundle_identifier = plist["CFBundleIdentifier"] return [] unless bundle_identifier.is_a?(String) [bundle_identifier] 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.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'dev-cmd/generate-zap.rb', line 212 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.
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 245 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.
204 205 206 207 208 209 |
# File 'dev-cmd/generate-zap.rb', line 204 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.
278 279 280 281 282 283 284 285 286 |
# File 'dev-cmd/generate-zap.rb', line 278 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.
240 241 242 |
# File 'dev-cmd/generate-zap.rb', line 240 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.
266 267 268 269 |
# File 'dev-cmd/generate-zap.rb', line 266 def normalize_path(path) home = Dir.home path.start_with?(home) ? path.sub(home, "~") : path 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.
235 236 237 |
# File 'dev-cmd/generate-zap.rb', line 235 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 |
# 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 patterns = [app_artifact.target.basename(".app").to_s] patterns.concat(bundle_identifiers(app_artifact)) patterns.uniq else ohai "No app artifact found in cask \"#{cask.token}\"; using token as app name." [cask.token.tr("-", " ").split.map(&:capitalize).join(" ")] end 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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'dev-cmd/generate-zap.rb', line 164 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.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'dev-cmd/generate-zap.rb', line 186 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 |