|
| 1 | +use clap::{App, Arg, ArgMatches}; |
| 2 | +use std::env; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +fn get_clap_config<'a>() -> ArgMatches<'a> { |
| 6 | + App::new("lintcheck") |
| 7 | + .about("run clippy on a set of crates and check output") |
| 8 | + .arg( |
| 9 | + Arg::with_name("only") |
| 10 | + .takes_value(true) |
| 11 | + .value_name("CRATE") |
| 12 | + .long("only") |
| 13 | + .help("Only process a single crate of the list"), |
| 14 | + ) |
| 15 | + .arg( |
| 16 | + Arg::with_name("crates-toml") |
| 17 | + .takes_value(true) |
| 18 | + .value_name("CRATES-SOURCES-TOML-PATH") |
| 19 | + .long("crates-toml") |
| 20 | + .help("Set the path for a crates.toml where lintcheck should read the sources from"), |
| 21 | + ) |
| 22 | + .arg( |
| 23 | + Arg::with_name("threads") |
| 24 | + .takes_value(true) |
| 25 | + .value_name("N") |
| 26 | + .short("j") |
| 27 | + .long("jobs") |
| 28 | + .help("Number of threads to use, 0 automatic choice"), |
| 29 | + ) |
| 30 | + .arg( |
| 31 | + Arg::with_name("fix") |
| 32 | + .long("--fix") |
| 33 | + .help("Runs cargo clippy --fix and checks if all suggestions apply"), |
| 34 | + ) |
| 35 | + .arg( |
| 36 | + Arg::with_name("filter") |
| 37 | + .long("--filter") |
| 38 | + .takes_value(true) |
| 39 | + .multiple(true) |
| 40 | + .value_name("clippy_lint_name") |
| 41 | + .help("Apply a filter to only collect specified lints, this also overrides `allow` attributes"), |
| 42 | + ) |
| 43 | + .arg( |
| 44 | + Arg::with_name("markdown") |
| 45 | + .long("--markdown") |
| 46 | + .help("Change the reports table to use markdown links"), |
| 47 | + ) |
| 48 | + .get_matches() |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug)] |
| 52 | +pub(crate) struct LintcheckConfig { |
| 53 | + /// max number of jobs to spawn (default 1) |
| 54 | + pub max_jobs: usize, |
| 55 | + /// we read the sources to check from here |
| 56 | + pub sources_toml_path: PathBuf, |
| 57 | + /// we save the clippy lint results here |
| 58 | + pub lintcheck_results_path: PathBuf, |
| 59 | + /// Check only a specified package |
| 60 | + pub only: Option<String>, |
| 61 | + /// whether to just run --fix and not collect all the warnings |
| 62 | + pub fix: bool, |
| 63 | + /// A list of lints that this lintcheck run should focus on |
| 64 | + pub lint_filter: Vec<String>, |
| 65 | + /// Indicate if the output should support markdown syntax |
| 66 | + pub markdown: bool, |
| 67 | +} |
| 68 | + |
| 69 | +impl LintcheckConfig { |
| 70 | + pub fn new() -> Self { |
| 71 | + let clap_config = get_clap_config(); |
| 72 | + |
| 73 | + // first, check if we got anything passed via the LINTCHECK_TOML env var, |
| 74 | + // if not, ask clap if we got any value for --crates-toml <foo> |
| 75 | + // if not, use the default "lintcheck/lintcheck_crates.toml" |
| 76 | + let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| { |
| 77 | + clap_config |
| 78 | + .value_of("crates-toml") |
| 79 | + .clone() |
| 80 | + .unwrap_or("lintcheck/lintcheck_crates.toml") |
| 81 | + .to_string() |
| 82 | + }); |
| 83 | + |
| 84 | + let markdown = clap_config.is_present("markdown"); |
| 85 | + let sources_toml_path = PathBuf::from(sources_toml); |
| 86 | + |
| 87 | + // for the path where we save the lint results, get the filename without extension (so for |
| 88 | + // wasd.toml, use "wasd"...) |
| 89 | + let filename: PathBuf = sources_toml_path.file_stem().unwrap().into(); |
| 90 | + let lintcheck_results_path = PathBuf::from(format!( |
| 91 | + "lintcheck-logs/{}_logs.{}", |
| 92 | + filename.display(), |
| 93 | + if markdown { "md" } else { "txt" } |
| 94 | + )); |
| 95 | + |
| 96 | + // look at the --threads arg, if 0 is passed, ask rayon rayon how many threads it would spawn and |
| 97 | + // use half of that for the physical core count |
| 98 | + // by default use a single thread |
| 99 | + let max_jobs = match clap_config.value_of("threads") { |
| 100 | + Some(threads) => { |
| 101 | + let threads: usize = threads |
| 102 | + .parse() |
| 103 | + .unwrap_or_else(|_| panic!("Failed to parse '{}' to a digit", threads)); |
| 104 | + if threads == 0 { |
| 105 | + // automatic choice |
| 106 | + // Rayon seems to return thread count so half that for core count |
| 107 | + (rayon::current_num_threads() / 2) as usize |
| 108 | + } else { |
| 109 | + threads |
| 110 | + } |
| 111 | + }, |
| 112 | + // no -j passed, use a single thread |
| 113 | + None => 1, |
| 114 | + }; |
| 115 | + |
| 116 | + let lint_filter: Vec<String> = clap_config |
| 117 | + .values_of("filter") |
| 118 | + .map(|iter| { |
| 119 | + iter.map(|lint_name| { |
| 120 | + let mut filter = lint_name.replace('_', "-"); |
| 121 | + if !filter.starts_with("clippy::") { |
| 122 | + filter.insert_str(0, "clippy::"); |
| 123 | + } |
| 124 | + filter |
| 125 | + }) |
| 126 | + .collect() |
| 127 | + }) |
| 128 | + .unwrap_or_default(); |
| 129 | + |
| 130 | + LintcheckConfig { |
| 131 | + max_jobs, |
| 132 | + sources_toml_path, |
| 133 | + lintcheck_results_path, |
| 134 | + only: clap_config.value_of("only").map(String::from), |
| 135 | + fix: clap_config.is_present("fix"), |
| 136 | + lint_filter, |
| 137 | + markdown, |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments