Skip to content

Commit 0bd9be9

Browse files
committed
Implement default Validate base command and add ignore-audit flag
1 parent 8324b09 commit 0bd9be9

File tree

8 files changed

+80
-36
lines changed

8 files changed

+80
-36
lines changed

crates/tracel-xtask-macros/src/lib.rs

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// TODO: Check if it is possible to refactor all the macros using an inventory: https://crates.io/crates/inventory
1+
// TODO Check if it is possible to refactor all the macros using an inventory: https://crates.io/crates/inventory
22
// The idea would be to discover and register all the fields of base commands and all the variants of subcommand.
33
// If this is possible this should allow to generalize the command extension mechanism and make it even more useful.
44
extern crate proc_macro;
@@ -75,18 +75,17 @@ fn generate_dispatch_function(
7575
enum_ident: &syn::Ident,
7676
args: &Punctuated<Meta, Comma>,
7777
) -> TokenStream {
78-
let arms: Vec<proc_macro2::TokenStream> = args.iter().filter_map(|meta| {
78+
let arms: Vec<proc_macro2::TokenStream> = args.iter().map(|meta| {
7979
let cmd_ident = meta.path().get_ident().unwrap();
8080
let cmd_ident_string = cmd_ident.to_string();
8181
let module_ident = syn::Ident::new(cmd_ident_string.to_lowercase().as_str(), cmd_ident.span());
8282
match cmd_ident_string.as_str() {
83-
"Validate" => None,
84-
"Fix" => Some(quote! {
83+
"Fix" => quote! {
8584
#enum_ident::#cmd_ident(args) => base_commands::#module_ident::handle_command(args, None),
86-
}),
87-
_ => Some(quote! {
85+
},
86+
_ => quote! {
8887
#enum_ident::#cmd_ident(args) => base_commands::#module_ident::handle_command(args),
89-
})
88+
}
9089
}
9190
}).collect();
9291
let func = quote! {
@@ -182,7 +181,7 @@ pub fn base_commands(args: TokenStream, input: TokenStream) -> TokenStream {
182181
"Validate",
183182
quote! {
184183
#[doc = r"Validate the code base by running all the relevant checks and tests. Use this command before creating a new pull-request."]
185-
Validate
184+
Validate(tracel_xtask::commands::validate::ValidateCmdArgs)
186185
},
187186
);
188187
variant_map.insert("Vulnerabilities", quote! {
@@ -234,25 +233,43 @@ pub fn base_commands(args: TokenStream, input: TokenStream) -> TokenStream {
234233
// =================
235234

236235
fn get_additional_cmd_args_map() -> HashMap<&'static str, proc_macro2::TokenStream> {
237-
HashMap::from([(
238-
"TestCmdArgs",
239-
quote! {
240-
#[doc = r"Maximum number of parallel test crate compilations."]
241-
#[arg(
242-
long = "compilation-jobs",
243-
value_name = "NUMBER OF THREADS",
244-
required = false
245-
)]
246-
pub jobs: Option<u16>,
247-
#[doc = r"Maximum number of parallel test within a test crate execution."]
248-
#[arg(
249-
long = "test-threads",
250-
value_name = "NUMBER OF THREADS",
251-
required = false
252-
)]
253-
pub threads: Option<u16>,
254-
},
255-
)])
236+
HashMap::from([
237+
(
238+
"CheckCmdArgs",
239+
quote! {
240+
#[doc = r"Ignore audit errors."]
241+
#[arg(long = "ignore-audit", required = false)]
242+
pub ignore_audit: bool,
243+
},
244+
),
245+
(
246+
"TestCmdArgs",
247+
quote! {
248+
#[doc = r"Maximum number of parallel test crate compilations."]
249+
#[arg(
250+
long = "compilation-jobs",
251+
value_name = "NUMBER OF THREADS",
252+
required = false
253+
)]
254+
pub jobs: Option<u16>,
255+
#[doc = r"Maximum number of parallel test within a test crate execution."]
256+
#[arg(
257+
long = "test-threads",
258+
value_name = "NUMBER OF THREADS",
259+
required = false
260+
)]
261+
pub threads: Option<u16>,
262+
},
263+
),
264+
(
265+
"ValidateCmdArgs",
266+
quote! {
267+
#[doc = r"Ignore audit errors."]
268+
#[arg(long = "ignore-audit", required = false)]
269+
pub ignore_audit: bool,
270+
},
271+
),
272+
])
256273
}
257274

258275
// Returns a tuple where 0 is the actual struct and 1 is additional implementations
@@ -349,8 +366,11 @@ fn generate_command_args_struct(
349366
};
350367

