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
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
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.
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
.setup_arch_methods(base) ⇒ void
This method returns an undefined value.
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.
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.
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
|