forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathci_matrix.rb
177 lines (146 loc) · 5.61 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
# frozen_string_literal: true
require "cask/cask_loader"
require_relative "changed_files"
module CiMatrix
MAX_JOBS = 256
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
# 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
return filtered_runners unless filtered_runners.empty?
RUNNERS
end
def self.random_runner(avalible_runners = 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
else
# Otherwise, select a runner based on weighted random sample.
[random_runner(filtered_runners)]
end
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.generate(tap, labels: [])
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
modified_cask_files = changed_files[:modified_cask_files]
jobs = modified_cask_files.count
odie "Maximum job matrix size exceeded: #{jobs}/#{MAX_JOBS}" if jobs > MAX_JOBS
modified_cask_files.flat_map do |path|
cask_token = path.basename(".rb")
audit_args = ["--online"]
audit_args << "--new-cask" if changed_files[:added_files].include?(path)
audit_args << "--signing"
audit_exceptions = []
if labels.include?("ci-skip-livecheck")
audit_exceptions << %w[hosting_with_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_args << "--except" << audit_exceptions.join(",") if audit_exceptions.any?
cask_content = path.read
runners(cask_content: cask_content).product(architectures(cask_content: cask_content)).map do |runner, arch|
native_runner_arch = arch == runner.fetch(:arch)
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_readall: !native_runner_arch,
runner: runner.fetch(:name),
}
end
end
end
end