diff --git a/Library/Homebrew/cmd/postinstall.rb b/Library/Homebrew/cmd/postinstall.rb index 93ed03f059..967bba4695 100644 --- a/Library/Homebrew/cmd/postinstall.rb +++ b/Library/Homebrew/cmd/postinstall.rb @@ -24,8 +24,11 @@ module Homebrew args.named.to_resolved_formulae.each do |f| ohai "Postinstalling #{f}" - fi = FormulaInstaller.new(f, **{ debug: args.debug?, quiet: args.quiet?, verbose: args.verbose? }.compact) - fi.post_install + f.install_etc_var + if f.post_install_defined? + fi = FormulaInstaller.new(f, **{ debug: args.debug?, quiet: args.quiet?, verbose: args.verbose? }.compact) + fi.post_install + end end end end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 2e3ee7e9d2..d85768060b 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1142,6 +1142,23 @@ class Formula sig { overridable.void } 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 sig { void } def run_post_install @@ -1164,13 +1181,6 @@ class Formula ENV.clear_sensitive_environment! 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 post_install end @@ -2174,6 +2184,7 @@ class Formula "disabled" => disabled?, "disable_date" => disable_date, "disable_reason" => disable_reason, + "post_install_defined" => post_install_defined?, "service" => (service.serialize if service?), "tap_git_head" => tap_git_head, "ruby_source_path" => ruby_source_path, diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index d3d2833fb8..e22fac868e 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -796,7 +796,8 @@ on_request: installed_on_request?, options: options) puts "You can run it manually using:" puts " brew postinstall #{formula.full_name}" else - post_install + formula.install_etc_var + post_install if formula.post_install_defined? end keg.prepare_debug_symbols if debug_symbols? diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 1f072fe56e..20a53bd0c7 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -268,6 +268,12 @@ module Formulary raise "Cannot build from source from abstract formula." 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) service_hash = Homebrew::Service.deserialize(service_hash) service do diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 82f525bd84..95c39ac23e 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -13,6 +13,7 @@ describe Formula do alias_matcher :have_changed_alias, :be_alias_changed 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 :pour_bottle, :be_pour_bottle @@ -636,6 +637,23 @@ describe Formula do expect(f.desc).to eq("a formula") 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 f1 = formula do url "foo-1.0"