-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_checks_workflow.rb
207 lines (190 loc) · 4.82 KB
/
generate_checks_workflow.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
205
206
207
# frozen_string_literal: true
require 'set'
require 'yaml'
require_relative '../../lib/str_metrics/version'
def deep_stringify_keys(element)
return element.map(&method(:deep_stringify_keys)) if element.is_a?(Array)
return element.map { |k, v| [k.to_s, deep_stringify_keys(v)] }.to_h if element.is_a?(Hash)
element
end
DEFAULT_RUBY_VERSION = '2.7'
DEFAULT_RUST_VERSION = '1.42.0'
RUBY_VERSIONS = Set.new([DEFAULT_RUBY_VERSION, '2.7', '2.6', '2.5', '2.4', '2.3', 'jruby', 'truffleruby']).to_a.freeze
RUST_VERSIONS = Set.new([DEFAULT_RUST_VERSION, '1.42.0', '1.41.0', '1.40.0', '1.39.0', '1.38.0']).to_a.freeze
INSTALL_GEMS_STEPS = [
{
name: 'Install gems',
run: [
'bundle config path vendor/bundle',
'bundle install --jobs=4 --retry=3'
].join("\n")
}
].freeze
CACHE_GEMS_STEPS = [
{
name: 'Generate Gemfile.lock',
run: 'bundle lock'
},
{
name: 'Cache gems',
uses: 'actions/cache@v1',
with: {
path: 'vendor/bundle',
key: "v0-${{ runner.os }}-${{ matrix.ruby }}-gems-${{ hashFiles('**/Gemfile.lock') }}",
'restore-keys': 'v0-${{ runner.os }}-${{ matrix.ruby }}-gems-'
}
}
].freeze
INSTALL_RUST_STEPS = [
{
uses: 'actions-rs/toolchain@v1',
id: 'rust_install',
with: {
profile: 'minimal',
toolchain: '${{ matrix.rust }}',
default: true
}
}
].freeze
CACHE_CARGO_STEPS = [
{
name: 'Generate Cargo.lock',
run: 'cargo generate-lockfile'
},
# seems to be failing to restore for the moment: https://github.com/actions/cache/issues/133
# {
# name: 'Cache cargo registry',
# uses: 'actions/cache@v1',
# with: {
# path: '~/.cargo/registry',
# key: "v0-${{ runner.os }}-${{ matrix.rust }}-${{ steps.rust_install.outputs.rustc_hash }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}"
# }
# },
{
name: 'Cache cargo index',
uses: 'actions/cache@v1',
with: {
path: '~/.cargo/git',
key: "v0-${{ runner.os }}-${{ matrix.rust }}-${{ steps.rust_install.outputs.rustc_hash }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}"
}
},
{
name: 'Cache cargo build',
uses: 'actions/cache@v1',
with: {
path: 'target',
key: "v0-${{ runner.os }}-${{ matrix.rust }}-${{ steps.rust_install.outputs.rustc_hash }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}"
}
}
].freeze
INSTALL_RUBY_STEPS = [
{
uses: 'ruby/setup-ruby@v1',
with: { 'ruby-version': '${{ matrix.ruby }}' }
}
].freeze
TEST_STEPS = [
{ uses: 'actions/checkout@v2' },
*INSTALL_RUBY_STEPS,
*INSTALL_RUST_STEPS,
*CACHE_GEMS_STEPS,
*CACHE_CARGO_STEPS,
*INSTALL_GEMS_STEPS,
{
name: 'rspec',
run: 'bundle exec rake spec'
}
].freeze
RUBOCOP_JOB = {
rubocop: {
strategy: {
matrix: {
ruby: [DEFAULT_RUBY_VERSION]
}
},
'runs-on': 'ubuntu-latest',
steps: [
{ uses: 'actions/checkout@v2' },
*INSTALL_RUBY_STEPS,
*CACHE_GEMS_STEPS,
*INSTALL_GEMS_STEPS,
{
name: 'rubocop',
run: 'bundle exec rubocop --parallel'
}
]
}
}.freeze
def gem_install_job(runs_on)
{
"gem-install-#{runs_on}": {
strategy: {
matrix: {
ruby: [DEFAULT_RUBY_VERSION],
rust: [DEFAULT_RUST_VERSION]
}
},
'runs-on': runs_on,
steps: [
{ uses: 'actions/checkout@v2' },
*INSTALL_RUBY_STEPS,
*INSTALL_RUST_STEPS,
*CACHE_GEMS_STEPS,
*CACHE_CARGO_STEPS,
*INSTALL_GEMS_STEPS,
{
name: 'Package gem',
run: 'bundle exec rake build'
},
{
name: 'Install packaged gem',
run: "gem install pkg/str_metrics-#{StrMetrics::VERSION}.gem"
},
{
name: 'Make sure gem is loadable',
run: "ruby -rstr_metrics -e 'puts StrMetrics::VERSION'"
}
]
}
}
end
def sanity_test_job(runs_on)
{
"sanity-test-#{runs_on}": {
strategy: {
matrix: {
ruby: [DEFAULT_RUBY_VERSION],
rust: [DEFAULT_RUST_VERSION]
}
},
'runs-on': runs_on,
steps: TEST_STEPS
}
}
end
CHECKS_WORKFLOW = {
name: 'checks',
on: %w[push],
jobs: {
test: {
strategy: {
matrix: {
ruby: RUBY_VERSIONS,
rust: RUST_VERSIONS
}
},
'runs-on': 'ubuntu-latest',
steps: TEST_STEPS
},
**RUBOCOP_JOB,
**sanity_test_job('windows-latest'),
**sanity_test_job('macos-latest'),
**gem_install_job('ubuntu-latest'),
**gem_install_job('macos-latest'),
**gem_install_job('windows-latest')
}
}.then(&method(:deep_stringify_keys)).freeze
File.open(File.expand_path(File.join(__dir__, 'checks.yml')), 'w') do |f|
f.puts("# THIS IS A GENERATED FILE. DO NOT EDIT MANUALLY. EDIT & RUN gen_github_workflow.rb TO REGENERATE.\n")
f.write(CHECKS_WORKFLOW.to_yaml)
end