Skip to content

Commit

Permalink
Merge pull request #135 from woodruffw-forks/ww/refactor-formula-all
Browse files Browse the repository at this point in the history
executables_db: avoid `Formula#all`
  • Loading branch information
MikeMcQuaid authored Oct 23, 2023
2 parents d70d6e8 + a565eb7 commit 036375f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
5 changes: 4 additions & 1 deletion cmd/which-update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def which_update_args
description: "Update database entries with outdated formula versions."
switch "--install-missing",
description: "Install and update formulae that are missing from the database and don't have bottles."
switch "--eval-all",
description: "Evaluate all installed taps, rather than just the core tap."
flag "--max-downloads=",
description: "Specify a maximum number of formulae to download and update."
conflicts "--stats", "--commit"
Expand All @@ -39,7 +41,8 @@ def which_update
Homebrew::WhichUpdate.update_and_save! source: args.named.first, commit: args.commit?,
update_existing: args.update_existing?,
install_missing: args.install_missing?,
max_downloads: args.max_downloads
max_downloads: args.max_downloads,
eval_all: args.eval_all?
end
end
end
77 changes: 43 additions & 34 deletions lib/executables_db.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "formula"
require "formulary"
require "tap"

module Homebrew
# ExecutablesDB represents a DB associating formulae to the binaries they
Expand Down Expand Up @@ -51,43 +53,50 @@ def changed?

# update the DB with the installed formulae
# @see #save!
def update!(update_existing: false, install_missing: false, max_downloads: nil)
def update!(update_existing: false, install_missing: false, max_downloads: nil, eval_all: false)
downloads = 0
disabled_formulae = []
Formula.all.each do |f|
break if max_downloads.present? && downloads > max_downloads.to_i
next if f.tap?

name = f.full_name

if f.disabled?
disabled_formulae << name
next
end

update_formula = missing_formula?(f) || (update_existing && outdated_formula?(f))

# Install unbottled formulae if they should be added/updated
if !f.bottled? && install_missing && update_formula
downloads += 1
ohai "Installing #{f}"
system HOMEBREW_BREW_FILE, "install", "--formula", f.to_s
end

# We don't need to worry about updating outdated versions unless update_existing is true
if f.latest_version_installed?
update_installed_formula f
elsif f.bottled? && update_formula
downloads += 1
update_bottled_formula f
end

# renamed formulae
mv f.oldname, name if !f.oldname.nil? && @exes[f.oldname]

# aliased formulae
f.aliases.each do |a|
mv a, name if @exes[a]
# Evaluate only the core tap by default.
taps = eval_all ? Tap.each.to_a : [CoreTap.instance]
taps.each do |tap|
tap.formula_files_by_name.each do |name, path|
f = Formulary.load_formula_from_path(name, path)

break if max_downloads.present? && downloads > max_downloads.to_i
next if f.tap?

name = f.full_name

if f.disabled?
disabled_formulae << name
next
end

update_formula = missing_formula?(f) || (update_existing && outdated_formula?(f))

# Install unbottled formulae if they should be added/updated
if !f.bottled? && install_missing && update_formula
downloads += 1
ohai "Installing #{f}"
system HOMEBREW_BREW_FILE, "install", "--formula", f.to_s
end

# We don't need to worry about updating outdated versions unless update_existing is true
if f.latest_version_installed?
update_installed_formula f
elsif f.bottled? && update_formula
downloads += 1
update_bottled_formula f
end

# renamed formulae
mv f.oldname, name if !f.oldname.nil? && @exes[f.oldname]

# aliased formulae
f.aliases.each do |a|
mv a, name if @exes[a]
end
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/which_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ def stats(source: nil)
end

def update_and_save!(source: nil, commit: false, update_existing: false, install_missing: false,
max_downloads: nil)
max_downloads: nil, eval_all: false)
source ||= default_source
db = ExecutablesDB.new source
db.update!(update_existing: update_existing, install_missing: install_missing, max_downloads: max_downloads)
db.update!(update_existing: update_existing, install_missing: install_missing,
max_downloads: max_downloads, eval_all: eval_all)
db.save!
return if !commit || !db.changed?

Expand Down

0 comments on commit 036375f

Please sign in to comment.