Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def extract_artifacts(include_manual_installers: false, include_all_signable_art
)
end
valid_formula_installers = Homebrew::Install.fetch_formulae(formula_installers)
::Utils::Analytics.report_formula_installs(valid_formula_installers)

formula_installers.each do |fi|
next unless valid_formula_installers.include?(fi)
Expand Down
25 changes: 13 additions & 12 deletions Library/Homebrew/cask/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ def install
tab.installed_on_request = installed_on_request?
tab.write

report_analytics

purge_backed_up_versioned_files

puts summary
Expand All @@ -227,15 +225,6 @@ def install
@cask.cleanup_temp(retain: !completed)
end

# Report a successful installation or upgrade.
sig { void }
def report_analytics
if (tap = @cask.tap) && tap.should_report_analytics?
::Utils::Analytics.report_package_event(:cask_install, package_name: @cask.token, tap_name: tap.name,
on_request: true)
end
end

sig { void }
def check_deprecate_disable
deprecate_disable_type = DeprecateDisable.type(@cask)
Expand Down Expand Up @@ -531,7 +520,16 @@ def satisfy_cask_and_formula_dependencies
raise CaskError, "Dependency download failed for #{failed_installer.cask}."
end

cask_installers.reject { |installer| installer.cask.installed? }.each(&:install)
missing_cask_installers = cask_installers.reject { |installer| installer.cask.installed? }
# Without a prefetch, download (and verify) them before reporting; installing reuses the downloads.
unless @dependencies_enqueued
missing_cask_installers.each do |installer|
installer.prelude
installer.download
end
end
::Utils::Analytics.report_cask_installs(missing_cask_installers)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
missing_cask_installers.each(&:install)
return if formula_installers.blank?

Homebrew::Install.perform_preinstall_checks_once
Expand All @@ -540,6 +538,9 @@ def satisfy_cask_and_formula_dependencies
else
Homebrew::Install.fetch_formulae(formula_installers)
end
::Utils::Analytics.report_formula_installs(valid_formula_installers.reject do |formula_installer|
formula_installer.formula.any_version_installed? && formula_installer.formula.optlinked?
end)
valid_formula_installers.each do |formula_installer|
next if formula_installer.formula.any_version_installed? && formula_installer.formula.optlinked?

Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cask/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def self.reinstall_casks(
# Reinstall everything that did download and report each failure as it
# happens, rather than aborting the whole run; the failures still exit
# nonzero at the end.
# Callers that prefetched the casks have already reported them.
::Utils::Analytics.report_cask_installs(cask_installers) unless skip_prefetch

reinstalled_casks = T.let([], T::Array[Cask])
cask_installers.each do |installer|
installer.install
Expand Down
73 changes: 59 additions & 14 deletions Library/Homebrew/cask/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,30 @@ def self.upgrade_casks!(

download_queue ||= Homebrew::DownloadQueue.default

# Report every cask once it has been downloaded and verified. A cask whose prefetch couldn't be
# enqueued gets the installer `upgrade_cask` would fall back to and is downloaded now instead.
fallback_cask_installers = T.let([], T::Array[Installer])
new_cask_installers = upgradable_casks.map do |(old_cask, new_cask)|
prefetched_cask_installers.find { |installer| installer.cask.equal?(new_cask) } ||
begin
installer = fallback_installer(old_cask, new_cask, binaries:, force:, require_sha:,
skip_cask_deps:, verbose:, download_queue:)
installer.prelude
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
installer.download
fallback_cask_installers << installer
installer
rescue
# `upgrade_cask` retries it and reports it if that download succeeds.
nil
end
end
# Callers that prefetched the casks have already reported them.
::Utils::Analytics.report_cask_installs(skip_prefetch ? fallback_cask_installers : new_cask_installers.compact)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

upgradable_casks.each_with_index do |(old_cask, new_cask), index|
new_cask_installer = prefetched_cask_installers.find { |installer| installer.cask.equal?(new_cask) }
new_cask_installer = new_cask_installers.fetch(index)
# A fallback's prelude ran before any upgrade, which may have since installed a conflicting cask.
new_cask_installer&.check_conflicts if fallback_cask_installers.include?(new_cask_installer)
upgrade_cask(
old_cask, new_cask,
binaries:, force:, skip_cask_deps:, verbose:,
Expand Down Expand Up @@ -373,6 +395,37 @@ def self.reopen_apps_after_upgrade(old_cask, new_cask)
end
private_class_method :reopen_apps_after_upgrade

# The installer `upgrade_cask` uses for a cask that wasn't prefetched.
sig {
params(
old_cask: Cask,
new_cask: Cask,
binaries: T.nilable(T::Boolean),
force: T.nilable(T::Boolean),
require_sha: T.nilable(T::Boolean),
skip_cask_deps: T.nilable(T::Boolean),
verbose: T.nilable(T::Boolean),
download_queue: Homebrew::DownloadQueue,
).returns(Installer)
}
def self.fallback_installer(old_cask, new_cask, binaries:, force:, require_sha:, skip_cask_deps:, verbose:,
download_queue:)
new_cask.config = new_cask.default_config.merge(old_cask.config)

new_options = {
binaries:,
verbose:,
force:,
skip_cask_deps:,
require_sha:,
upgrade: true,
download_queue:,
}.compact

Installer.new(new_cask, **new_options, defer_fetch: true)
end
private_class_method :fallback_installer

sig {
params(
old_cask: Cask,
Expand Down Expand Up @@ -410,17 +463,9 @@ def self.upgrade_cask(

new_cask.config = new_cask.default_config.merge(old_config)

new_options = {
binaries:,
verbose:,
force:,
skip_cask_deps:,
require_sha:,
upgrade: true,
download_queue:,
}.compact

new_cask_installer ||= Installer.new(new_cask, **new_options, defer_fetch: true)
built_fallback_installer = new_cask_installer.nil?
new_cask_installer ||= fallback_installer(old_cask, new_cask, binaries:, force:, require_sha:,
skip_cask_deps:, verbose:, download_queue:)
raise CaskError, "Download failed for #{new_cask}." if new_cask_installer.download_failed?

started_upgrade = false
Expand Down Expand Up @@ -465,6 +510,8 @@ def self.upgrade_cask(

# Install the new cask
new_cask_installer.stage
# Staging downloaded (and verified) a fallback built here, which nothing has reported yet.
::Utils::Analytics.report_cask_installs([new_cask_installer]) if built_fallback_installer
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

new_cask_installer.install_artifacts(predecessor: old_cask)
new_artifacts_installed = true
Expand Down Expand Up @@ -524,8 +571,6 @@ def self.upgrade_cask(
tab.installed_on_request = old_tab.tabfile.nil? || old_tab.installed_on_request
tab.write

new_cask_installer.report_analytics

end_time = Time.now
Homebrew.messages.package_installed(new_cask.token, end_time - start_time)
completed = true
Expand Down
35 changes: 20 additions & 15 deletions Library/Homebrew/cmd/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ def run
formulae_installer = fetched.formula_installers
dependent_formulae_installer = fetched.dependent_formula_installers
prefetched_cask_installers = fetched.cask_installers
Utils::Analytics.report_formula_installs(formulae_installer + dependent_formulae_installer)
Utils::Analytics.report_cask_installs(prefetched_cask_installers)
end
shared_download_queue&.shutdown

Expand Down Expand Up @@ -523,25 +525,28 @@ def fetch_installers(formulae_installer, dependent_formulae_installer, fetch_cas
def install_casks(new_casks, installed_casks, failed_casks, prefetched_cask_installers, cask_upgrade_summary,
installed_on_request:)
installed_or_upgraded_casks = T.let([], T::Array[Cask::Cask])
new_casks.each do |cask|
new_cask_installers = new_casks.filter_map do |cask|
next if failed_casks.any? { |failed_cask| failed_cask.equal?(cask) }

installer = prefetched_cask_installers.find { |candidate| candidate.cask.equal?(cask) }
installer ||= Cask::Installer.new(
cask,
adopt: args.adopt?,
binaries: args.binaries?,
force: args.force?,
quiet: args.quiet?,
require_sha: args.cask_require_sha?,
skip_cask_deps: args.skip_cask_deps?,
verbose: args.verbose?,
installed_on_request:,
)
prefetched_cask_installers.find { |candidate| candidate.cask.equal?(cask) } ||
Cask::Installer.new(
cask,
adopt: args.adopt?,
binaries: args.binaries?,
force: args.force?,
quiet: args.quiet?,
require_sha: args.cask_require_sha?,
skip_cask_deps: args.skip_cask_deps?,
verbose: args.verbose?,
installed_on_request:,
)
end

new_cask_installers.each do |installer|
installer.install
installed_or_upgraded_casks << cask
installed_or_upgraded_casks << installer.cask
rescue => e
ofail "#{cask.full_name}: #{e}"
ofail "#{installer.cask.full_name}: #{e}"
end

installed_casks_to_upgrade = installed_casks.reject do |cask|
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cmd/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ def run
download_queue.shutdown
end

Utils::Analytics.report_formula_installs(formulae_installers + dependent_formulae_installers)

# Reinstall everything that did download, rather than aborting the
# whole run; the failures above still exit nonzero at the end.
reinstall_contexts.each do |reinstall_context|
Expand Down Expand Up @@ -332,6 +334,7 @@ def run

reinstalled_casks = T.let([], T::Array[Cask::Cask])
if casks.any?
Utils::Analytics.report_cask_installs(prefetched_cask_installers) if casks_prefetched
begin
reinstalled_casks = Cask::Reinstall.reinstall_casks(
*casks,
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/cmd/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def run
end
if !only_upgrade_formulae && !skip_upgrades_after_failed_ask_preview
if prefetched_casks
Utils::Analytics.report_cask_installs(prefetched_cask_installers)
upgrade_outdated_casks!(
prefetched_cask_upgrade_casks,
skip_prefetch: true,
Expand Down Expand Up @@ -718,6 +719,9 @@ def upgrade_outdated_formulae!(formulae, prefetch_only: false, use_prefetched: f
[]
end

if use_prefetched_context
Utils::Analytics.report_formula_installs(context.formulae_installer + context.dependent_formulae_installer)
end
upgraded_formula_installers = Upgrade.upgrade_formulae(
context.formulae_installer,
dry_run:,
Expand Down
7 changes: 6 additions & 1 deletion Library/Homebrew/dev-cmd/cask-ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def run
cask = load_cask(require_cask_path!(operation, cask_path))
raise Cask::CaskError, "#{cask} is already installed." if cask.installed?

Cask::Installer.new(cask, verbose: args.verbose?).install
installer = Cask::Installer.new(cask, verbose: args.verbose?)
# Download (and verify) the cask before reporting it; installing reuses the download.
installer.prelude
installer.download
Utils::Analytics.report_cask_installs([installer])
installer.install
Homebrew.messages.display_messages(force_caveats: true)
when "snapshot"
raise UsageError, "The `snapshot` operation does not accept a cask." if cask_path
Expand Down
10 changes: 10 additions & 0 deletions Library/Homebrew/download_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ def fetch(only: nil, heading: nil, allow_failures: false)
sig { returns(T::Array[Downloadable]) }
attr_reader :failed_downloads

# Waits for just this download and returns whether it downloaded and passed its checksum
# verification. A failure is still reported once, by `fetch`.
sig { params(downloadable: Downloadable).returns(T::Boolean) }
def downloaded_and_verified?(downloadable)
download = @downloads_by_location[downloadable.cached_download]
return downloadable.downloaded_and_valid? if download.nil?

download.wait.fulfilled?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Data Integrity & Integration · Quick win

Require content verification before returning true.

For a corrupt cached immutable bottle, Bottle#downloaded_and_valid? can return true because its filename matches the expected digest. enqueue then fulfils this raw future at Line 71 without checking the contents. This method returns true and permits install_fetch_deps to report the unverified dependency.

Use a content-verification result before returning true. Preserve the silent false result for rejected downloads.

end

sig { params(message: String).void }
def stdout_print_and_flush_if_tty(message)
stdout_print_and_flush(message) if tty_with_cursor_move_support?
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/extend/os/mac/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def reinstall_pkgconf_if_needed!(dry_run: false)
end

require "install"
require "utils/analytics"

pkgconf = ::Formula["pkgconf"]

Expand All @@ -32,6 +33,7 @@ def reinstall_pkgconf_if_needed!(dry_run: false)
begin
return if Homebrew::Install.fetch_formulae([context.formula_installer]).empty?

::Utils::Analytics.report_formula_installs([context.formula_installer])
reinstall_formula(context)
ohai "Reinstalled pkgconf due to macOS version mismatch"
rescue
Expand Down
Loading
Loading