From e3372e2a14dde3adab0423379a9de9b72a47d616 Mon Sep 17 00:00:00 2001
From: Bo Anderson <mail@boanderson.me>
Date: Tue, 18 Aug 2020 00:23:23 +0100
Subject: [PATCH] Lazily require some heavy files

---
 Library/Homebrew/cask/cmd/--cache.rb          |  4 +--
 Library/Homebrew/cask/cmd/audit.rb            |  5 ++--
 Library/Homebrew/cask/cmd/doctor.rb           |  5 ++--
 Library/Homebrew/cask/cmd/fetch.rb            |  5 ++--
 Library/Homebrew/cask/cmd/info.rb             |  3 +-
 Library/Homebrew/cask/cmd/install.rb          |  2 ++
 Library/Homebrew/cask/cmd/internal_stanza.rb  |  2 ++
 Library/Homebrew/cask/cmd/reinstall.rb        |  1 +
 Library/Homebrew/cask/cmd/uninstall.rb        |  2 ++
 Library/Homebrew/cask/cmd/upgrade.rb          |  2 ++
 Library/Homebrew/cli/named_args.rb            |  4 +++
 Library/Homebrew/cli/parser.rb                |  1 -
 Library/Homebrew/dev-cmd/extract.rb           |  1 +
 .../extend/os/linux/extend/ENV/super.rb       |  2 +-
 Library/Homebrew/formulary.rb                 |  3 ++
 Library/Homebrew/global.rb                    |  9 +++---
 Library/Homebrew/tap.rb                       |  2 +-
 Library/Homebrew/test/cask/cmd/audit_spec.rb  |  1 +
 Library/Homebrew/test/dev-cmd/extract_spec.rb | 29 ++++++++++++-------
 .../support/helper/cask/install_helper.rb     |  2 ++
 .../spec/shared_context/homebrew_cask.rb      |  1 +
 21 files changed, 57 insertions(+), 29 deletions(-)

diff --git a/Library/Homebrew/cask/cmd/--cache.rb b/Library/Homebrew/cask/cmd/--cache.rb
index 54aebf6c98..310c79d84e 100644
--- a/Library/Homebrew/cask/cmd/--cache.rb
+++ b/Library/Homebrew/cask/cmd/--cache.rb
@@ -1,7 +1,5 @@
 # frozen_string_literal: true
 
-require "cask/download"
-
 module Cask
   class Cmd
     class Cache < AbstractCommand
@@ -24,6 +22,8 @@ module Cask
       end
 
       def self.cached_location(cask)
+        require "cask/download"
+
         Download.new(cask).downloader.cached_location
       end
     end
diff --git a/Library/Homebrew/cask/cmd/audit.rb b/Library/Homebrew/cask/cmd/audit.rb
index d03858c9a3..ee457e9f76 100644
--- a/Library/Homebrew/cask/cmd/audit.rb
+++ b/Library/Homebrew/cask/cmd/audit.rb
@@ -1,8 +1,5 @@
 # frozen_string_literal: true
 
-require "cli/parser"
-require "cask/auditor"
-
 module Cask
   class Cmd
     class Audit < AbstractCommand
@@ -35,6 +32,8 @@ module Cask
       end
 
       def run
+        require "cask/auditor"
+
         Homebrew.auditing = true
         strict = args.new_cask? || args.strict?
         online = args.new_cask? || args.online?
diff --git a/Library/Homebrew/cask/cmd/doctor.rb b/Library/Homebrew/cask/cmd/doctor.rb
index 9feb0e1119..c052483445 100644
--- a/Library/Homebrew/cask/cmd/doctor.rb
+++ b/Library/Homebrew/cask/cmd/doctor.rb
@@ -1,8 +1,5 @@
 # frozen_string_literal: true
 
-require "system_config"
-require "diagnostic"
-
 module Cask
   class Cmd
     class Doctor < AbstractCommand
@@ -15,6 +12,8 @@ module Cask
       end
 
       def run
+        require "diagnostic"
+
         success = true
 
         checks = Homebrew::Diagnostic::Checks.new(verbose: true)
