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

Class Method Summary collapse

Class Method Details

.arch_condition_met?(arch) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'extend/on_system.rb', line 15

def 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:



49
50
51
# File 'extend/on_system.rb', line 49

def 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)


132
133
134
# File 'extend/on_system.rb', line 132

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)


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

def 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 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}"
  end

  return false if Homebrew::SimulateSystem.simulating_or_running_on_linux?

  base_os = MacOS::Version.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
    MacOS::Version.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)


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

def 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(__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

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

.setup_base_os_methods(base) ⇒ void

This method returns an undefined value.

Parameters:

  • base (Class)


78
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
# File 'extend/on_system.rb', line 78

def 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(__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
end

.setup_macos_methods(base) ⇒ void

This method returns an undefined value.

Parameters:

  • base (Class)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'extend/on_system.rb', line 114

def setup_macos_methods(base)
  MacOSVersions::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 __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