Skip to content

Commit

Permalink
feat: Improved 'xtask' experience, removed old commands.
Browse files Browse the repository at this point in the history
This commit removes some minimally useful commands from `xtask`, and
also rewrites the `xtask` CLI and printing to be more consistent, more
helpful, and easier to control.

We now use `clap-verbosity-flag` to auto-handle verbosity through the
`log` crate, and all printing (outside of auto-printing by `duct`) is
handled through `log` and `env_logger`, with `clap-verbosity-flag`
applying filtering based on CLI flags.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker committed May 10, 2024
1 parent 4e37ed9 commit f0a5a57
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 606 deletions.
238 changes: 124 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ edition = "2021"
publish = false

[dependencies]
clap = { version = "4.5.4", default-features = false, features = [
"cargo",
"std",
] }
anyhow = "1.0.83"
pathbuf = "1.0.0"
clap = { version = "4.5.4", features = ["cargo", "derive"] }
clap-verbosity-flag = "2.2.0"
duct = "0.13.5"
env_logger = "0.11.3"
log = "0.4.21"
glob = "0.3.0"
pathbuf = "1.0.0"
serde = { version = "1.0.133", features = ["derive"] }
toml = "0.8.12"
which = "6.0.1"
duct = "0.13.5"
open = "5.1.2"
9 changes: 0 additions & 9 deletions xtask/src/exit.rs

This file was deleted.

141 changes: 24 additions & 117 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,132 +1,39 @@
// SPDX-License-Identifier: Apache-2.0

mod exit;
mod task;
mod workspace;

use crate::exit::EXIT_FAILURE;
use crate::exit::EXIT_SUCCESS;
use crate::task::doc::OpenDoc;
use anyhow::Error;
use anyhow::Result;
use clap::crate_version;
use clap::Arg;
use clap::ArgAction;
use clap::ArgMatches;
use clap::Command;
use std::process::exit;
use clap::Parser;
use std::process::ExitCode;

fn main() {
let matches = Command::new("xtask")
.about("Hipcheck development task runner.")
.version(crate_version!())
.disable_version_flag(true)
.arg(
Arg::new("help")
.short('h')
.long("help")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("version")
.short('V')
.long("version")
.action(ArgAction::SetTrue),
)
.subcommand(Command::new("ci"))
.subcommand(
Command::new("doc").arg(
Arg::new("open")
.short('o')
.long("open")
.action(ArgAction::SetTrue),
),
)
.subcommand(
Command::new("bench").arg(
Arg::new("build")
.value_name("build")
.index(1)
.default_value(""),
),
)
.subcommand(Command::new("install"))
.subcommand(Command::new("validate"))
.get_matches();
fn main() -> ExitCode {
let args = Args::parse();

if matches.get_flag("help") {
print_help();
}

if matches.get_flag("version") {
print_version();
}

if let Err(err) = dispatch(matches) {
print_error(err);
exit(EXIT_FAILURE);
}
}

fn print_error(err: Error) {
let mut chain = err.chain();

// PANIC: First error is guaranteed to be present.
eprintln!("error: {}", chain.next().unwrap());
env_logger::Builder::new()
.filter_level(args.verbose.log_level_filter())
.init();

for err in chain {
eprintln!(" {}", err);
match args.command {
Commands::Validate => task::validate::run(),
Commands::Ci => task::ci::run(),
}
}

fn dispatch(matches: ArgMatches) -> Result<()> {
match matches.subcommand() {
Some(("validate", _)) => task::validate::run(),
Some(("install", _)) => task::install::run(),
Some(("ci", _)) => task::ci::run(),
Some(("bench", _)) => task::bench::build(),
Some(("doc", doc)) => {
// PANIC: Should be safe to unwrap, because there is a default value
let open = OpenDoc::from(doc.get_flag("open"));
task::doc::run(open)
}
Some((_, _)) | None => print_help(),
}
}

fn print_help() -> ! {
let raw_version = env!("CARGO_PKG_VERSION", "can't find xtask package version");

let help_text = format!(
"\
cargo {} {}
{}
USAGE:
cargo {} [FLAGS] [<TASK>]
FLAGS:
-h, --help print help information
-v, --version print version information
TASKS:
ci simulate a CI run locally
doc [open] generate docs for all crates in the workspace
install install hipcheck
bench [build] run time benchmarks to get duration on events
validate analyze workspace for expected configuration",
env!("CARGO_PKG_NAME"),
raw_version,
env!("CARGO_PKG_DESCRIPTION"),
env!("CARGO_BIN_NAME")
);
/// Hipcheck development task runner.
#[derive(Debug, clap::Parser)]
#[clap(about, version)]
struct Args {
#[clap(flatten)]
verbose: clap_verbosity_flag::Verbosity,

println!("{}", help_text);
exit(EXIT_FAILURE);
#[clap(subcommand)]
command: Commands,
}

fn print_version() -> ! {
let version_text = crate_version!();
println!("{}", version_text);
exit(EXIT_SUCCESS);
#[derive(Debug, clap::Subcommand)]
enum Commands {
/// Run a variety of quality checks.
Validate,
/// Simulate a CI run locally.
Ci,
}
52 changes: 0 additions & 52 deletions xtask/src/task/bench.rs

This file was deleted.

Loading

0 comments on commit f0a5a57

Please sign in to comment.