diff --git a/Library/Homebrew/cask/cmd/fetch.rb b/Library/Homebrew/cask/cmd/fetch.rb
index 4715087a95..141ae73e76 100644
--- a/Library/Homebrew/cask/cmd/fetch.rb
+++ b/Library/Homebrew/cask/cmd/fetch.rb
@@ -1,7 +1,5 @@
 # frozen_string_literal: true
 
-require "cask/download"
-
 module Cask
   class Cmd
     class Fetch < AbstractCommand
@@ -21,6 +19,9 @@ module Cask
       end
 
       def run
+        require "cask/download"
+        require "cask/installer"
+
         options = {
           force:      args.force?,
           quarantine: args.quarantine?,
diff --git a/Library/Homebrew/cask/cmd/info.rb b/Library/Homebrew/cask/cmd/info.rb
index bcb56348cb..a52a40464b 100644
--- a/Library/Homebrew/cask/cmd/info.rb
+++ b/Library/Homebrew/cask/cmd/info.rb
@@ -1,7 +1,6 @@
 # frozen_string_literal: true
 
 require "json"
-require "cask/installer"
 
 module Cask
   class Cmd
@@ -34,6 +33,8 @@ module Cask
       end
 
       def self.get_info(cask)
+        require "cask/installer"
+
         output = +"#{title_info(cask)}\n"
         output << "#{Formatter.url(cask.homepage)}\n" if cask.homepage
         output << installation_info(cask)
diff --git a/Library/Homebrew/cask/cmd/install.rb b/Library/Homebrew/cask/cmd/install.rb
index da74e0958d..55b8f77ee8 100644
--- a/Library/Homebrew/cask/cmd/install.rb
+++ b/Library/Homebrew/cask/cmd/install.rb
@@ -23,6 +23,8 @@ module Cask
       end
 
       def run
+        require "cask/installer"
+
         options = {
           binaries:       args.binaries?,
           verbose:        args.verbose?,
diff --git a/Library/Homebrew/cask/cmd/internal_stanza.rb b/Library/Homebrew/cask/cmd/internal_stanza.rb
index 5db74bd012..41f91ebf5c 100644
--- a/Library/Homebrew/cask/cmd/internal_stanza.rb
+++ b/Library/Homebrew/cask/cmd/internal_stanza.rb
@@ -1,5 +1,7 @@
 # frozen_string_literal: true
 
+require "cask/dsl"
+
 module Cask
   class Cmd
     class InternalStanza < AbstractInternalCommand
diff --git a/Library/Homebrew/cask/cmd/reinstall.rb b/Library/Homebrew/cask/cmd/reinstall.rb
index 75edc85813..2264b246be 100644
--- a/Library/Homebrew/cask/cmd/reinstall.rb
+++ b/Library/Homebrew/cask/cmd/reinstall.rb
@@ -28,6 +28,7 @@ module Cask
         require_sha: nil,
         quarantine: nil
       )
+        require "cask/installer"
 
         options = {
           binaries:       binaries,
diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb
index cde416c65d..1acb9fb109 100644
--- a/Library/Homebrew/cask/cmd/uninstall.rb
+++ b/Library/Homebrew/cask/cmd/uninstall.rb
@@ -29,6 +29,8 @@ module Cask
       end
 
       def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false)
+        require "cask/installer"
+
         options = {
           binaries: binaries,
           force:    force,
diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb
index 6edf182451..2562cdfab0 100644
--- a/Library/Homebrew/cask/cmd/upgrade.rb
+++ b/Library/Homebrew/cask/cmd/upgrade.rb
@@ -100,6 +100,8 @@ module Cask
         old_cask, new_cask,
         binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:
       )
+        require "cask/installer"
+
         odebug "Started upgrade process for Cask #{old_cask}"
         old_config = old_cask.config
 
diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb
index 414595b2d6..2597f4ba51 100644
--- a/Library/Homebrew/cli/named_args.rb
+++ b/Library/Homebrew/cli/named_args.rb
@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
+require "cask/cask_loader"
 require "delegate"
+require "formulary"
 
 module Homebrew
   module CLI
@@ -133,6 +135,8 @@ module Homebrew
       def resolve_keg(name)
         raise UsageError if name.blank?
 
+        require "keg"
+
         rack = Formulary.to_rack(name.downcase)
 
         dirs = rack.directory? ? rack.subdirs : []
diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb
index 1f6ab8a08d..6b86cff43a 100644
--- a/Library/Homebrew/cli/parser.rb
+++ b/Library/Homebrew/cli/parser.rb
@@ -4,7 +4,6 @@ require "env_config"
 require "cli/args"
 require "optparse"
 require "set"
-require "formula"
 require "utils/tty"
 
 COMMAND_DESC_WIDTH = 80
diff --git a/Library/Homebrew/dev-cmd/extract.rb b/Library/Homebrew/dev-cmd/extract.rb
index 9eb432a17c..8ac3d04530 100644
--- a/Library/Homebrew/dev-cmd/extract.rb
+++ b/Library/Homebrew/dev-cmd/extract.rb
@@ -3,6 +3,7 @@
 require "cli/parser"
 require "utils/git"
 require "formulary"
+require "software_spec"
 require "tap"
 
 def with_monkey_patch
diff --git a/Library/Homebrew/extend/os/linux/extend/ENV/super.rb b/Library/Homebrew/extend/os/linux/extend/ENV/super.rb
index 965d7a2ce5..b5ba7e450e 100644
--- a/Library/Homebrew/extend/os/linux/extend/ENV/super.rb
+++ b/Library/Homebrew/extend/os/linux/extend/ENV/super.rb
@@ -17,7 +17,7 @@ module Superenv
   def homebrew_extra_paths
     paths = []
     paths += %w[binutils make].map do |f|
-      bin = Formula[f].opt_bin
+      bin = Formulary.factory(f).opt_bin
       bin if bin.directory?
     rescue FormulaUnavailableError
       nil
diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb
index ce3d098cf9..c9e147a91e 100644
--- a/Library/Homebrew/formulary.rb
+++ b/Library/Homebrew/formulary.rb
@@ -2,6 +2,7 @@
 
 require "digest/md5"
 require "extend/cachable"
+require "tab"
 
 # The Formulary is responsible for creating instances of {Formula}.
 # It is not meant to be used directly from formulae.
@@ -30,6 +31,8 @@ module Formulary
   def self.load_formula(name, path, contents, namespace, flags:)
     raise "Formula loading disabled by HOMEBREW_DISABLE_LOAD_FORMULA!" if Homebrew::EnvConfig.disable_load_formula?
 
+    require "formula"
+
     mod = Module.new
     const_set(namespace, mod)
 
diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb
index 30d0eacb94..f0b8b55059 100644
--- a/Library/Homebrew/global.rb
+++ b/Library/Homebrew/global.rb
@@ -6,6 +6,7 @@ require "json/add/exception"
 require "pathname"
 require "ostruct"
 require "pp"
+require "forwardable"
 
 require_relative "load_path"
 
@@ -38,6 +39,9 @@ require "env_config"
 
 require "config"
 require "os"
+require "context"
+require "extend/pathname"
+require "extend/predicable"
 require "cli/args"
 require "messages"
 
@@ -106,7 +110,6 @@ HOMEBREW_PULL_API_REGEX =
 HOMEBREW_PULL_OR_COMMIT_URL_REGEX =
   %r[https://github\.com/([\w-]+)/([\w-]+)?/(?:pull/(\d+)|commit/[0-9a-fA-F]{4,40})].freeze
 
-require "forwardable"
 require "PATH"
 
 ENV["HOMEBREW_PATH"] ||= ENV["PATH"]
@@ -118,11 +121,7 @@ end.compact.freeze
 
 require "set"
 
-require "context"
-require "extend/pathname"
-
 require "extend/module"
-require "extend/predicable"
 require "extend/string"
 require "active_support/core_ext/object/blank"
 require "active_support/core_ext/hash/deep_merge"
diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb
index 1138cf6149..d83f09891c 100644
--- a/Library/Homebrew/tap.rb
+++ b/Library/Homebrew/tap.rb
@@ -2,7 +2,6 @@
 
 require "commands"
 require "extend/cachable"
-require "readall"
 require "description_cache_store"
 
 # A {Tap} is used to extend the formulae provided by Homebrew core.
@@ -232,6 +231,7 @@ class Tap
   # @param quiet [Boolean] If set, suppress all output.
   def install(full_clone: true, quiet: false, clone_target: nil, force_auto_update: nil)
     require "descriptions"
+    require "readall"
 
     if official? && DEPRECATED_OFFICIAL_TAPS.include?(repo)
       odie "#{name} was deprecated. This tap is now empty and all its contents were either deleted or migrated."
diff --git a/Library/Homebrew/test/cask/cmd/audit_spec.rb b/Library/Homebrew/test/cask/cmd/audit_spec.rb
index 96e295f618..587734b0a6 100644
--- a/Library/Homebrew/test/cask/cmd/audit_spec.rb
+++ b/Library/Homebrew/test/cask/cmd/audit_spec.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 require_relative "shared_examples/invalid_option"
+require "cask/auditor"
 
 describe Cask::Cmd::Audit, :cask do
   let(:cask) { Cask::Cask.new("cask") }
diff --git a/Library/Homebrew/test/dev-cmd/extract_spec.rb b/Library/Homebrew/test/dev-cmd/extract_spec.rb
index a34d6f844e..c12c4eb06a 100644
--- a/Library/Homebrew/test/dev-cmd/extract_spec.rb
+++ b/Library/Homebrew/test/dev-cmd/extract_spec.rb
@@ -27,23 +27,32 @@ describe "brew extract", :integration_test do
   end
 
   it "retrieves the most recent version of formula" do
+    path = target[:path]/"Formula/testball@0.2.rb"
     expect { brew "extract", "testball", target[:name] }
-      .to be_a_success
-    expect(target[:path]/"Formula/testball@0.2.rb").to exist
-    expect(Formulary.factory(target[:path]/"Formula/testball@0.2.rb").version).to be == "0.2"
+      .to output(/^#{path}$/).to_stdout
+      .and not_to_output.to_stderr
+      .and be_a_success
+    expect(path).to exist
+    expect(Formulary.factory(path).version).to be == "0.2"
   end
 
   it "retrieves the specified version of formula" do
+    path = target[:path]/"Formula/testball@0.1.rb"
     expect { brew "extract", "testball", target[:name], "--version=0.1" }
-      .to be_a_success
-    expect(target[:path]/"Formula/testball@0.1.rb").to exist
-    expect(Formulary.factory(target[:path]/"Formula/testball@0.1.rb").version).to be == "0.1"
+      .to output(/^#{path}$/).to_stdout
+      .and not_to_output.to_stderr
+      .and be_a_success
+    expect(path).to exist
+    expect(Formulary.factory(path).version).to be == "0.1"
   end
 
   it "retrieves the compatible version of formula" do
-    expect { brew "extract", "testball", target[:name], "--version=0", "--debug" }
-      .to be_a_success
-    expect(target[:path]/"Formula/testball@0.rb").to exist
-    expect(Formulary.factory(target[:path]/"Formula/testball@0.rb").version).to be == "0.2"
+    path = target[:path]/"Formula/testball@0.rb"
+    expect { brew "extract", "testball", target[:name], "--version=0" }
+      .to output(/^#{path}$/).to_stdout
+      .and not_to_output.to_stderr
+      .and be_a_success
+    expect(path).to exist
+    expect(Formulary.factory(path).version).to be == "0.2"
   end
 end
diff --git a/Library/Homebrew/test/support/helper/cask/install_helper.rb b/Library/Homebrew/test/support/helper/cask/install_helper.rb
index 25ba7ea17a..19ef9594e0 100644
--- a/Library/Homebrew/test/support/helper/cask/install_helper.rb
+++ b/Library/Homebrew/test/support/helper/cask/install_helper.rb
@@ -1,5 +1,7 @@
 # frozen_string_literal: true
 
+require "cask/installer"
+
 module InstallHelper
   module_function
 
diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/homebrew_cask.rb b/Library/Homebrew/test/support/helper/spec/shared_context/homebrew_cask.rb
index 80328827a9..87c9517b6c 100644
--- a/Library/Homebrew/test/support/helper/spec/shared_context/homebrew_cask.rb
+++ b/Library/Homebrew/test/support/helper/spec/shared_context/homebrew_cask.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 require "cask/config"
+require "cask/cache"
 
 require "test/support/helper/cask/fake_system_command"
 require "test/support/helper/cask/install_helper"
-- 
GitLab