351368
let additional_cmd_args_map = get_additional_cmd_args_map();
352-
let additional_fields = match additional_cmd_args_map.get(struct_name.to_string().as_str())
353-
{
369+
let mut base_command_type = struct_name.to_string();
370+
if args.len() == 3 {
371+
base_command_type = args.get(0).unwrap().path().get_ident().unwrap().to_string();
372+
}
373+
let additional_fields = match additional_cmd_args_map.get(base_command_type.as_str()) {
354374
Some(fields) => fields.clone(),
355375
None => quote! {},
356376
};
@@ -454,7 +474,15 @@ fn generate_command_args_tryinto(args: TokenStream, input: TokenStream) -> Token
454474
.filter_map(|f| {
455475
f.ident.as_ref().map(|ident| {
456476
let ident_str = ident.to_string();
457-
if ident_str != "target" && (ident_str == "exclude" || ident_str == "only") {
477+
// TODO this hardcoded predicates are awful, they should be unneccesarry if
478+
// we can use an inventory (see TODO at the top of the file)
479+
if ident_str != "target"
480+
&& (ident_str == "exclude"
481+
|| ident_str == "only"
482+
|| ident_str == "ignore_audit"
483+
|| ident_str == "jobs"
484+
|| ident_str == "threads")
485+
{
458486
quote! { #ident: self.#ident, }
459487
} else {
460488
quote! {}

crates/tracel-xtask/src/commands/check.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub fn handle_command(args: CheckCmdArgs) -> anyhow::Result<()> {
2222
}
2323

2424
match args.get_command() {
25+
CheckSubCommand::Audit if args.ignore_audit => {
26+
if run_audit().is_err() {
27+
warn!("Ignoring audit error because of '--ignore-audit' flag.");
28+
}
29+
Ok(())
30+
}
2531
CheckSubCommand::Audit => run_audit(),
2632
CheckSubCommand::Format => run_format(&args.target, &args.exclude, &args.only),
2733
CheckSubCommand::Lint => run_lint(&args.target, &args.exclude, &args.only),
@@ -34,6 +40,7 @@ pub fn handle_command(args: CheckCmdArgs) -> anyhow::Result<()> {
3440
target: args.target.clone(),
3541
exclude: args.exclude.clone(),
3642
only: args.only.clone(),
43+
ignore_audit: args.ignore_audit,
3744
})
3845
}),
3946
}

crates/tracel-xtask/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod doc;
88
pub mod fix;
99
pub mod publish;
1010
pub mod test;
11+
pub mod validate;
1112
pub mod vulnerabilities;
1213

1314
// use crate::declare_target;

xtask/src/commands/validate.rs renamed to crates/tracel-xtask/src/commands/validate.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
use tracel_xtask::prelude::*;
1+
use super::{
2+
check::{CheckCmdArgs, CheckSubCommand},
3+
test::{TestCmdArgs, TestSubCommand},
4+
Target,
5+
};
26

3-
pub fn handle_command() -> anyhow::Result<()> {
7+
#[tracel_xtask_macros::declare_command_args(None, None)]
8+
struct ValidateCmdArgs {}
9+
10+
pub fn handle_command(args: ValidateCmdArgs) -> anyhow::Result<()> {
411
let target = Target::Workspace;
512
let exclude = vec![];
613
let only = vec![];
@@ -14,16 +21,17 @@ pub fn handle_command() -> anyhow::Result<()> {
1421
]
1522
.iter()
1623
.try_for_each(|c| {
17-
base_commands::check::handle_command(CheckCmdArgs {
24+
super::check::handle_command(CheckCmdArgs {
1825
target: target.clone(),
1926
exclude: exclude.clone(),
2027
only: only.clone(),
2128
command: Some(c.clone()),
29+
ignore_audit: args.ignore_audit,
2230
})
2331
})?;
2432

2533
// tests
26-
base_commands::test::handle_command(TestCmdArgs {
34+
super::test::handle_command(TestCmdArgs {
2735
target: target.clone(),
2836
exclude: exclude.clone(),
2937
only: only.clone(),

crates/tracel-xtask/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub mod prelude {
3838
pub use crate::commands::publish::PublishCmdArgs;
3939
pub use crate::commands::test::TestCmdArgs;
4040
pub use crate::commands::test::TestSubCommand;
41+
pub use crate::commands::validate::ValidateCmdArgs;
4142
pub use crate::commands::vulnerabilities::VulnerabilitiesCmdArgs;
4243
pub use crate::commands::vulnerabilities::VulnerabilitiesSubCommand;
4344
pub use crate::commands::Target;

xtask/src/commands/extended_check_sub_commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn handle_command(args: ExtendedCheckArgsCmdArgs) -> anyhow::Result<()> {
2525
target: args.target.clone(),
2626
exclude: args.exclude.clone(),
2727
only: args.only.clone(),
28+
ignore_audit: args.ignore_audit,
2829
})
2930
})
3031
}

xtask/src/commands/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ pub(crate) mod extended_target;
55
pub(crate) mod fix;
66
pub(crate) mod my_command;
77
pub(crate) mod my_command_with_sub_commands;
8-
pub(crate) mod validate;

xtask/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ fn main() -> anyhow::Result<()> {
5656
Command::MyCommandWithSubCommand(args) => {
5757
commands::my_command_with_sub_commands::handle_command(args)
5858
}
59-
Command::Validate => commands::validate::handle_command(),
6059
// dispatch_base_commands function is generated by the commands macro
6160
_ => dispatch_base_commands(args),
6261
}?;

0 commit comments

Comments
 (0)