Class: Homebrew::DevCmd::GenerateZap Private

Inherits:
AbstractCommand show all
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

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_cannot_install, #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

#argsHomebrew::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.

Parameters:

Returns:



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'dev-cmd/generate-zap.rb', line 245

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.

Parameters:

Returns:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'dev-cmd/generate-zap.rb', line 278

def derive_rmdir_candidates(paths)
  home = Dir.home
  candidates = []

  paths.each do |path|
    expanded = path.start_with?("~") ? File.join(home, path[2..]) : path
    parent = File.dirname(expanded)

    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.

Parameters:



237
238
239
240
241
242
# File 'dev-cmd/generate-zap.rb', line 237

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.

Parameters:

Returns:



311
312
313
314
315
316
317
318
319
# File 'dev-cmd/generate-zap.rb', line 311

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.

Parameters:

Returns:



273
274
275
# File 'dev-cmd/generate-zap.rb', line 273

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.

Parameters:

Returns:



299
300
301
302
# File 'dev-cmd/generate-zap.rb', line 299

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.

Parameters:

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
# File 'dev-cmd/generate-zap.rb', line 177

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.

Parameters:

Returns:



268
269
270
# File 'dev-cmd/generate-zap.rb', line 268

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.

Parameters:

Returns:



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
173
174
# File 'dev-cmd/generate-zap.rb', line 135

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

#runvoid

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.



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
131
132
# File 'dev-cmd/generate-zap.rb', line 97

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
    message = "Unable to generate a complete zap stanza: #{e.message}"

    unless Cask::Utils.full_disk_access_enabled?
      message += " Please enable Full Disk Access for your terminal under " \
                 "#{Cask::Utils.privacy_security_preference_pane("Full Disk Access")}."
    end

    odie message
  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.

Parameters:

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'dev-cmd/generate-zap.rb', line 197

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.

Parameters:

Returns:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'dev-cmd/generate-zap.rb', line 219

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