From b538ce73616d3a1e91aee291fca3fafc352bfe95 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 23 Jun 2022 17:18:58 -0400 Subject: [PATCH 01/10] Add `OnSystem` module to generate `on_*` methods for formulae and casks --- Library/Homebrew/cask/dsl.rb | 20 ++++- Library/Homebrew/extend/on_os.rb | 26 ------ Library/Homebrew/extend/on_os.rbi | 5 -- Library/Homebrew/extend/on_system.rb | 105 ++++++++++++++++++++++ Library/Homebrew/extend/os/linux/on_os.rb | 10 --- Library/Homebrew/extend/os/mac/on_os.rb | 10 --- Library/Homebrew/extend/os/on_os.rb | 8 -- Library/Homebrew/formula.rb | 8 +- Library/Homebrew/formula.rbi | 3 + Library/Homebrew/override.rb | 53 +++++++++++ Library/Homebrew/resource.rb | 5 +- Library/Homebrew/software_spec.rb | 7 +- 12 files changed, 191 insertions(+), 69 deletions(-) delete mode 100644 Library/Homebrew/extend/on_os.rb delete mode 100644 Library/Homebrew/extend/on_os.rbi create mode 100644 Library/Homebrew/extend/on_system.rb delete mode 100644 Library/Homebrew/extend/os/linux/on_os.rb delete mode 100644 Library/Homebrew/extend/os/mac/on_os.rb delete mode 100644 Library/Homebrew/extend/os/on_os.rb create mode 100644 Library/Homebrew/override.rb diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index e4d1ac47a2..9a631484d1 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -4,6 +4,7 @@ require "locale" require "lazy_object" require "livecheck" +require "override" require "cask/artifact" @@ -25,6 +26,8 @@ require "cask/dsl/version" require "cask/url" require "cask/utils" +require "extend/on_system" + module Cask # Class representing the domain-specific language used for casks. # @@ -89,8 +92,12 @@ module Cask *ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] }, ]).freeze + extend Predicable + attr_reader :cask, :token + attr_predicate :on_system_blocks_exist? + def initialize(cask) @cask = cask @token = cask.token @@ -112,10 +119,17 @@ module Cask def set_unique_stanza(stanza, should_return) return instance_variable_get("@#{stanza}") if should_return - if !@cask.allow_reassignment && instance_variable_defined?("@#{stanza}") - raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.") + unless @cask.allow_reassignment + if instance_variable_defined?("@#{stanza}") && !@called_in_on_system_block + raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.") + end + + if instance_variable_defined?("@#{stanza}_set_in_block") && @called_in_on_system_block + raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only be overridden once.") + end end + instance_variable_set("@#{stanza}_set_in_block", true) if @called_in_on_system_block instance_variable_set("@#{stanza}", yield) rescue CaskInvalidError raise @@ -306,6 +320,8 @@ module Cask @livecheckable == true end + OnSystem.setup_methods! onto: self, include_linux: false + ORDINARY_ARTIFACT_CLASSES.each do |klass| define_method(klass.dsl_key) do |*args| if [*artifacts.map(&:class), klass].include?(Artifact::StageOnly) && diff --git a/Library/Homebrew/extend/on_os.rb b/Library/Homebrew/extend/on_os.rb deleted file mode 100644 index aacadd608e..0000000000 --- a/Library/Homebrew/extend/on_os.rb +++ /dev/null @@ -1,26 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - extend T::Sig - - # Block only executed on macOS. No-op on Linux. - #
on_macos do
-  # # Do something Mac-specific
-  # end
- sig { params(block: T.proc.void).void } - def on_macos(&block) - raise "No block content defined for 'on_macos' block" unless T.unsafe(block) - end - - # Block only executed on Linux. No-op on macOS. - #
on_linux do
-  # # Do something Linux-specific
-  # end
- sig { params(block: T.proc.void).void } - def on_linux(&block) - raise "No block content defined for 'on_linux' block" unless T.unsafe(block) - end -end - -require "extend/os/on_os" diff --git a/Library/Homebrew/extend/on_os.rbi b/Library/Homebrew/extend/on_os.rbi deleted file mode 100644 index a53474fd86..0000000000 --- a/Library/Homebrew/extend/on_os.rbi +++ /dev/null @@ -1,5 +0,0 @@ -# typed: strict - -module OnOS - include Kernel -end diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb new file mode 100644 index 0000000000..711bb4b187 --- /dev/null +++ b/Library/Homebrew/extend/on_system.rb @@ -0,0 +1,105 @@ +# typed: false +# frozen_string_literal: true + +require "override" + +module OnSystem + extend T::Sig + + ARCH_OPTIONS = [:intel, :arm].freeze + BASE_OS_OPTIONS = [:macos, :linux].freeze + + module_function + + sig { params(arch: Symbol).returns(T::Boolean) } + def arch_condition_met?(arch) + raise ArgumentError, "Invalid arch condition: #{arch.inspect}" unless ARCH_OPTIONS.include?(arch) + + current_arch = Homebrew::Override.arch || Hardware::CPU.type + arch == current_arch + end + + sig { params(os_name: Symbol, or_condition: T.nilable(Symbol)).returns(T::Boolean) } + def os_condition_met?(os_name, or_condition = nil) + if Homebrew::EnvConfig.simulate_macos_on_linux? + return false if os_name == :linux + return true if [:macos, *MacOS::Version::SYMBOLS.keys].include?(os_name) + end + + if BASE_OS_OPTIONS.include?(os_name) + if Homebrew::Override.none? + return OS.linux? if os_name == :linux + return OS.mac? if os_name == :macos + end + + return Homebrew::Override.send("#{os_name}?") + end + + raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOS::Version::SYMBOLS.key?(os_name) + + if or_condition.present? && [:or_newer, :or_older].exclude?(or_condition) + raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}" + end + + base_os = MacOS::Version.from_symbol(os_name) + current_os = MacOS::Version.from_symbol(Homebrew::Override.os || MacOS.version.to_sym) + + return current_os >= base_os if or_condition == :or_newer + return current_os <= base_os if or_condition == :or_older + + current_os == base_os + end + + sig { params(method_name: Symbol).returns(Symbol) } + def condition_from_method_name(method_name) + method_name.to_s.sub(/^on_/, "").to_sym + end + + sig { params(onto: Class, include_linux: T::Boolean).void } + def setup_methods!(onto:, include_linux: true) + ARCH_OPTIONS.each do |arch| + onto.define_method("on_#{arch}") do |&block| + @on_system_blocks_exist = true + + return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(__method__) + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + + if include_linux + BASE_OS_OPTIONS.each do |base_os| + onto.define_method("on_#{base_os}") do |&block| + @on_system_blocks_exist = true + + return unless OnSystem.os_condition_met? OnSystem.condition_from_method_name(__method__) + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + end + + MacOS::Version::SYMBOLS.each_key do |os_name| + onto.define_method("on_#{os_name}") do |or_condition = nil, &block| + @on_system_blocks_exist = true + + os_condition = OnSystem.condition_from_method_name __method__ + return unless OnSystem.os_condition_met? os_condition, or_condition + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + end +end diff --git a/Library/Homebrew/extend/os/linux/on_os.rb b/Library/Homebrew/extend/os/linux/on_os.rb deleted file mode 100644 index 4373a45ab2..0000000000 --- a/Library/Homebrew/extend/os/linux/on_os.rb +++ /dev/null @@ -1,10 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - def on_linux(&block) - raise "No block content defined for 'on_linux' block" unless T.unsafe(block) - - yield - end -end diff --git a/Library/Homebrew/extend/os/mac/on_os.rb b/Library/Homebrew/extend/os/mac/on_os.rb deleted file mode 100644 index 78759c1a9d..0000000000 --- a/Library/Homebrew/extend/os/mac/on_os.rb +++ /dev/null @@ -1,10 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - def on_macos(&block) - raise "No block content defined for 'on_macos' block" unless T.unsafe(block) - - yield - end -end diff --git a/Library/Homebrew/extend/os/on_os.rb b/Library/Homebrew/extend/os/on_os.rb deleted file mode 100644 index c74ea9d459..0000000000 --- a/Library/Homebrew/extend/os/on_os.rb +++ /dev/null @@ -1,8 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -if OS.mac? || Homebrew::EnvConfig.simulate_macos_on_linux? - require "extend/os/mac/on_os" -elsif OS.linux? - require "extend/os/linux/on_os" -end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 25ff2c3920..250583b465 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -28,7 +28,7 @@ require "tab" require "mktemp" require "find" require "utils/spdx" -require "extend/on_os" +require "extend/on_system" require "api" # A formula provides instructions and metadata for Homebrew to install a piece @@ -64,11 +64,12 @@ class Formula include Utils::Shebang include Utils::Shell include Context - include OnOS extend Forwardable extend Cachable extend Predicable + OnSystem.setup_methods! onto: self + # @!method inreplace(paths, before = nil, after = nil) # @see Utils::Inreplace.inreplace @@ -2470,7 +2471,6 @@ class Formula # The methods below define the formula DSL. class << self include BuildEnvironment::DSL - include OnOS def method_added(method) super @@ -2483,6 +2483,8 @@ class Formula end end + OnSystem.setup_methods! onto: self + # The reason for why this software is not linked (by default) to # {::HOMEBREW_PREFIX}. # @private diff --git a/Library/Homebrew/formula.rbi b/Library/Homebrew/formula.rbi index c573f64b3c..a47b034dee 100644 --- a/Library/Homebrew/formula.rbi +++ b/Library/Homebrew/formula.rbi @@ -48,4 +48,7 @@ class Formula def env; end def conflicts; end + + # This method is generated by `OnSystem::setup_methods!` + def self.on_macos(&block); end end diff --git a/Library/Homebrew/override.rb b/Library/Homebrew/override.rb new file mode 100644 index 0000000000..7b6765ac5d --- /dev/null +++ b/Library/Homebrew/override.rb @@ -0,0 +1,53 @@ +# typed: true +# frozen_string_literal: true + +module Homebrew + # Helper module for overriding machine-specific methods. + # + # @api private + class Override + class << self + extend T::Sig + + ARCH_OPTIONS = [:intel, :arm64].freeze + OS_OPTIONS = [:macos, :linux].freeze + + attr_reader :os, :arch + + sig { params(new_os: Symbol).void } + def os=(new_os) + os_options = [:macos, :linux, *MacOS::Version::SYMBOLS.keys] + raise "Unknown OS: #{new_os}" unless os_options.include?(new_os) + + @os = new_os + end + + sig { params(new_arch: Symbol).void } + def arch=(new_arch) + raise "New arch must be :arm or :intel" unless [:arm, :intel].include?(new_arch) + + @arch = new_arch + end + + sig { void } + def clear + @os = @arch = nil + end + + sig { returns(T::Boolean) } + def none? + @os.nil? && @arch.nil? + end + + sig { returns(T::Boolean) } + def macos? + [:macos, *MacOS::Version::SYMBOLS.keys].include?(@os) + end + + sig { returns(T::Boolean) } + def linux? + @os == :linux + end + end + end +end diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 49d9e362a8..9cbd64bc7f 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -5,7 +5,7 @@ require "download_strategy" require "checksum" require "version" require "mktemp" -require "extend/on_os" +require "extend/on_system" # Resource is the fundamental representation of an external resource. The # primary formula download, along with other declared resources, are instances @@ -17,7 +17,8 @@ class Resource include Context include FileUtils - include OnOS + + OnSystem.setup_methods! onto: self attr_reader :mirrors, :specs, :using, :source_modified_time, :patches, :owner attr_writer :version diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 437af9f0a8..61a0b8d463 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -12,19 +12,20 @@ require "utils/bottles" require "patch" require "compilers" require "os/mac/version" -require "extend/on_os" +require "extend/on_system" class SoftwareSpec extend T::Sig extend Forwardable - include OnOS PREDEFINED_OPTIONS = { universal: Option.new("universal", "Build a universal binary"), cxx11: Option.new("c++11", "Build using C++11 mode"), }.freeze + OnSystem.setup_methods! onto: self + attr_reader :name, :full_name, :owner, :build, :resources, :patches, :options, :deprecated_flags, :deprecated_options, :dependency_collector, :bottle_specification, :compiler_failures, :uses_from_macos_elements @@ -583,7 +584,7 @@ class BottleSpecification end class PourBottleCheck - include OnOS + OnSystem.setup_methods! onto: self def initialize(formula) @formula = formula From d3c3f7be9efbb697f07587c1b675948da67c13d7 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 23 Jun 2022 17:19:27 -0400 Subject: [PATCH 02/10] Add `variations` to `Cask#to_h` --- Library/Homebrew/cask/cask.rb | 43 ++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 89a7c19ad9..40218642e5 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -6,6 +6,7 @@ require "cask/config" require "cask/dsl" require "cask/metadata" require "searchable" +require "utils/bottles" module Cask # An instance of a cask. @@ -58,6 +59,10 @@ module Cask def config=(config) @config = config + refresh + end + + def refresh @dsl = DSL.new(self) return unless @block @@ -214,7 +219,7 @@ module Cask end alias == eql? - def to_h + def to_hash { "token" => token, "full_token" => full_name, @@ -238,6 +243,42 @@ module Cask } end + def to_h + hash = to_hash + variations = {} + + hash_keys_to_skip = %w[outdated installed versions] + + if @dsl.on_system_blocks_exist? + [:arm, :intel].each do |arch| + MacOS::Version::SYMBOLS.each_key do |os_name| + # Big Sur is the first version of macOS that supports arm + next if arch == :arm && MacOS::Version.from_symbol(os_name) < MacOS::Version.from_symbol(:big_sur) + + Homebrew::Override.os = os_name + Homebrew::Override.arch = arch + + refresh + + bottle_tag = ::Utils::Bottles::Tag.new(system: os_name, arch: arch).to_sym + to_hash.each do |key, value| + next if hash_keys_to_skip.include? key + next if value.to_s == hash[key].to_s + + variations[bottle_tag] ||= {} + variations[bottle_tag][key] = value + end + end + end + end + + Homebrew::Override.clear + refresh + + hash["variations"] = variations + hash + end + private def to_h_string_gsubs(string) From 6ebd5174d6cb38035280b661b05aaaeec2bc9ca9 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 23 Jun 2022 17:19:47 -0400 Subject: [PATCH 03/10] Update tests for `on_*` methods in formulae and casks --- Library/Homebrew/test/cask/cmd/list_spec.rb | 59 +++++++-- Library/Homebrew/test/formula_spec.rb | 113 ++++++++++++++++++ .../fixtures/cask/Casks/multiple-versions.rb | 23 +++- 3 files changed, 180 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/test/cask/cmd/list_spec.rb b/Library/Homebrew/test/cask/cmd/list_spec.rb index 81f7a83c8e..8149e76210 100644 --- a/Library/Homebrew/test/cask/cmd/list_spec.rb +++ b/Library/Homebrew/test/cask/cmd/list_spec.rb @@ -120,7 +120,9 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } }, { "token": "local-transmission", @@ -149,7 +151,9 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } }, { "token": "multiple-versions", @@ -160,11 +164,13 @@ describe Cask::Cmd::List, :cask do ], "desc": null, "homepage": "https://brew.sh/", - "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip", + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.3/arm.zip", "appcast": null, "version": "1.2.3", "versions": { - "test_os": "1.2.0" + "big_sur": "1.2.0", + "catalina": "1.0.0", + "mojave": "1.0.0" }, "installed": "1.2.3", "outdated": false, @@ -179,7 +185,32 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + "arm_big_sur": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.0/arm.zip", + "version": "1.2.0", + "sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + }, + "intel_monterey": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.3/intel.zip" + }, + "intel_big_sur": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.0/intel.zip", + "version": "1.2.0", + "sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + }, + "intel_catalina": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.0.0/intel.zip", + "version": "1.0.0", + "sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + }, + "intel_mojave": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.0.0/intel.zip", + "version": "1.0.0", + "sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + } + } }, { "token": "third-party-cask", @@ -208,7 +239,9 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } } ] EOS @@ -217,10 +250,18 @@ describe Cask::Cmd::List, :cask do before do casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) - # Add a test OS to ensure that all cask versions are listed regardless of OS. - symbols = MacOS::Version::SYMBOLS.dup - symbols[:test_os] = "10.9" + # Use a more limited symbols list to shorten the variations hash + symbols = { + monterey: "12", + big_sur: "11", + catalina: "10.15", + mojave: "10.14", + } stub_const("MacOS::Version::SYMBOLS", symbols) + + # For consistency, always run on Monterey and ARM + MacOS.full_version = "12" + allow(Hardware::CPU).to receive(:type).and_return(:arm) end it "of all installed Casks" do diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 8c5ee1b2f5..58d70fcc5c 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -1580,4 +1580,117 @@ describe Formula do expect(f.test).to eq(2) end end + + describe "on_{os_version} blocks", :needs_macos do + before do + Homebrew::Override.os = :monterey + end + + after do + Homebrew::Override.clear + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_monterey :or_newer do + @test = 1 + end + on_big_sur do + @test = 2 + end + on_catalina :or_older do + @test = 3 + end + end + end.new + end + + it "only calls code within `on_monterey`" do + Homebrew::Override.os = :monterey + f.brew { f.install } + expect(f.test).to eq(1) + end + + it "only calls code within `on_monterey :or_newer`" do + Homebrew::Override.os = :ventura + f.brew { f.install } + expect(f.test).to eq(1) + end + + it "only calls code within `on_big_sur`" do + Homebrew::Override.os = :big_sur + f.brew { f.install } + expect(f.test).to eq(2) + end + + it "only calls code within `on_catalina`" do + Homebrew::Override.os = :catalina + f.brew { f.install } + expect(f.test).to eq(3) + end + + it "only calls code within `on_catalina :or_older`" do + Homebrew::Override.os = :mojave + f.brew { f.install } + expect(f.test).to eq(3) + end + end + + describe "#on_arm" do + before do + allow(Hardware::CPU).to receive(:type).and_return(:arm) + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_arm do + @test = 1 + end + on_intel do + @test = 2 + end + end + end.new + end + + it "only calls code within on_arm" do + f.brew { f.install } + expect(f.test).to eq(1) + end + end + + describe "#on_intel" do + before do + allow(Hardware::CPU).to receive(:type).and_return(:intel) + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_arm do + @test = 1 + end + on_intel do + @test = 2 + end + end + end.new + end + + it "only calls code within on_intel" do + f.brew { f.install } + expect(f.test).to eq(2) + end + end end diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb index 0107d0ec8a..ad05491a2d 100644 --- a/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb @@ -1,12 +1,23 @@ cask "multiple-versions" do - if MacOS.version == :test_os - version "1.2.0" - else - version "1.2.3" - end + arch = "arm" + version "1.2.3" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" - url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip" + on_intel do + arch = "intel" + end + + on_big_sur do + version "1.2.0" + sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + end + + on_catalina :or_older do + version "1.0.0" + sha256 "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + end + + url "file://#{TEST_FIXTURE_DIR}/cask/caffeine/#{version}/#{arch}.zip" homepage "https://brew.sh/" app "Caffeine.app" From f38db6e10046be7eb7ca3f334cd3f3b3e31f3345 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 23 Jun 2022 17:49:08 -0400 Subject: [PATCH 04/10] Define `MacOS::Version::SYMBOLS` on macOS and linux --- Library/Homebrew/os.rb | 21 +++++++++++++++++++++ Library/Homebrew/os/linux.rb | 6 ------ Library/Homebrew/os/mac.rb | 6 ------ Library/Homebrew/os/mac/version.rb | 18 +++--------------- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Library/Homebrew/os.rb b/Library/Homebrew/os.rb index 52945a402a..b2d6c64cad 100644 --- a/Library/Homebrew/os.rb +++ b/Library/Homebrew/os.rb @@ -1,6 +1,8 @@ # typed: true # frozen_string_literal: true +require "version" + # Helper functions for querying operating system information. # # @api private @@ -48,6 +50,25 @@ module OS CI_GLIBC_VERSION = "2.23" CI_OS_VERSION = "Ubuntu 16.04" + module Mac + ::MacOS = OS::Mac + + class Version < ::Version + # TODO: when removing symbols here, ensure that they are added to + # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. + SYMBOLS = { + ventura: "13", + monterey: "12", + big_sur: "11", + catalina: "10.15", + mojave: "10.14", + high_sierra: "10.13", + sierra: "10.12", + el_capitan: "10.11", + }.freeze + end + end + if OS.mac? require "os/mac" # Don't tell people to report issues on unsupported configurations. diff --git a/Library/Homebrew/os/linux.rb b/Library/Homebrew/os/linux.rb index 6c029ef8be..57598261ef 100644 --- a/Library/Homebrew/os/linux.rb +++ b/Library/Homebrew/os/linux.rb @@ -31,12 +31,6 @@ module OS module Mac module_function - # rubocop:disable Naming/ConstantName - # rubocop:disable Style/MutableConstant - ::MacOS = OS::Mac - # rubocop:enable Naming/ConstantName - # rubocop:enable Style/MutableConstant - raise "Loaded OS::Linux on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] def version diff --git a/Library/Homebrew/os/mac.rb b/Library/Homebrew/os/mac.rb index bb75f0de9c..b23e62dd8b 100644 --- a/Library/Homebrew/os/mac.rb +++ b/Library/Homebrew/os/mac.rb @@ -13,12 +13,6 @@ module OS module_function - # rubocop:disable Naming/ConstantName - # rubocop:disable Style/MutableConstant - ::MacOS = OS::Mac - # rubocop:enable Naming/ConstantName - # rubocop:enable Style/MutableConstant - raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] VERSION = ENV.fetch("HOMEBREW_MACOS_VERSION").chomp.freeze diff --git a/Library/Homebrew/os/mac/version.rb b/Library/Homebrew/os/mac/version.rb index 130d1c4f63..a7d183eaef 100644 --- a/Library/Homebrew/os/mac/version.rb +++ b/Library/Homebrew/os/mac/version.rb @@ -13,22 +13,10 @@ module OS class Version < ::Version extend T::Sig - # TODO: when removing symbols here, ensure that they are added to - # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. - SYMBOLS = { - ventura: "13", - monterey: "12", - big_sur: "11", - catalina: "10.15", - mojave: "10.14", - high_sierra: "10.13", - sierra: "10.12", - el_capitan: "10.11", - }.freeze - # TODO: bump version when new macOS is released or announced - # and also update references in docs/Installation.md and - # https://github.com/Homebrew/install/blob/HEAD/install.sh + # and also update references in docs/Installation.md, + # https://github.com/Homebrew/install/blob/HEAD/install.sh and + # OS::Mac::Version (in HOMEBREW_LIBRARY/os.rb) NEWEST_UNSUPPORTED = "13" private_constant :NEWEST_UNSUPPORTED From d96a8fd35e55269015ef8161d2021ed6c9f07496 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Fri, 24 Jun 2022 17:20:03 -0400 Subject: [PATCH 05/10] Fix tests --- Library/Homebrew/test/cask/cmd/list_spec.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/cask/cmd/list_spec.rb b/Library/Homebrew/test/cask/cmd/list_spec.rb index 8149e76210..6c0e3db2ff 100644 --- a/Library/Homebrew/test/cask/cmd/list_spec.rb +++ b/Library/Homebrew/test/cask/cmd/list_spec.rb @@ -246,10 +246,9 @@ describe Cask::Cmd::List, :cask do ] EOS } + let(:original_macos_version) { MacOS.full_version.to_s } before do - casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) - # Use a more limited symbols list to shorten the variations hash symbols = { monterey: "12", @@ -262,6 +261,12 @@ describe Cask::Cmd::List, :cask do # For consistency, always run on Monterey and ARM MacOS.full_version = "12" allow(Hardware::CPU).to receive(:type).and_return(:arm) + + casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) + end + + after do + MacOS.full_version = original_macos_version end it "of all installed Casks" do From 5ec19adf58b18d0736be1b793164632fdf3f9b00 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Tue, 28 Jun 2022 15:04:30 -0400 Subject: [PATCH 06/10] Rename `Override` module to `SimulateSystem` --- Library/Homebrew/cask/cask.rb | 6 +++--- Library/Homebrew/cask/dsl.rb | 1 - Library/Homebrew/extend/on_system.rb | 12 ++++++------ .../Homebrew/{override.rb => simulate_system.rb} | 7 ++----- Library/Homebrew/test/formula_spec.rb | 14 +++++++------- 5 files changed, 18 insertions(+), 22 deletions(-) rename Library/Homebrew/{override.rb => simulate_system.rb} (85%) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 40218642e5..90f5e756c0 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -255,8 +255,8 @@ module Cask # Big Sur is the first version of macOS that supports arm next if arch == :arm && MacOS::Version.from_symbol(os_name) < MacOS::Version.from_symbol(:big_sur) - Homebrew::Override.os = os_name - Homebrew::Override.arch = arch + Homebrew::SimulateSystem.os = os_name + Homebrew::SimulateSystem.arch = arch refresh @@ -272,7 +272,7 @@ module Cask end end - Homebrew::Override.clear + Homebrew::SimulateSystem.clear refresh hash["variations"] = variations diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index 9a631484d1..1748d5fce0 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -4,7 +4,6 @@ require "locale" require "lazy_object" require "livecheck" -require "override" require "cask/artifact" diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index 711bb4b187..5490149e68 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -1,7 +1,7 @@ # typed: false # frozen_string_literal: true -require "override" +require "simulate_system" module OnSystem extend T::Sig @@ -13,9 +13,9 @@ module OnSystem sig { params(arch: Symbol).returns(T::Boolean) } def arch_condition_met?(arch) - raise ArgumentError, "Invalid arch condition: #{arch.inspect}" unless ARCH_OPTIONS.include?(arch) + raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(arch) - current_arch = Homebrew::Override.arch || Hardware::CPU.type + current_arch = Homebrew::SimulateSystem.arch || Hardware::CPU.type arch == current_arch end @@ -27,12 +27,12 @@ module OnSystem end if BASE_OS_OPTIONS.include?(os_name) - if Homebrew::Override.none? + if Homebrew::SimulateSystem.none? return OS.linux? if os_name == :linux return OS.mac? if os_name == :macos end - return Homebrew::Override.send("#{os_name}?") + return Homebrew::SimulateSystem.send("#{os_name}?") end raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOS::Version::SYMBOLS.key?(os_name) @@ -42,7 +42,7 @@ module OnSystem end base_os = MacOS::Version.from_symbol(os_name) - current_os = MacOS::Version.from_symbol(Homebrew::Override.os || MacOS.version.to_sym) + current_os = MacOS::Version.from_symbol(Homebrew::SimulateSystem.os || MacOS.version.to_sym) return current_os >= base_os if or_condition == :or_newer return current_os <= base_os if or_condition == :or_older diff --git a/Library/Homebrew/override.rb b/Library/Homebrew/simulate_system.rb similarity index 85% rename from Library/Homebrew/override.rb rename to Library/Homebrew/simulate_system.rb index 7b6765ac5d..12d1e0e34d 100644 --- a/Library/Homebrew/override.rb +++ b/Library/Homebrew/simulate_system.rb @@ -2,16 +2,13 @@ # frozen_string_literal: true module Homebrew - # Helper module for overriding machine-specific methods. + # Helper module for simulating different system condfigurations. # # @api private - class Override + class SimulateSystem class << self extend T::Sig - ARCH_OPTIONS = [:intel, :arm64].freeze - OS_OPTIONS = [:macos, :linux].freeze - attr_reader :os, :arch sig { params(new_os: Symbol).void } diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 58d70fcc5c..f9e3f93116 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -1583,11 +1583,11 @@ describe Formula do describe "on_{os_version} blocks", :needs_macos do before do - Homebrew::Override.os = :monterey + Homebrew::SimulateSystem.os = :monterey end after do - Homebrew::Override.clear + Homebrew::SimulateSystem.clear end let(:f) do @@ -1610,31 +1610,31 @@ describe Formula do end it "only calls code within `on_monterey`" do - Homebrew::Override.os = :monterey + Homebrew::SimulateSystem.os = :monterey f.brew { f.install } expect(f.test).to eq(1) end it "only calls code within `on_monterey :or_newer`" do - Homebrew::Override.os = :ventura + Homebrew::SimulateSystem.os = :ventura f.brew { f.install } expect(f.test).to eq(1) end it "only calls code within `on_big_sur`" do - Homebrew::Override.os = :big_sur + Homebrew::SimulateSystem.os = :big_sur f.brew { f.install } expect(f.test).to eq(2) end it "only calls code within `on_catalina`" do - Homebrew::Override.os = :catalina + Homebrew::SimulateSystem.os = :catalina f.brew { f.install } expect(f.test).to eq(3) end it "only calls code within `on_catalina :or_older`" do - Homebrew::Override.os = :mojave + Homebrew::SimulateSystem.os = :mojave f.brew { f.install } expect(f.test).to eq(3) end From ac067eedb2e64e024e77e126b652588a4a5dd58b Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Wed, 29 Jun 2022 11:29:46 -0400 Subject: [PATCH 07/10] Create `MacOSVersions` module --- Library/Homebrew/cask/cask.rb | 4 ++-- Library/Homebrew/cask/dsl/depends_on.rb | 2 +- Library/Homebrew/extend/on_system.rb | 6 +++--- Library/Homebrew/global.rb | 1 + Library/Homebrew/macos_versions.rb | 20 ++++++++++++++++++++ Library/Homebrew/os.rb | 19 ------------------- Library/Homebrew/os/linux.rb | 2 ++ Library/Homebrew/os/mac.rb | 2 ++ Library/Homebrew/os/mac/version.rb | 8 ++++---- Library/Homebrew/simulate_system.rb | 4 ++-- Library/Homebrew/test/cask/cmd/list_spec.rb | 2 +- 11 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 Library/Homebrew/macos_versions.rb diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 90f5e756c0..c30dfa7c22 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -94,7 +94,7 @@ module Cask version_os_hash = {} actual_version = MacOS.full_version.to_s - MacOS::Version::SYMBOLS.each do |os_name, os_version| + MacOSVersions::SYMBOLS.each do |os_name, os_version| MacOS.full_version = os_version cask = CaskLoader.load(token) version_os_hash[os_name] = cask.version if cask.version != version @@ -251,7 +251,7 @@ module Cask if @dsl.on_system_blocks_exist? [:arm, :intel].each do |arch| - MacOS::Version::SYMBOLS.each_key do |os_name| + MacOSVersions::SYMBOLS.each_key do |os_name| # Big Sur is the first version of macOS that supports arm next if arch == :arm && MacOS::Version.from_symbol(os_name) < MacOS::Version.from_symbol(:big_sur) diff --git a/Library/Homebrew/cask/dsl/depends_on.rb b/Library/Homebrew/cask/dsl/depends_on.rb index d7ac942dfd..1d4e3a2448 100644 --- a/Library/Homebrew/cask/dsl/depends_on.rb +++ b/Library/Homebrew/cask/dsl/depends_on.rb @@ -55,7 +55,7 @@ module Cask begin @macos = if args.count > 1 MacOSRequirement.new([args], comparator: "==") - elsif MacOS::Version::SYMBOLS.key?(args.first) + elsif MacOSVersions::SYMBOLS.key?(args.first) MacOSRequirement.new([args.first], comparator: "==") elsif /^\s*(?<|>|[=<>]=)\s*:(?\S+)\s*$/ =~ args.first MacOSRequirement.new([version.to_sym], comparator: comparator) diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index 5490149e68..e008a6feca 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -23,7 +23,7 @@ module OnSystem def os_condition_met?(os_name, or_condition = nil) if Homebrew::EnvConfig.simulate_macos_on_linux? return false if os_name == :linux - return true if [:macos, *MacOS::Version::SYMBOLS.keys].include?(os_name) + return true if [:macos, *MacOSVersions::SYMBOLS.keys].include?(os_name) end if BASE_OS_OPTIONS.include?(os_name) @@ -35,7 +35,7 @@ module OnSystem return Homebrew::SimulateSystem.send("#{os_name}?") end - raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOS::Version::SYMBOLS.key?(os_name) + raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOSVersions::SYMBOLS.key?(os_name) if or_condition.present? && [:or_newer, :or_older].exclude?(or_condition) raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}" @@ -87,7 +87,7 @@ module OnSystem end end - MacOS::Version::SYMBOLS.each_key do |os_name| + MacOSVersions::SYMBOLS.each_key do |os_name| onto.define_method("on_#{os_name}") do |or_condition = nil, &block| @on_system_blocks_exist = true diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 08a898c3aa..1a11464f69 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -72,6 +72,7 @@ HOMEBREW_BOTTLES_EXTNAME_REGEX = /\.([a-z0-9_]+)\.bottle\.(?:(\d+)\.)?tar\.gz$/. require "env_config" require "compat/early" unless Homebrew::EnvConfig.no_compat? +require "macos_versions" require "os" require "messages" require "default_prefix" diff --git a/Library/Homebrew/macos_versions.rb b/Library/Homebrew/macos_versions.rb new file mode 100644 index 0000000000..16d4bd47e4 --- /dev/null +++ b/Library/Homebrew/macos_versions.rb @@ -0,0 +1,20 @@ +# typed: true +# frozen_string_literal: true + +# Helper functions for querying operating system information. +# +# @api private +module MacOSVersions + # TODO: when removing symbols here, ensure that they are added to + # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. + SYMBOLS = { + ventura: "13", + monterey: "12", + big_sur: "11", + catalina: "10.15", + mojave: "10.14", + high_sierra: "10.13", + sierra: "10.12", + el_capitan: "10.11", + }.freeze +end diff --git a/Library/Homebrew/os.rb b/Library/Homebrew/os.rb index b2d6c64cad..3152c91389 100644 --- a/Library/Homebrew/os.rb +++ b/Library/Homebrew/os.rb @@ -50,25 +50,6 @@ module OS CI_GLIBC_VERSION = "2.23" CI_OS_VERSION = "Ubuntu 16.04" - module Mac - ::MacOS = OS::Mac - - class Version < ::Version - # TODO: when removing symbols here, ensure that they are added to - # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. - SYMBOLS = { - ventura: "13", - monterey: "12", - big_sur: "11", - catalina: "10.15", - mojave: "10.14", - high_sierra: "10.13", - sierra: "10.12", - el_capitan: "10.11", - }.freeze - end - end - if OS.mac? require "os/mac" # Don't tell people to report issues on unsupported configurations. diff --git a/Library/Homebrew/os/linux.rb b/Library/Homebrew/os/linux.rb index 57598261ef..c8fcc00e52 100644 --- a/Library/Homebrew/os/linux.rb +++ b/Library/Homebrew/os/linux.rb @@ -8,6 +8,8 @@ module OS module_function + ::MacOS = OS::Mac + sig { returns(String) } def os_version if which("lsb_release") diff --git a/Library/Homebrew/os/mac.rb b/Library/Homebrew/os/mac.rb index b23e62dd8b..5f422450e9 100644 --- a/Library/Homebrew/os/mac.rb +++ b/Library/Homebrew/os/mac.rb @@ -13,6 +13,8 @@ module OS module_function + ::MacOS = OS::Mac + raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] VERSION = ENV.fetch("HOMEBREW_MACOS_VERSION").chomp.freeze diff --git a/Library/Homebrew/os/mac/version.rb b/Library/Homebrew/os/mac/version.rb index a7d183eaef..a460141c50 100644 --- a/Library/Homebrew/os/mac/version.rb +++ b/Library/Homebrew/os/mac/version.rb @@ -30,7 +30,7 @@ module OS sig { params(version: Symbol).returns(T.attached_class) } def self.from_symbol(version) - str = SYMBOLS.fetch(version) { raise MacOSVersionError, version } + str = MacOSVersions::SYMBOLS.fetch(version) { raise MacOSVersionError, version } new(str) end @@ -48,10 +48,10 @@ module OS sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } def <=>(other) @comparison_cache.fetch(other) do - if SYMBOLS.key?(other) && to_sym == other + if MacOSVersions::SYMBOLS.key?(other) && to_sym == other 0 else - v = SYMBOLS.fetch(other) { other.to_s } + v = MacOSVersions::SYMBOLS.fetch(other) { other.to_s } @comparison_cache[other] = super(::Version.new(v)) end end @@ -69,7 +69,7 @@ module OS sig { returns(Symbol) } def to_sym - @to_sym ||= SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) + @to_sym ||= MacOSVersions::SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) end sig { returns(String) } diff --git a/Library/Homebrew/simulate_system.rb b/Library/Homebrew/simulate_system.rb index 12d1e0e34d..d52a2af752 100644 --- a/Library/Homebrew/simulate_system.rb +++ b/Library/Homebrew/simulate_system.rb @@ -13,7 +13,7 @@ module Homebrew sig { params(new_os: Symbol).void } def os=(new_os) - os_options = [:macos, :linux, *MacOS::Version::SYMBOLS.keys] + os_options = [:macos, :linux, *MacOSVersions::SYMBOLS.keys] raise "Unknown OS: #{new_os}" unless os_options.include?(new_os) @os = new_os @@ -38,7 +38,7 @@ module Homebrew sig { returns(T::Boolean) } def macos? - [:macos, *MacOS::Version::SYMBOLS.keys].include?(@os) + [:macos, *MacOSVersions::SYMBOLS.keys].include?(@os) end sig { returns(T::Boolean) } diff --git a/Library/Homebrew/test/cask/cmd/list_spec.rb b/Library/Homebrew/test/cask/cmd/list_spec.rb index 6c0e3db2ff..77b6e7af89 100644 --- a/Library/Homebrew/test/cask/cmd/list_spec.rb +++ b/Library/Homebrew/test/cask/cmd/list_spec.rb @@ -256,7 +256,7 @@ describe Cask::Cmd::List, :cask do catalina: "10.15", mojave: "10.14", } - stub_const("MacOS::Version::SYMBOLS", symbols) + stub_const("MacOSVersions::SYMBOLS", symbols) # For consistency, always run on Monterey and ARM MacOS.full_version = "12" From a9e62b9e38826807d8de11b210a560550f285f9f Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Wed, 29 Jun 2022 11:38:55 -0400 Subject: [PATCH 08/10] `include OnSystem` to define DSL methods --- Library/Homebrew/cask/dsl.rb | 3 +-- Library/Homebrew/extend/on_system.rb | 26 ++++++++++++-------------- Library/Homebrew/formula.rb | 6 ++---- Library/Homebrew/resource.rb | 3 +-- Library/Homebrew/software_spec.rb | 5 ++--- 5 files changed, 18 insertions(+), 25 deletions(-) diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index 1748d5fce0..6367141d8c 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -92,6 +92,7 @@ module Cask ]).freeze extend Predicable + include OnSystem attr_reader :cask, :token @@ -319,8 +320,6 @@ module Cask @livecheckable == true end - OnSystem.setup_methods! onto: self, include_linux: false - ORDINARY_ARTIFACT_CLASSES.each do |klass| define_method(klass.dsl_key) do |*args| if [*artifacts.map(&:class), klass].include?(Artifact::StageOnly) && diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index e008a6feca..01e489b0f4 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -55,10 +55,10 @@ module OnSystem method_name.to_s.sub(/^on_/, "").to_sym end - sig { params(onto: Class, include_linux: T::Boolean).void } - def setup_methods!(onto:, include_linux: true) + sig { params(base: Class).void } + def self.included(base) ARCH_OPTIONS.each do |arch| - onto.define_method("on_#{arch}") do |&block| + base.define_method("on_#{arch}") do |&block| @on_system_blocks_exist = true return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(__method__) @@ -71,24 +71,22 @@ module OnSystem end end - if include_linux - BASE_OS_OPTIONS.each do |base_os| - onto.define_method("on_#{base_os}") do |&block| - @on_system_blocks_exist = true + BASE_OS_OPTIONS.each do |base_os| + base.define_method("on_#{base_os}") do |&block| + @on_system_blocks_exist = true - return unless OnSystem.os_condition_met? OnSystem.condition_from_method_name(__method__) + return unless OnSystem.os_condition_met? OnSystem.condition_from_method_name(__method__) - @called_in_on_system_block = true - result = block.call - @called_in_on_system_block = false + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false - result - end + result end end MacOSVersions::SYMBOLS.each_key do |os_name| - onto.define_method("on_#{os_name}") do |or_condition = nil, &block| + base.define_method("on_#{os_name}") do |or_condition = nil, &block| @on_system_blocks_exist = true os_condition = OnSystem.condition_from_method_name __method__ diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 250583b465..09ede72647 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -64,12 +64,11 @@ class Formula include Utils::Shebang include Utils::Shell include Context + include OnSystem extend Forwardable extend Cachable extend Predicable - OnSystem.setup_methods! onto: self - # @!method inreplace(paths, before = nil, after = nil) # @see Utils::Inreplace.inreplace @@ -2471,6 +2470,7 @@ class Formula # The methods below define the formula DSL. class << self include BuildEnvironment::DSL + include OnSystem def method_added(method) super @@ -2483,8 +2483,6 @@ class Formula end end - OnSystem.setup_methods! onto: self - # The reason for why this software is not linked (by default) to # {::HOMEBREW_PREFIX}. # @private diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 9cbd64bc7f..9d407c5aab 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -17,8 +17,7 @@ class Resource include Context include FileUtils - - OnSystem.setup_methods! onto: self + include OnSystem attr_reader :mirrors, :specs, :using, :source_modified_time, :patches, :owner attr_writer :version diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 61a0b8d463..35734f1aa5 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -18,14 +18,13 @@ class SoftwareSpec extend T::Sig extend Forwardable + include OnSystem PREDEFINED_OPTIONS = { universal: Option.new("universal", "Build a universal binary"), cxx11: Option.new("c++11", "Build using C++11 mode"), }.freeze - OnSystem.setup_methods! onto: self - attr_reader :name, :full_name, :owner, :build, :resources, :patches, :options, :deprecated_flags, :deprecated_options, :dependency_collector, :bottle_specification, :compiler_failures, :uses_from_macos_elements @@ -584,7 +583,7 @@ class BottleSpecification end class PourBottleCheck - OnSystem.setup_methods! onto: self + include OnSystem def initialize(formula) @formula = formula From 1294de9e4878d69019c73aee57328ae1b9ca59ef Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Wed, 29 Jun 2022 11:45:02 -0400 Subject: [PATCH 09/10] Fix `Formula::on_macos` type annotation --- Library/Homebrew/formula.rbi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/formula.rbi b/Library/Homebrew/formula.rbi index a47b034dee..f77f4c0c50 100644 --- a/Library/Homebrew/formula.rbi +++ b/Library/Homebrew/formula.rbi @@ -49,6 +49,6 @@ class Formula def env; end def conflicts; end - # This method is generated by `OnSystem::setup_methods!` + # This method is included by `OnSystem` def self.on_macos(&block); end end From b271ae151b7e06d7179687aafab2fa5dab9a3984 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Wed, 29 Jun 2022 11:47:57 -0400 Subject: [PATCH 10/10] Cleanup --- Library/Homebrew/os.rb | 2 -- Library/Homebrew/os/linux.rb | 4 ++-- Library/Homebrew/os/mac/version.rb | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/os.rb b/Library/Homebrew/os.rb index 3152c91389..52945a402a 100644 --- a/Library/Homebrew/os.rb +++ b/Library/Homebrew/os.rb @@ -1,8 +1,6 @@ # typed: true # frozen_string_literal: true -require "version" - # Helper functions for querying operating system information. # # @api private diff --git a/Library/Homebrew/os/linux.rb b/Library/Homebrew/os/linux.rb index c8fcc00e52..277e3de3a9 100644 --- a/Library/Homebrew/os/linux.rb +++ b/Library/Homebrew/os/linux.rb @@ -8,8 +8,6 @@ module OS module_function - ::MacOS = OS::Mac - sig { returns(String) } def os_version if which("lsb_release") @@ -33,6 +31,8 @@ module OS module Mac module_function + ::MacOS = OS::Mac + raise "Loaded OS::Linux on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] def version diff --git a/Library/Homebrew/os/mac/version.rb b/Library/Homebrew/os/mac/version.rb index a460141c50..5f86f697a2 100644 --- a/Library/Homebrew/os/mac/version.rb +++ b/Library/Homebrew/os/mac/version.rb @@ -16,7 +16,7 @@ module OS # TODO: bump version when new macOS is released or announced # and also update references in docs/Installation.md, # https://github.com/Homebrew/install/blob/HEAD/install.sh and - # OS::Mac::Version (in HOMEBREW_LIBRARY/os.rb) + # MacOSVersions::SYMBOLS NEWEST_UNSUPPORTED = "13" private_constant :NEWEST_UNSUPPORTED