fn query_rustfmt_targets()

in tools/rustfmt/srcs/main.rs [18:72]


fn query_rustfmt_targets(options: &Config) -> Vec<String> {
    // Determine what packages to query
    let scope = match options.packages.is_empty() {
        true => "//...:all".to_owned(),
        false => {
            // Check to see if all the provided packages are actually targets
            let is_all_targets = options
                .packages
                .iter()
                .all(|pkg| match label::analyze(pkg) {
                    Ok(tgt) => tgt.name != "all",
                    Err(_) => false,
                });

            // Early return if a list of targets and not packages were provided
            if is_all_targets {
                return options.packages.clone();
            }

            options.packages.join(" + ")
        }
    };

    let query_args = vec![
        "query".to_owned(),
        format!(
            r#"kind('{types}', {scope}) except attr(tags, 'norustfmt', kind('{types}', {scope}))"#,
            types = "^rust_",
            scope = scope
        ),
    ];

    let child = Command::new(&options.bazel)
        .current_dir(&options.workspace)
        .args(query_args)
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()
        .expect("Failed to spawn bazel query command");

    let output = child
        .wait_with_output()
        .expect("Failed to wait on spawned command");

    if !output.status.success() {
        std::process::exit(output.status.code().unwrap_or(1));
    }

    str::from_utf8(&output.stdout)
        .expect("Invalid stream from command")
        .split('\n')
        .filter(|line| !line.is_empty())
        .map(|line| line.to_string())
        .collect()
}