forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathci_matrix.rb
204 lines (169 loc) · 6.72 KB
/
ci_matrix.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
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
77
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
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
138
139
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true
require "cask/cask_loader"
require_relative "changed_files"
module CiMatrix
MAX_JOBS = 256
# Weight for each arch must add up to 1.0.
INTEL_RUNNERS = {
{ symbol: :big_sur, name: "macos-11", arch: :intel } => 0.0,
{ symbol: :monterey, name: "macos-12", arch: :intel } => 0.0,
{ symbol: :ventura, name: "macos-13", arch: :intel } => 1.0,
}.freeze
ARM_RUNNERS = {
{ symbol: :sonoma, name: "macos-14", arch: :arm } => 1.0,
}.freeze
RUNNERS = INTEL_RUNNERS.merge(ARM_RUNNERS).freeze
# This string uses regex syntax and is intended to be interpolated into
# `Regexp` literals, so the backslashes must be escaped to be preserved.
DEPENDS_ON_MACOS_ARRAY_MEMBER = '\\s*"?:([^\\s",]+)"?,?\\s*'
def self.filter_runners(cask_content)
# Retrieve arguments from `depends_on macos:`
required_macos = case cask_content
when /depends_on\s+macos:\s+\[((?:#{DEPENDS_ON_MACOS_ARRAY_MEMBER})+)\]/o
Regexp.last_match(1).scan(/#{DEPENDS_ON_MACOS_ARRAY_MEMBER}/o).flatten.map(&:to_sym).map do |v|
{
version: v,
comparator: "==",
}
end
when /depends_on\s+macos:\s+"?:([^\s"]+)"?/ # e.g. `depends_on macos: :big_sur`
[
{
version: Regexp.last_match(1).to_sym,
comparator: "==",
},
]
when /depends_on\s+macos:\s+"([=<>]=)\s+:([^\s"]+)"/ # e.g. `depends_on macos: ">= :monterey"`
[
{
version: Regexp.last_match(2).to_sym,
comparator: Regexp.last_match(1),
},
]
when /depends_on\s+macos:/
# In this case, `depends_on macos:` is present but wasn't matched by the
# previous regexes. We want this to visibly fail so we can address the
# shortcoming instead of quietly defaulting to `RUNNERS`.
odie "Unhandled `depends_on macos` argument"
else
[]
end
filtered_runners = RUNNERS.select do |runner, _|
required_macos.any? do |r|
MacOSVersion.from_symbol(runner.fetch(:symbol)).compare(
r.fetch(:comparator),
MacOSVersion.from_symbol(r.fetch(:version)),
)
end
end
filtered_runners = RUNNERS.dup if filtered_runners.empty?
archs = architectures(cask_content:)
filtered_runners.select! do |runner, _|
archs.include?(runner.fetch(:arch))
end
RUNNERS
end
def self.architectures(cask_content:)
case cask_content
when /depends_on\s+arch:\s+:arm64/
[:arm]
when /depends_on\s+arch:\s+:x86_64/
[:intel]
when /\barch\b/, /\bon_(arm|intel)\b/
[:arm, :intel]
else
RUNNERS.keys.map { |r| r.fetch(:arch) }.uniq.sort
end
end
def self.random_runner(avalible_runners = INTEL_RUNNERS)
avalible_runners.max_by { |(_, weight)| rand ** (1.0 / weight) }
.first
end
def self.runners(cask_content:)
filtered_runners = filter_runners(cask_content)
macos_version_found = cask_content.match?(/\bMacOS\s*\.version\b/m)
filtered_macos_found = filtered_runners.keys.any? do |runner|
(
macos_version_found &&
cask_content.include?(runner[:symbol].inspect)
) || cask_content.include?("on_#{runner[:symbol]}")
end
if filtered_macos_found
# If the cask varies on a MacOS version, test it on every possible macOS version.
[filtered_runners.keys, true]
else
# Otherwise, select a runner from each architecture based on weighted random sample.
grouped_runners = filtered_runners.group_by { |runner, _| runner.fetch(:arch) }
selected_runners = grouped_runners.map do |_, runners|
random_runner(runners)
end
[selected_runners, false]
end
end
def self.generate(tap, labels: [], cask_names: [], skip_install: false, new_cask: false)
odie "This command must be run from inside a tap directory." unless tap
tap.extend(ChangedFiles)
changed_files = tap.changed_files
ruby_files_in_wrong_directory =
changed_files[:modified_ruby_files] - (
changed_files[:modified_cask_files] +
changed_files[:modified_command_files] +
changed_files[:modified_github_actions_files]
)
if ruby_files_in_wrong_directory.any?
ruby_files_in_wrong_directory.each do |path|
puts "::error file=#{path}::File is in wrong directory."
end
odie "Found Ruby files in wrong directory:\n#{ruby_files_in_wrong_directory.join("\n")}"
end
cask_files_to_check = if cask_names.any?
cask_names.map do |cask_name|
Cask::CaskLoader.find_cask_in_tap(cask_name, tap).relative_path_from(tap.path)
end
else
changed_files[:modified_cask_files]
end
jobs = cask_files_to_check.count
odie "Maximum job matrix size exceeded: #{jobs}/#{MAX_JOBS}" if jobs > MAX_JOBS
cask_files_to_check.flat_map do |path|
cask_token = path.basename(".rb")
audit_args = ["--online"]
audit_args << "--new" if changed_files[:added_files].include?(path) || new_cask
audit_args << "--signing"
audit_exceptions = []
audit_exceptions << %w[homepage_https_availability] if labels.include?("ci-skip-homepage")
if labels.include?("ci-skip-livecheck")
audit_exceptions << %w[hosting_with_livecheck livecheck_https_availability
livecheck_min_os livecheck_version]
end
audit_exceptions << "livecheck_min_os" if labels.include?("ci-skip-livecheck-min-os")
if labels.include?("ci-skip-repository")
audit_exceptions << %w[github_repository github_prerelease_version
gitlab_repository gitlab_prerelease_version
bitbucket_repository]
end
audit_exceptions << %w[token_conflicts token_valid token_bad_words] if labels.include?("ci-skip-token")
audit_args << "--except" << audit_exceptions.join(",") if audit_exceptions.any?
cask_content = path.read
runners, multi_os = runners(cask_content:)
runners.product(architectures(cask_content:)).filter_map do |runner, arch|
native_runner_arch = arch == runner.fetch(:arch)
# If it's just a single OS test then we can just use the two real arch runners.
next if !native_runner_arch && !multi_os
arch_args = native_runner_arch ? [] : ["--arch=#{arch}"]
{
name: "test #{cask_token} (#{runner.fetch(:name)}, #{arch})",
tap: tap.name,
cask: {
token: cask_token,
path: "./#{path}",
},
audit_args: audit_args + arch_args,
fetch_args: arch_args,
skip_install: labels.include?("ci-skip-install") || !native_runner_arch || skip_install,
runner: runner.fetch(:name),
}
end
end
end
end