Significantly improve install speeds with smarter postinstall detection

This commit is contained in:
Bo Anderson 2023-06-22 03:06:45 +01:00
parent 1c081f379d
commit 8dc2e80a98
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
5 changed files with 49 additions and 10 deletions

View File

@ -24,8 +24,11 @@ module Homebrew
args.named.to_resolved_formulae.each do |f| args.named.to_resolved_formulae.each do |f|
ohai "Postinstalling #{f}" ohai "Postinstalling #{f}"
f.install_etc_var
if f.post_install_defined?
fi = FormulaInstaller.new(f, **{ debug: args.debug?, quiet: args.quiet?, verbose: args.verbose? }.compact) fi = FormulaInstaller.new(f, **{ debug: args.debug?, quiet: args.quiet?, verbose: args.verbose? }.compact)
fi.post_install fi.post_install
end end
end end
end end
end

View File

@ -1142,6 +1142,23 @@ class Formula
sig { overridable.void } sig { overridable.void }
def post_install; end def post_install; end
# @private
sig { returns(T::Boolean) }
def post_install_defined?
method(:post_install).owner != Formula
end
# @private
sig { void }
def install_etc_var
etc_var_dirs = [bottle_prefix/"etc", bottle_prefix/"var"]
Find.find(*etc_var_dirs.select(&:directory?)) do |path|
path = Pathname.new(path)
path.extend(InstallRenamed)
path.cp_path_sub(bottle_prefix, HOMEBREW_PREFIX)
end
end
# @private # @private
sig { void } sig { void }
def run_post_install def run_post_install
@ -1164,13 +1181,6 @@ class Formula
ENV.clear_sensitive_environment! ENV.clear_sensitive_environment!
ENV.activate_extensions! ENV.activate_extensions!
etc_var_dirs = [bottle_prefix/"etc", bottle_prefix/"var"]
Find.find(*etc_var_dirs.select(&:directory?)) do |path|
path = Pathname.new(path)
path.extend(InstallRenamed)
path.cp_path_sub(bottle_prefix, HOMEBREW_PREFIX)
end
with_logging("post_install") do with_logging("post_install") do
post_install post_install
end end
@ -2174,6 +2184,7 @@ class Formula
"disabled" => disabled?, "disabled" => disabled?,
"disable_date" => disable_date, "disable_date" => disable_date,
"disable_reason" => disable_reason, "disable_reason" => disable_reason,
"post_install_defined" => post_install_defined?,
"service" => (service.serialize if service?), "service" => (service.serialize if service?),
"tap_git_head" => tap_git_head, "tap_git_head" => tap_git_head,
"ruby_source_path" => ruby_source_path, "ruby_source_path" => ruby_source_path,

View File

@ -796,7 +796,8 @@ on_request: installed_on_request?, options: options)
puts "You can run it manually using:" puts "You can run it manually using:"
puts " brew postinstall #{formula.full_name}" puts " brew postinstall #{formula.full_name}"
else else
post_install formula.install_etc_var
post_install if formula.post_install_defined?
end end
keg.prepare_debug_symbols if debug_symbols? keg.prepare_debug_symbols if debug_symbols?

View File

@ -268,6 +268,12 @@ module Formulary
raise "Cannot build from source from abstract formula." raise "Cannot build from source from abstract formula."
end end
@post_install_defined_boolean = json_formula["post_install_defined"]
@post_install_defined_boolean = true if @post_install_defined_boolean.nil? # Backwards compatibility
def post_install_defined?
self.class.instance_variable_get(:@post_install_defined_boolean)
end
if (service_hash = json_formula["service"].presence) if (service_hash = json_formula["service"].presence)
service_hash = Homebrew::Service.deserialize(service_hash) service_hash = Homebrew::Service.deserialize(service_hash)
service do service do

View File

@ -13,6 +13,7 @@ describe Formula do
alias_matcher :have_changed_alias, :be_alias_changed alias_matcher :have_changed_alias, :be_alias_changed
alias_matcher :have_option_defined, :be_option_defined alias_matcher :have_option_defined, :be_option_defined
alias_matcher :have_post_install_defined, :be_post_install_defined
alias_matcher :have_test_defined, :be_test_defined alias_matcher :have_test_defined, :be_test_defined
alias_matcher :pour_bottle, :be_pour_bottle alias_matcher :pour_bottle, :be_pour_bottle
@ -636,6 +637,23 @@ describe Formula do
expect(f.desc).to eq("a formula") expect(f.desc).to eq("a formula")
end end
specify "#post_install_defined?" do
f1 = formula do
url "foo-1.0"
def post_install
# do nothing
end
end
f2 = formula do
url "foo-1.0"
end
expect(f1).to have_post_install_defined
expect(f2).not_to have_post_install_defined
end
specify "test fixtures" do specify "test fixtures" do
f1 = formula do f1 = formula do
url "foo-1.0" url "foo-1.0"