- Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathdevtool.rb
executable file
·347 lines (262 loc) · 8.18 KB
/
devtool.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
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# Copyright (c) 2020-2023 Andre Richter <andre.o.richter@gmail.com>
require'rubygems'
require'bundler/setup'
require'colorize'
require'fileutils'
require_relative'devtool/copyright'
# Actions for tutorial folders.
classTutorialCrate
attr_reader:folder
definitialize(folder)
@folder=folder
end
deftutorial?
/[0-9]/.match?(@folder[0])
end
defclean
puts'Cleaning '.light_blue + @folder
# No output needed.
Dir.chdir(@folder){`make clean`}
end
defupdate
puts"\n\n"
puts'Updating '.light_blue + @folder
Dir.chdir(@folder){system('cargo update')}
end
defclippy(bsp)
puts"\n\n"
puts"Clippy #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make clippy")}
end
deffmt_cargo_rust(args)
Dir.chdir(@folder){exit(1)unlesssystem("cargo fmt #{args}")}
end
defmake(bsp)
puts"\n\n"
puts"Make #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make")}
end
deftest(bsp)
returnunlessboot_test?
puts"\n\n"
puts"Test #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make test")}
end
deftest_boot(bsp)
returnunlessboot_test?
puts"\n\n"
puts"Test Boot #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make test_boot")}
end
deftest_unit(bsp)
returnunlessunit_integration_tests?
puts"\n\n"
puts"Test Unit #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make test_unit")}
end
deftest_integration(bsp)
returnunlessunit_integration_tests?
puts"\n\n"
puts"Test Integration #{@folder} - BSP: #{bsp}".light_blue
Dir.chdir(@folder){exit(1)unlesssystem("BSP=#{bsp} make test_integration")}
end
private
defboot_test?
Dir.exist?("#{@folder}/tests") || Dir.exist?("#{@folder}/kernel/tests")
end
defunit_integration_tests?
!Dir.glob("#{@folder}/kernel/tests/00_*.rs").empty?
end
end
# Forks commands to all applicable receivers.
classDevTool
definitialize
@user_has_supplied_crates=false
@bsp=bsp_from_env || SUPPORTED_BSPS.first
cl=user_supplied_crate_list || Dir['*/Cargo.toml']
@crates=cl.map{ |c| TutorialCrate.new(c.delete_suffix('/Cargo.toml'))}
end
defclean
@crates.each(&:clean)
end
defupdate
@crates.each(&:update)
end
defclippy(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.clippy(bsp)}
end
defdiff
tuts=tutorials.map(&:folder)
padding=tuts.map(&:length).max
tuts[0..-2].each_with_indexdo |original,i|
update=tuts[i + 1]
diff_pair(original,update,padding)
end
end
deffmt
fmt_cargo_rust(check: false)
puts
fmt_prettier(check: false)
end
deffmt_check
fmt_cargo_rust(check: true)
puts
fmt_prettier(check: true)
end
defmake(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.make(bsp)}
end
defmake_xtra
returnif@user_has_supplied_crates
puts"\n\n"
puts'Make Xtra stuff'.light_blue
system('cd *_uart_chainloader && bash update.sh')
system('cd X1_JTAG_boot && bash update.sh')
end
deftest(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.test(bsp)}
end
deftest_boot(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.test_boot(bsp)}
end
deftest_unit(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.test_unit(bsp)}
end
deftest_integration(bsp=nil)
bsp ||= @bsp
@crates.each{ |c| c.test_integration(bsp)}
end
defcopyright
exit(1)unlesscopyright_check_files(copyright_source_files)
end
defmisspell
puts'Misspell'.light_blue
translations=['README.CN.md','README.ES.md']
files=tracked_files.reject{ |f| translations.include?(File.basename(f))}
files=files.join(' ')
exit(1)unlesssystem(".vendor/misspell -error #{files}")
end
defrubocop
puts'Rubocop'.light_blue
exit(1)unlesssystem('bundle exec rubocop')
end
defready_for_publish_no_rust
clean
fmt
rubocop
copyright
diff
misspell
clean
end
defready_for_publish
ready_for_publish_no_rust
make_xtra
clippy('rpi4')
clippy('rpi3')
test_boot('rpi3')
test_unit('rpi3')
test_integration('rpi3')
clean
end
private
SUPPORTED_BSPS=%w[rpi3rpi4].freeze
defbsp_from_env
bsp=ENV.fetch('BSP',nil)
returnbspifSUPPORTED_BSPS.include?(bsp)
nil
end
deffmt_cargo_rust(check: false)
args='-- --check'ifcheck
@crates.eachdo |c|
print'Rust cargo fmt '.light_blue
print"#{args} ".light_blueunlessargs.nil?
putsc.folder
Process.fork{c.fmt_cargo_rust(args)}
end
Process.waitall
end
deffmt_prettier(check: false)
args=ifcheck
'--check'
else
'--write'
end
args += if@user_has_supplied_crates
" #{@crates.map(&:folder).join(' ')}"
else
' .'
end
puts'Prettier:'.light_blue
exit(1)unlesssystem("./node_modules/.bin/prettier #{args}")
end
defuser_supplied_crate_list
folders=ARGV.drop(1)
returnniliffolders.empty?
crates=folders.map{ |d| "#{d}/Cargo.toml"}.sort
crates.eachdo |c|
unlessFile.exist?(c)
puts"Crate not found: #{c}"
exit(1)
end
end
@user_has_supplied_crates=true
crates
end
deftutorials
@crates.select(&:tutorial?)
end
deftracked_files
crate_list=@crates.map(&:folder).join(' ')if@user_has_supplied_crates
`git ls-files #{crate_list}`.split("\n")# crates_list == nil means all files
end
defdiff_pair(original,update,padding)
# Only diff adjacent tutorials. This checks the numbers of the tutorial folders.
returnunlessoriginal[0..1].to_i + 1 == update[0..1].to_i
# Skip for tutorial 11. Due to the change to virtual manifest, the diff is rather
# unreadable.
iforiginal[0..1].to_i == 11
puts'Skipping '.light_yellow +
"#{original}: Too noisy due to change to virtual manifest"
return
end
puts'Diffing '.light_blue + original.ljust(padding) + " -> #{update}"
system("bash utils/diff_tut_folders.bash #{original}#{update}")
end
defcopyright_source_files
extensions=['.S','.rs','.rb']
# NOTE: The selection result is the return value of the function.
tracked_files.selectdo |f|
nextunlessFile.exist?(f)
nextiff.include?('build.rs')
nextiff.include?('boot_test_string.rb')
f.include?('Makefile') ||
f.include?('Dockerfile') ||
extensions.include?(File.extname(f))
end
end
end
## -------------------------------------------------------------------------------------------------
## Execution starts here
## -------------------------------------------------------------------------------------------------
tool=DevTool.new
cmd=ARGV[0]
commands=tool.public_methods(false).sort
ifcommands.include?(cmd&.to_sym)
tool.public_send(cmd)
else
puts"Usage: ./#{__FILE__.split('/').last} COMMAND [optional list of folders]"
puts
puts'Commands:'
commands.each{ |m| puts" #{m}"}
exit(1)
end