Module: OnSystem

Defined in:
extend/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 =
[*MacOSVersion::SYMBOLS.keys, :linux].freeze
ALL_OS_ARCH_COMBINATIONS =
ALL_OS_OPTIONS.product(ARCH_OPTIONS).freeze

Class Method Summary collapse

Class Method Details

.arch_condition_met?(arch) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'extend/on_system.rb', line 13

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

Parameters:

Returns:



47
48
49
# File 'extend/on_system.rb', line 47

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

.included(_base) ⇒ void

This method returns an undefined value.

Parameters:

  • _base (Class)


143
144
145
# File 'extend/on_system.rb', line 143

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

Parameters:

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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'extend/on_system.rb', line 20

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

  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 returns an undefined value.

Parameters:

  • base (Class)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'extend/on_system.rb', line 52

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

      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 = true

    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 returns an undefined value.

Parameters:

  • base (Class)


79
80
81
82
83
84
85
86
87
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
# File 'extend/on_system.rb', line 79

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 = true

      return unless OnSystem.os_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_system) do |linux, macos:, &block|
    @on_system_blocks_exist = true

    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
    result = block.call
    @called_in_on_system_block = false

    result
  end

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

    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 returns an undefined value.

Parameters:

  • base (Class)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'extend/on_system.rb', line 125

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 = true

      os_condition = OnSystem.condition_from_method_name T.must(__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