-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRakefile
More file actions
393 lines (367 loc) · 12.6 KB
/
Rakefile
File metadata and controls
393 lines (367 loc) · 12.6 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# frozen_string_literal: true
require "yaml"
NODE_VERSION = "24"
namespace :build_matrix do
namespace :github do
task :generate do
node_version = NODE_VERSION
yaml = YAML.load_file("build_matrix.yml")
matrix = yaml["matrix"]
skipped_packages = Set.new
# Build test matrix for GitHub Actions
test_matrix = []
matrix["packages"].each do |package|
has_package_tests = package_has_tests?(package["path"])
if !has_package_tests && skipped_packages.add?(package["package"])
puts "DEBUG: Skipping JavaScript tests for #{package["package"]}: No tests files found"
end
package["variations"].each do |variation|
variation.fetch("name")
dependency_specification = variation["packages"]
dependencies = ""
if dependency_specification
packages = dependency_specification.map do |name, version|
"#{name}@#{version}"
end
dependencies = packages.join(" ")
end
next unless has_package_tests
test_matrix << {
"package" => package["package"],
"dependencies" => dependencies
}
end
end
# Generate GitHub Actions workflow
github_actions = {
"name" => "CI",
"on" => {
"push" => {
"branches" => ["main", "develop"]
},
"pull_request" => {
"branches" => ["main", "develop"]
},
"schedule" => [
{ "cron" => "0 0 * * 1-5" }
]
},
"concurrency" => {
"group" => "${{ github.workflow }}-${{ github.ref }}",
"cancel-in-progress" => "${{ !contains(github.ref, 'main')}}"
},
"env" => {
"RUNNING_IN_CI" => "true",
"NODE_ENV" => "test"
},
"jobs" => {
"validate-changesets" => {
"uses" => "./.github/workflows/validate_changesets.yml"
},
"validate" => {
"name" => "Validate CI setup",
"runs-on" => "ubuntu-latest",
"steps" => [
{ "uses" => "actions/checkout@v4" },
{
"name" => "Setup Ruby",
"uses" => "ruby/setup-ruby@v1",
"with" => { "bundler-cache" => true }
},
{
"name" => "Validate CI setup",
"run" => "rake build_matrix:github:validate"
}
]
},
"lint-ruby" => {
"name" => "Ruby linter (RuboCop)",
"runs-on" => "ubuntu-latest",
"steps" => [
{ "uses" => "actions/checkout@v4" },
{
"name" => "Setup Ruby",
"uses" => "ruby/setup-ruby@v1",
"with" => { "bundler-cache" => true }
},
{
"name" => "Run RuboCop",
"run" => "bundle exec rubocop"
}
]
},
"lint-javascript" => {
"name" => "JavaScript linter (Prettier)",
"runs-on" => "ubuntu-latest",
"steps" => [
{ "uses" => "actions/checkout@v4" },
{
"name" => "Setup Node.js",
"uses" => "actions/setup-node@v4",
"with" => {
"node-version" => NODE_VERSION,
"cache" => "npm"
}
},
{
"name" => "Install dependencies",
"run" => "npm install"
},
{
"name" => "Run Prettier",
"run" => "npm run lint"
}
]
},
"lint-git" => {
"name" => "Git linter (Lintje)",
"runs-on" => "ubuntu-latest",
"if" => "${{ github.event_name != 'schedule' }}",
"steps" => [
{
"uses" => "actions/checkout@v4",
"with" => { "fetch-depth" => 0 }
},
{
"name" => "Run Git linter",
"uses" => "lintje/action@v0.11"
}
]
},
"build" => {
"name" => "Build",
"runs-on" => "ubuntu-latest",
"needs" => ["validate"],
"steps" => [
{ "uses" => "actions/checkout@v4" },
{
"name" => "Setup Node.js",
"uses" => "actions/setup-node@v4",
"with" => {
"node-version" => node_version,
"cache" => "npm"
}
},
{
"name" => "Setup mono",
"run" => "script/setup && echo \"$HOME/mono/bin\" >> $GITHUB_PATH"
},
{
"name" => "Install dependencies",
"run" => "npm ci"
},
{
"name" => "Bootstrap packages",
"run" => "mono bootstrap --ci"
},
{
"name" => "Build packages",
"run" => "mono build"
},
{
"name" => "Cache build artifacts",
"uses" => "actions/cache@v4",
"with" => {
"path" => "packages/*/dist",
"key" => "build-${{ github.sha }}-${{ github.run_id }}"
}
}
]
},
"test" => {
"name" => "Test",
"runs-on" => "ubuntu-latest",
"needs" => ["build"],
"strategy" => {
"fail-fast" => false,
"matrix" => {
"include" => test_matrix
}
},
"steps" => [
{ "uses" => "actions/checkout@v4" },
{
"name" => "Setup Node.js",
"uses" => "actions/setup-node@v4",
"with" => {
"node-version" => node_version,
"cache" => "npm"
}
},
{
"name" => "Setup mono",
"run" => "script/setup && echo \"$HOME/mono/bin\" >> $GITHUB_PATH"
},
{
"name" => "Install dependencies",
"run" => "npm ci"
},
{
"name" => "Bootstrap packages",
"run" => "mono bootstrap --ci"
},
{
"name" => "Restore build artifacts",
"uses" => "actions/cache@v4",
"with" => {
"path" => "packages/*/dist",
"key" => "build-${{ github.sha }}-${{ github.run_id }}"
}
},
{
"name" => "Install test dependencies",
"if" => "matrix.dependencies != ''",
"run" => "script/install_packages ${{ matrix.dependencies }}"
},
{
"name" => "Run tests",
"run" => "mono test --package=${{ matrix.package }}"
}
]
}
}
}
header = "# DO NOT EDIT\n" \
"# This is a generated file by the `rake build_matrix:github:generate` task.\n" \
"# See `build_matrix.yml` for the build matrix.\n" \
"# Generate this file with `rake build_matrix:github:generate`.\n"
require "fileutils"
FileUtils.mkdir_p(".github/workflows")
generated_yaml = header + YAML.dump(github_actions)
File.write(".github/workflows/ci.yml", generated_yaml)
puts "Generated `.github/workflows/ci.yml`"
puts "Test matrix count: #{test_matrix.length}"
end
task :validate => :generate do
`git status | grep .github/workflows/ci.yml 2>&1`
if $?.exitstatus.zero? # rubocop:disable Style/SpecialGlobalVars
puts "The `.github/workflows/ci.yml` is modified. The changes were not committed."
puts "Please run `rake build_matrix:github:generate` and commit the changes."
exit 1
end
end
end
namespace :semaphore do
task :generate do
node_version = NODE_VERSION
yaml = YAML.load_file("build_matrix.yml")
matrix = yaml["matrix"]
semaphore = yaml["semaphore"]
skipped_packages = Set.new
build_block_name = "JavaScript with Node.js #{node_version} - build"
build_block = build_semaphore_task(
"name" => build_block_name,
"dependencies" => ["Validation"],
"task" => {
"env_vars" => ["name" => "NODE_VERSION", "value" => node_version],
"prologue" => {
"commands" => [
"cache restore",
"mono bootstrap --ci",
"cache store"
]
},
"jobs" => [
build_semaphore_job(
"name" => "Build",
"commands" => [
"mono build",
"cache store $_PACKAGES_CACHE-packages-$SEMAPHORE_GIT_SHA-v$NODE_VERSION " \
"packages"
]
)
]
}
)
tests_block_name = "JavaScript - Tests"
test_jobs = []
matrix["packages"].each do |package|
has_package_tests = package_has_tests?(package["path"])
if !has_package_tests && skipped_packages.add?(package["package"])
puts "DEBUG: Skipping JavaScript tests for #{package["package"]}: No tests files found"
end
package["variations"].each do |variation|
variation_name = variation.fetch("name")
dependency_specification = variation["packages"]
update_package_version_command =
if dependency_specification
packages = dependency_specification.map do |name, version|
"#{name}@#{version}"
end.join(" ")
"script/install_packages #{packages}"
end
next unless has_package_tests
test_jobs << build_semaphore_job(
"name" => "#{package["package"]} - #{variation_name}",
"commands" => ([
update_package_version_command,
"mono test --package=#{package["package"]}"
] + package.fetch("extra_commands", [])).compact
)
end
end
tests_block =
build_semaphore_task(
"name" => tests_block_name,
"dependencies" => [build_block_name],
"task" => {
"env_vars" => [
{
"name" => "NODE_VERSION",
"value" => node_version
}
],
"prologue" => {
"commands" => [
"cache restore",
"cache restore $_PACKAGES_CACHE-packages-$SEMAPHORE_GIT_SHA-v$NODE_VERSION",
"mono bootstrap --ci"
]
},
"jobs" => test_jobs
}
)
builds = [build_block, tests_block]
semaphore["blocks"] += builds
header = "# DO NOT EDIT\n" \
"# This is a generated file by the `rake build_matrix:semaphore:generate` task.\n" \
"# See `build_matrix.yml` for the build matrix.\n" \
"# Generate this file with `rake build_matrix:semaphore:generate`.\n"
generated_yaml = header + YAML.dump(semaphore)
File.write(".semaphore/semaphore.yml", generated_yaml)
puts "Generated `.semaphore/semaphore.yml`"
puts "Task count: #{builds.length}"
puts "Jobs count: #{builds.sum { |block| block["task"]["jobs"].count }}"
end
task :validate => :generate do
`git status | grep .semaphore/semaphore.yml 2>&1`
if $?.exitstatus.zero? # rubocop:disable Style/SpecialGlobalVars
puts "The `.semaphore/semaphore.yml` is modified. The changes were not committed."
puts "Please run `rake build_matrix:semaphore:generate` and commit the changes."
exit 1
end
end
end
end
def build_semaphore_task(task_hash)
{
"name" => task_hash.delete("name") { raise "`name` key not found for task" },
"dependencies" => [],
"task" => task_hash.delete("task") { raise "`task` key not found for task" }
}.merge(task_hash)
end
def build_semaphore_job(job_hash)
{
"name" => job_hash.delete("name") { "`name` key not found" },
"commands" => []
}.merge(job_hash)
end
def package_has_tests?(package)
test_dir = File.join(package, "src/__tests__")
# Has a dedicated test dir and it contains files
return true if Dir.exist?(test_dir) && Dir.glob(File.join(test_dir, "**", "*.*s*")).any?
Dir
.glob(File.join(package, "**/*.test.*s"))
.reject { |file| file.include?("/node_modules/") }
.any?
end