-
Notifications
You must be signed in to change notification settings - Fork 16
/
Rakefile
143 lines (128 loc) · 4.59 KB
/
Rakefile
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
# frozen_string_literal: true
require "yaml"
require "set"
namespace :build_matrix do
namespace :semaphore do
task :generate do
node_version = "16"
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