@@ -11,13 +11,14 @@ mod qemu;
11
11
mod tpm;
12
12
mod util;
13
13
14
- use crate :: opt:: TestOpt ;
14
+ use crate :: opt:: { FmtOpt , TestOpt } ;
15
15
use anyhow:: Result ;
16
16
use arch:: UefiArch ;
17
17
use cargo:: { Cargo , CargoAction , Feature , Package , TargetTypes } ;
18
18
use clap:: Parser ;
19
19
use itertools:: Itertools ;
20
20
use opt:: { Action , BuildOpt , ClippyOpt , DocOpt , Opt , QemuOpt , TpmVersion } ;
21
+ use std:: process:: Command ;
21
22
use util:: run_cmd;
22
23
23
24
fn build_feature_permutations ( opt : & BuildOpt ) -> Result < ( ) > {
@@ -206,6 +207,98 @@ fn run_host_tests(test_opt: &TestOpt) -> Result<()> {
206
207
run_cmd ( cargo. command ( ) ?)
207
208
}
208
209
210
+ /// Formats the project: nix, rust, and yml.
211
+ fn run_fmt_project ( fmt_opt : & FmtOpt ) -> Result < ( ) > {
212
+ // fmt rust
213
+ {
214
+ eprintln ! ( "Formatting: rust" ) ;
215
+ let mut command = Command :: new ( "cargo" ) ;
216
+ command. arg ( "fmt" ) ;
217
+ if fmt_opt. check {
218
+ command. arg ( "--check" ) ;
219
+ }
220
+ command
221
+ . arg ( "--all" )
222
+ . arg ( "--" )
223
+ . arg ( "--config" )
224
+ . arg ( "imports_granularity=Module" ) ;
225
+
226
+ match run_cmd ( command) {
227
+ Ok ( _) => {
228
+ eprintln ! ( "✅ rust files format" )
229
+ }
230
+ Err ( e) => {
231
+ if fmt_opt. check {
232
+ eprintln ! ( "❌ rust files to not pass check" ) ;
233
+ } else {
234
+ eprintln ! ( "❌ rust formatter failed: {e:#?}" ) ;
235
+ }
236
+ }
237
+ }
238
+ }
239
+
240
+ // fmt yml
241
+ if has_cmd ( "yamlfmt" ) {
242
+ eprintln ! ( "Formatting: yml" ) ;
243
+ let mut command = Command :: new ( "yamlfmt" ) ;
244
+ if fmt_opt. check {
245
+ command. arg ( "-lint" ) ;
246
+ }
247
+ command. arg ( "." ) ;
248
+
249
+ match run_cmd ( command) {
250
+ Ok ( _) => {
251
+ eprintln ! ( "✅ yml files format" )
252
+ }
253
+ Err ( e) => {
254
+ if fmt_opt. check {
255
+ eprintln ! ( "❌ yml files to not pass check" ) ;
256
+ } else {
257
+ eprintln ! ( "❌ yml formatter failed: {e:#?}" ) ;
258
+ }
259
+ }
260
+ }
261
+ } else {
262
+ eprintln ! ( "Formatting: yml - SKIPPED" ) ;
263
+ }
264
+
265
+ // fmt nix
266
+ if has_cmd ( "nixpkgs-fmt" ) {
267
+ eprintln ! ( "Formatting: nix" ) ;
268
+ let mut command = Command :: new ( "nixpkgs-fmt" ) ;
269
+ if fmt_opt. check {
270
+ command. arg ( "--check" ) ;
271
+ }
272
+ command. arg ( "." ) ;
273
+
274
+ match run_cmd ( command) {
275
+ Ok ( _) => {
276
+ eprintln ! ( "✅ nix files format" )
277
+ }
278
+ Err ( e) => {
279
+ if fmt_opt. check {
280
+ eprintln ! ( "❌ nix files to not pass check" ) ;
281
+ } else {
282
+ eprintln ! ( "❌ nix formatter failed: {e:#?}" ) ;
283
+ }
284
+ }
285
+ }
286
+ } else {
287
+ eprintln ! ( "Formatting: nix - SKIPPED" ) ;
288
+ }
289
+
290
+ Ok ( ( ) )
291
+ }
292
+
293
+ fn has_cmd ( target_cmd : & str ) -> bool {
294
+ #[ cfg( target_os = "windows" ) ]
295
+ let mut cmd = Command :: new ( "where" ) ;
296
+ #[ cfg( target_family = "unix" ) ]
297
+ let mut cmd = Command :: new ( "which" ) ;
298
+ cmd. arg ( target_cmd) ;
299
+ run_cmd ( cmd) . is_ok ( )
300
+ }
301
+
209
302
fn main ( ) -> Result < ( ) > {
210
303
let opt = Opt :: parse ( ) ;
211
304
@@ -218,5 +311,6 @@ fn main() -> Result<()> {
218
311
Action :: Miri ( _) => run_miri ( ) ,
219
312
Action :: Run ( qemu_opt) => run_vm_tests ( qemu_opt) ,
220
313
Action :: Test ( test_opt) => run_host_tests ( test_opt) ,
314
+ Action :: Fmt ( fmt_opt) => run_fmt_project ( fmt_opt) ,
221
315
}
222
316
}
0 commit comments