|
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 |
2 | 2 | // The idea would be to discover and register all the fields of base commands and all the variants of subcommand.
|
3 | 3 | // If this is possible this should allow to generalize the command extension mechanism and make it even more useful.
|
4 | 4 | extern crate proc_macro;
|
@@ -75,18 +75,17 @@ fn generate_dispatch_function(
|
75 | 75 | enum_ident: &syn::Ident,
|
76 | 76 | args: &Punctuated<Meta, Comma>,
|
77 | 77 | ) -> TokenStream {
|
78 |
| - let arms: Vec<proc_macro2::TokenStream> = args.iter().filter_map(|meta| { |
| 78 | + let arms: Vec<proc_macro2::TokenStream> = args.iter().map(|meta| { |
79 | 79 | let cmd_ident = meta.path().get_ident().unwrap();
|
80 | 80 | let cmd_ident_string = cmd_ident.to_string();
|
81 | 81 | let module_ident = syn::Ident::new(cmd_ident_string.to_lowercase().as_str(), cmd_ident.span());
|
82 | 82 | match cmd_ident_string.as_str() {
|
83 |
| - "Validate" => None, |
84 |
| - "Fix" => Some(quote! { |
| 83 | + "Fix" => quote! { |
85 | 84 | #enum_ident::#cmd_ident(args) => base_commands::#module_ident::handle_command(args, None),
|
86 |
| - }), |
87 |
| - _ => Some(quote! { |
| 85 | + }, |
| 86 | + _ => quote! { |
88 | 87 | #enum_ident::#cmd_ident(args) => base_commands::#module_ident::handle_command(args),
|
89 |
| - }) |
| 88 | + } |
90 | 89 | }
|
91 | 90 | }).collect();
|
92 | 91 | let func = quote! {
|
@@ -182,7 +181,7 @@ pub fn base_commands(args: TokenStream, input: TokenStream) -> TokenStream {
|
182 | 181 | "Validate",
|
183 | 182 | quote! {
|
184 | 183 | #[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) |
186 | 185 | },
|
187 | 186 | );
|
188 | 187 | variant_map.insert("Vulnerabilities", quote! {
|
@@ -234,25 +233,43 @@ pub fn base_commands(args: TokenStream, input: TokenStream) -> TokenStream {
|
234 | 233 | // =================
|
235 | 234 |
|
236 | 235 | 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 | + ]) |
256 | 273 | }
|
257 | 274 |
|
258 | 275 | // Returns a tuple where 0 is the actual struct and 1 is additional implementations
|
@@ -349,8 +366,11 @@ fn generate_command_args_struct(
|
349 | 366 | };
|
350 | 367 |
|
351 | 368 | 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()) { |
354 | 374 | Some(fields) => fields.clone(),
|
355 | 375 | None => quote! {},
|
356 | 376 | };
|
@@ -454,7 +474,15 @@ fn generate_command_args_tryinto(args: TokenStream, input: TokenStream) -> Token
|
454 | 474 | .filter_map(|f| {
|
455 | 475 | f.ident.as_ref().map(|ident| {
|
456 | 476 | 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 | + { |
458 | 486 | quote! { #ident: self.#ident, }
|
459 | 487 | } else {
|
460 | 488 | quote! {}
|
|
0 commit comments