Module: OnSystem

Defined in:
on_system.rb

Defined Under Namespace

Modules: MacOSAndLinux, MacOSOnly

Constant Summary collapse

ARCH_OPTIONS =
[:intel, :arm].freeze
BASE_OS_OPTIONS =
[:macos, :linux].freeze
ALL_OS_OPTIONS =
T.let([*MacOSVersion::SYMBOLS.keys, :linux].freeze, T::Array[Symbol])
ALL_OS_ARCH_COMBINATIONS =
T.let(ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze, T::Array[[Symbol, Symbol]])
VALID_OS_ARCH_TAGS =
T.let(ALL_OS_ARCH_COMBINATIONS.filter_map do |os, arch|
  tag = Utils::Bottles::Tag.new(system: os, arch:)
  next unless tag.valid_combination?

  tag
end.freeze, T::Array[Utils::Bottles::Tag])

Class Method Summary collapse

Class Method Details

.arch_condition_met?(arch) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'on_system.rb', line 20

def self.arch_condition_met?(arch)
  raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(arch)

  arch == Homebrew::SimulateSystem.current_arch
end

.condition_from_method_name(method_name) ⇒ Symbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



56
57
58
# File 'on_system.rb', line 56

def self.condition_from_method_name(method_name)
  method_name.to_s.sub(/^on_/, "").to_sym
end

.included(_base) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • _base (T::Class[T.anything])


169
170
171
# File 'on_system.rb', line 169

def self.included(_base)
  raise "Do not include `OnSystem` directly. Instead, include `OnSystem::MacOSAndLinux` or `OnSystem::MacOSOnly`"
end

.os_condition_met?(os_name, or_condition = nil) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • os_name (Symbol)
  • or_condition (Symbol, nil) (defaults to: nil)

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'on_system.rb', line 27

def self.os_condition_met?(os_name, or_condition = nil)
  if BASE_OS_OPTIONS.include?(os_name)
    return Homebrew::SimulateSystem.public_send(:"simulating_or_running_on_#{os_name}?")
  end

  raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOSVersion::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

  return false if Homebrew::SimulateSystem.simulating_or_running_on_linux?

  base_os = MacOSVersion.from_symbol(os_name)
  current_os = if Homebrew::SimulateSystem.current_os == :macos
    # Assume the oldest macOS version when simulating a generic macOS version
    # Version::NULL is always treated as less than any other version.
    Version::NULL
  else
    MacOSVersion.from_symbol(Homebrew::SimulateSystem.current_os)
  end

  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

.setup_arch_methods(base) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • base (T::Class[T.anything])


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'on_system.rb', line 61

def self.setup_arch_methods(base)
  ARCH_OPTIONS.each do |arch|
    base.define_method(:"on_#{arch}") do |&block|
      @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))

      return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(T.must(__method__))

      @called_in_on_system_block = true
      result = block.call
      @called_in_on_system_block = false

      result
    end
  end

  base.define_method(:on_arch_conditional) do |arm: nil, intel: nil|
    @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))

    if OnSystem.arch_condition_met? :arm
      arm
    elsif OnSystem.arch_condition_met? :intel
      intel
    end
  end
end

.setup_base_os_methods(base) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • base (T::Class[T.anything])


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'on_system.rb', line 88

def self.setup_base_os_methods(base)
  BASE_OS_OPTIONS.each do |base_os|
    base.define_method(:"on_#{base_os}") do |&block|
      @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))
      @on_os_blocks_exist = T.let(true, T.nilable(TrueClass))

      return unless OnSystem.os_condition_met? OnSystem.condition_from_method_name(T.must(__method__))

      @called_in_on_system_block = true
      @called_in_on_os_block = T.let(true, T.nilable(T::Boolean))
      result = block.call
      @called_in_on_system_block = false
      @called_in_on_os_block = false

      result
    end
  end

  base.define_method(:on_system) do |linux, macos:, &block|
    @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))
    @on_os_blocks_exist = T.let(true, T.nilable(TrueClass))

    raise ArgumentError, "The first argument to `on_system` must be `:linux`" if linux != :linux

    os_version, or_condition = if macos.to_s.include?("_or_")
      macos.to_s.split(/_(?=or_)/).map(&:to_sym)
    else
      [macos.to_sym, nil]
    end
    return if !OnSystem.os_condition_met?(os_version, or_condition) && !OnSystem.os_condition_met?(:linux)

    @called_in_on_system_block = true
    @called_in_on_os_block = T.let(true, T.nilable(T::Boolean))
    result = block.call
    @called_in_on_system_block = false
    @called_in_on_os_block = false

    result
  end

  base.define_method(:on_system_conditional) do |macos: nil, linux: nil|
    @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))

    if OnSystem.os_condition_met?(:macos) && macos.present?
      macos
    elsif OnSystem.os_condition_met?(:linux) && linux.present?
      linux
    end
  end
end

.setup_macos_methods(base) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • base (T::Class[T.anything])


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'on_system.rb', line 140

def self.setup_macos_methods(base)
  MacOSVersion::SYMBOLS.each_key do |os_name|
    base.define_method(:"on_#{os_name}") do |or_condition = nil, &block|
      @on_system_blocks_exist = T.let(true, T.nilable(TrueClass))
      @on_os_blocks_exist = T.let(true, T.nilable(TrueClass))

      os_condition = OnSystem.condition_from_method_name T.must(__method__)
      return unless OnSystem.os_condition_met? os_condition, or_condition

      @on_system_block_min_os = T.let(
        if or_condition == :or_older
          @called_in_on_system_block ? @on_system_block_min_os : MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED)
        else
          MacOSVersion.from_symbol(os_condition)
        end,
        T.nilable(MacOSVersion),
      )
      @called_in_on_system_block = T.let(true, T.nilable(T::Boolean))
      @called_in_on_os_block = T.let(true, T.nilable(T::Boolean))
      result = block.call
      @called_in_on_system_block = false
      @called_in_on_os_block = false

      result
    end
  end
end