From 1a43a9d258ed791e6bcf05569e855bcc70af3d84 Mon Sep 17 00:00:00 2001 From: thibhero Date: Tue, 18 Feb 2025 22:41:40 -0500 Subject: [PATCH] extracting method to Homebrew/install.rb --- Library/Homebrew/cmd/install.rb | 83 ++----------------------------- Library/Homebrew/cmd/reinstall.rb | 83 ++----------------------------- Library/Homebrew/cmd/upgrade.rb | 83 ++----------------------------- Library/Homebrew/install.rb | 76 ++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 240 deletions(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index ec4adb3c73..d14a50def1 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -307,96 +307,19 @@ module Homebrew Install.perform_preinstall_checks_once Install.check_cc_argv(args.cc) - ask_input = lambda { - ohai "Do you want to proceed with the installation? [Y/y/yes/N/n]" - accepted_inputs = %w[y yes] - declined_inputs = %w[n no] - loop do - result = $stdin.gets.chomp.strip.downcase - if accepted_inputs.include?(result) - break - elsif declined_inputs.include?(result) - exit 0 - else - puts "Invalid input. Please enter 'Y', 'y', or 'yes' to proceed, or 'N' to abort." - end - end - } - - # Build a unique list of formulae to size by including: - # 1. The original formulae to install. - # 2. Their outdated dependents (subject to pruning criteria). - # 3. Optionally, any installed formula that depends on one of these and is outdated. - compute_sized_formulae = lambda { |f, check_dep: true, upgrade: true| - sized_formulae = f.flat_map do |formula| - # Always include the formula itself. - formula_list = [formula] - next unless upgrade - - deps = args.build_from_source? ? formula.deps.build : formula.deps.required - # If there are dependencies, try to gather outdated, bottled ones. - if deps.any? && check_dep - outdated_dependents = deps.map(&:to_formula).reject(&:pinned?).select do |dep| - dep.installed_kegs.empty? || (dep.bottled? && dep.outdated?) - end - - formula_list.concat(outdated_dependents) - end - - formula_list - end - - # Add any installed formula that depends on one of the sized formulae and is outdated. - if check_dep && !Homebrew::EnvConfig.no_installed_dependents_check? - sized_formulae.concat(Formula.installed.select do |installed_formula| - installed_formula.outdated? && - installed_formula.deps.required.any? { |dep| sized_formulae.include?(dep.to_formula) } - end) - end - - sized_formulae.uniq(&:to_s) - } - - # Compute the total sizes (download, installed, and net) for the given formulae. - compute_total_sizes = lambda { |sized_formulae, debug: false| - total_download_size = 0 - total_installed_size = 0 - total_net_size = 0 - - sized_formulae.each do |formula| - next unless (bottle = formula.bottle) - - # Fetch additional bottle metadata (if necessary). - bottle.fetch_tab(quiet: !debug) - - total_download_size += bottle.bottle_size.to_i if bottle.bottle_size - total_installed_size += bottle.installed_size.to_i if bottle.installed_size - - # Sum disk usage for all installed kegs of the formula. - next if formula.installed_kegs.none? - - kegs_dep_size = formula.installed_kegs.sum { |keg| keg.disk_usage.to_i } - total_net_size += bottle.installed_size.to_i - kegs_dep_size if bottle.installed_size - end - - { download: total_download_size, - installed: total_installed_size, - net: total_net_size } - } - # Main block: if asking the user is enabled, show dependency and size information. if args.ask? ohai "Looking for bottles..." - sized_formulae = compute_sized_formulae.call(formulae, check_dep: false, upgrade: false) - sizes = compute_total_sizes.call(sized_formulae, debug: args.debug?) + sized_formulae = Install.compute_sized_formulae(formulae, args: args) + sizes = Install.compute_total_sizes(sized_formulae, debug: args.debug?) puts "Formulae: #{sized_formulae.join(", ")}\n\n" puts "Download Size: #{disk_usage_readable(sizes[:download])}" puts "Install Size: #{disk_usage_readable(sizes[:installed])}" puts "Net Install Size: #{disk_usage_readable(sizes[:net])}" if sizes[:net] != 0 - ask_input.call + Install.ask_input end Install.install_formulae( diff --git a/Library/Homebrew/cmd/reinstall.rb b/Library/Homebrew/cmd/reinstall.rb index 54e74af6b9..9c67de6062 100644 --- a/Library/Homebrew/cmd/reinstall.rb +++ b/Library/Homebrew/cmd/reinstall.rb @@ -131,96 +131,19 @@ module Homebrew unless formulae.empty? Install.perform_preinstall_checks_once - ask_input = lambda { - ohai "Do you want to proceed with the installation? [Y/y/yes/N/n]" - accepted_inputs = %w[y yes] - declined_inputs = %w[n no] - loop do - result = $stdin.gets.chomp.strip.downcase - if accepted_inputs.include?(result) - break - elsif declined_inputs.include?(result) - exit 0 - else - puts "Invalid input. Please enter 'Y', 'y', or 'yes' to proceed, or 'N' to abort." - end - end - } - - # Build a unique list of formulae to size by including: - # 1. The original formulae to install. - # 2. Their outdated dependents (subject to pruning criteria). - # 3. Optionally, any installed formula that depends on one of these and is outdated. - compute_sized_formulae = lambda { |f, check_dep: true, upgrade: true| - sized_formulae = f.flat_map do |formula| - # Always include the formula itself. - formula_list = [formula] - next unless upgrade - - deps = args.build_from_source? ? formula.deps.build : formula.deps.required - # If there are dependencies, try to gather outdated, bottled ones. - if deps.any? && check_dep - outdated_dependents = deps.map(&:to_formula).reject(&:pinned?).select do |dep| - dep.installed_kegs.empty? || (dep.bottled? && dep.outdated?) - end - - formula_list.concat(outdated_dependents) - end - - formula_list - end - - # Add any installed formula that depends on one of the sized formulae and is outdated. - if check_dep && !Homebrew::EnvConfig.no_installed_dependents_check? - sized_formulae.concat(Formula.installed.select do |installed_formula| - installed_formula.outdated? && - installed_formula.deps.required.any? { |dep| sized_formulae.include?(dep.to_formula) } - end) - end - - sized_formulae.uniq(&:to_s) - } - - # Compute the total sizes (download, installed, and net) for the given formulae. - compute_total_sizes = lambda { |sized_formulae, debug: false| - total_download_size = 0 - total_installed_size = 0 - total_net_size = 0 - - sized_formulae.each do |formula| - next unless (bottle = formula.bottle) - - # Fetch additional bottle metadata (if necessary). - bottle.fetch_tab(quiet: !debug) - - total_download_size += bottle.bottle_size.to_i if bottle.bottle_size - total_installed_size += bottle.installed_size.to_i if bottle.installed_size - - # Sum disk usage for all installed kegs of the formula. - next if formula.installed_kegs.none? - - kegs_dep_size = formula.installed_kegs.sum { |keg| keg.disk_usage.to_i } - total_net_size += bottle.installed_size.to_i - kegs_dep_size if bottle.installed_size - end - - { download: total_download_size, - installed: total_installed_size, - net: total_net_size } - } - # Main block: if asking the user is enabled, show dependency and size information. if args.ask? ohai "Looking for bottles..." - sized_formulae = compute_sized_formulae.call(formulae, check_dep: false, upgrade: false) - sizes = compute_total_sizes.call(sized_formulae, debug: args.debug?) + sized_formulae = Install.compute_sized_formulae(formulae, args: args) + sizes = Install.compute_total_sizes(sized_formulae, debug: args.debug?) puts "Formulae: #{sized_formulae.join(", ")}\n\n" puts "Download Size: #{disk_usage_readable(sizes[:download])}" puts "Install Size: #{disk_usage_readable(sizes[:installed])}" puts "Net Install Size: #{disk_usage_readable(sizes[:net])}" if sizes[:net] != 0 - ask_input.call + Install.ask_input end formulae.each do |formula| diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 38eaf18f90..a65d96e9ba 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -221,96 +221,19 @@ module Homebrew Install.perform_preinstall_checks_once - ask_input = lambda { - ohai "Do you want to proceed with the installation? [Y/y/yes/N/n]" - accepted_inputs = %w[y yes] - declined_inputs = %w[n no] - loop do - result = $stdin.gets.chomp.strip.downcase - if accepted_inputs.include?(result) - break - elsif declined_inputs.include?(result) - exit 0 - else - puts "Invalid input. Please enter 'Y', 'y', or 'yes' to proceed, or 'N' to abort." - end - end - } - - # Build a unique list of formulae to size by including: - # 1. The original formulae to install. - # 2. Their outdated dependents (subject to pruning criteria). - # 3. Optionally, any installed formula that depends on one of these and is outdated. - compute_sized_formulae = lambda { |f, check_dep: true, upgrade: true| - sized_formulae = f.flat_map do |formula| - # Always include the formula itself. - formula_list = [formula] - next unless upgrade - - deps = args.build_from_source? ? formula.deps.build : formula.deps.required - # If there are dependencies, try to gather outdated, bottled ones. - if deps.any? && check_dep - outdated_dependents = deps.map(&:to_formula).reject(&:pinned?).select do |dep| - dep.installed_kegs.empty? || (dep.bottled? && dep.outdated?) - end - - formula_list.concat(outdated_dependents) - end - - formula_list - end - - # Add any installed formula that depends on one of the sized formulae and is outdated. - if check_dep && !Homebrew::EnvConfig.no_installed_dependents_check? - sized_formulae.concat(Formula.installed.select do |installed_formula| - installed_formula.outdated? && - installed_formula.deps.required.any? { |dep| sized_formulae.include?(dep.to_formula) } - end) - end - - sized_formulae.uniq(&:to_s) - } - - # Compute the total sizes (download, installed, and net) for the given formulae. - compute_total_sizes = lambda { |sized_formulae, debug: false| - total_download_size = 0 - total_installed_size = 0 - total_net_size = 0 - - sized_formulae.each do |formula| - next unless (bottle = formula.bottle) - - # Fetch additional bottle metadata (if necessary). - bottle.fetch_tab(quiet: !debug) - - total_download_size += bottle.bottle_size.to_i if bottle.bottle_size - total_installed_size += bottle.installed_size.to_i if bottle.installed_size - - # Sum disk usage for all installed kegs of the formula. - next if formula.installed_kegs.none? - - kegs_dep_size = formula.installed_kegs.sum { |keg| keg.disk_usage.to_i } - total_net_size += bottle.installed_size.to_i - kegs_dep_size if bottle.installed_size - end - - { download: total_download_size, - installed: total_installed_size, - net: total_net_size } - } - # Main block: if asking the user is enabled, show dependency and size information. if args.ask? ohai "Looking for bottles..." - sized_formulae = compute_sized_formulae.call(formulae_to_install) - sizes = compute_total_sizes.call(sized_formulae, debug: args.debug?) + sized_formulae = Install.compute_sized_formulae(formulae, args: args) + sizes = Install.compute_total_sizes(sized_formulae, debug: args.debug?) puts "Formulae: #{sized_formulae.join(", ")}\n\n" puts "Download Size: #{disk_usage_readable(sizes[:download])}" puts "Install Size: #{disk_usage_readable(sizes[:installed])}" puts "Net Install Size: #{disk_usage_readable(sizes[:net])}" if sizes[:net] != 0 - ask_input.call + Install.ask_input end Upgrade.upgrade_formulae( diff --git a/Library/Homebrew/install.rb b/Library/Homebrew/install.rb index 80c3c54069..bb2cffdfc7 100644 --- a/Library/Homebrew/install.rb +++ b/Library/Homebrew/install.rb @@ -327,6 +327,82 @@ module Homebrew puts formula_names.join(" ") end + def ask_input + ohai "Do you want to proceed with the installation? [Y/y/yes/N/n]" + accepted_inputs = %w[y yes] + declined_inputs = %w[n no] + loop do + result = $stdin.gets.chomp.strip.downcase + if accepted_inputs.include?(result) + break + elsif declined_inputs.include?(result) + exit 0 + else + puts "Invalid input. Please enter 'Y', 'y', or 'yes' to proceed, or 'N' to abort." + end + end + end + + # Build a unique list of formulae to size by including: + # 1. The original formulae to install. + # 2. Their outdated dependents (subject to pruning criteria). + # 3. Optionally, any installed formula that depends on one of these and is outdated. + def compute_sized_formulae(formulae, args:) + sized_formulae = formulae.flat_map do |formula| + # Always include the formula itself. + formula_list = [formula] + + deps = args.build_from_source? ? formula.deps.build : formula.deps.required + # If there are dependencies, try to gather outdated, bottled ones. + if deps.any? + outdated_dependents = deps.map(&:to_formula).reject(&:pinned?).select do |dep| + dep.installed_kegs.empty? || (dep.bottled? && dep.outdated?) + end + + formula_list.concat(outdated_dependents) + end + + formula_list + end + + # Add any installed formula that depends on one of the sized formulae and is outdated. + unless Homebrew::EnvConfig.no_installed_dependents_check? + sized_formulae.concat(Formula.installed.select do |installed_formula| + installed_formula.outdated? && + installed_formula.deps.required.any? { |dep| sized_formulae.include?(dep.to_formula) } + end) + end + + sized_formulae.uniq(&:to_s).compact + end + + # Compute the total sizes (download, installed, and net) for the given formulae. + def compute_total_sizes(sized_formulae, debug: false) + total_download_size = 0 + total_installed_size = 0 + total_net_size = 0 + + sized_formulae.each do |formula| + next unless (bottle = formula.bottle) + + # Fetch additional bottle metadata (if necessary). + bottle.fetch_tab(quiet: !debug) + + total_download_size += bottle.bottle_size.to_i if bottle.bottle_size + total_installed_size += bottle.installed_size.to_i if bottle.installed_size + + # Sum disk usage for all installed kegs of the formula. + next if formula.installed_kegs.none? + + kegs_dep_size = formula.installed_kegs.sum { |keg| keg.disk_usage.to_i } + total_net_size += bottle.installed_size.to_i - kegs_dep_size if bottle.installed_size + end + + { download: total_download_size, + installed: total_installed_size, + net: total_net_size } + end + private def perform_preinstall_checks(all_fatal: false)