From 0e072412802f5bdad3fc472fa0f158ecd8c90e94 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 14 Jun 2025 23:19:54 +0300 Subject: [PATCH] xtask: add completion generation --- .cargo/config.toml | 2 ++ Cargo.lock | 29 +++++++++++++++++++ Cargo.toml | 28 ++++++++++--------- watt/lib.rs | 17 +++++++++--- xtask/Cargo.toml | 19 +++++++++++++ xtask/main.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..35049cb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/Cargo.lock b/Cargo.lock index bb5a94f..34c4038 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,25 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aad5b1b4de04fead402672b48897030eec1f3bfe1550776322f59f6d6e6a5677" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_complete_nushell" +version = "4.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb8335b398d197fb3176efe9400c6c053a41733c26794316c73423d212b2f3d" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.32" @@ -625,6 +644,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "xtask" +version = "0.4.0" +dependencies = [ + "clap", + "clap_complete", + "clap_complete_nushell", + "watt", +] + [[package]] name = "yansi" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index 92845d7..0deee8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ "watt" ] +members = [ "watt", "xtask" ] resolver = "3" [workspace.package] @@ -12,15 +12,17 @@ rust-version = "1.85" version = "0.4.0" [workspace.dependencies] -anyhow = "1.0" -clap = { version = "4.0", features = [ "derive", "env" ] } -clap-verbosity-flag = "3.0.2" -ctrlc = "3.4" -derive_more = { version = "2.0.1", features = [ "full" ] } -env_logger = "0.11" -log = "0.4" -num_cpus = "1.16" -serde = { version = "1.0", features = [ "derive" ] } -thiserror = "2.0" -toml = "0.8" -yansi = { version = "1.0.1", features = [ "detect-env", "detect-tty" ] } +anyhow = "1.0" +clap = { version = "4.0", features = [ "derive", "env" ] } +clap-verbosity-flag = "3.0.2" +clap_complete = "4.5.54" +clap_complete_nushell = "4.5.7" +ctrlc = "3.4" +derive_more = { version = "2.0.1", features = [ "full" ] } +env_logger = "0.11" +log = "0.4" +num_cpus = "1.16" +serde = { version = "1.0", features = [ "derive" ] } +thiserror = "2.0" +toml = "0.8" +yansi = { version = "1.0.1", features = [ "detect-env", "detect-tty" ] } diff --git a/watt/lib.rs b/watt/lib.rs index 6b84a5b..62b1489 100644 --- a/watt/lib.rs +++ b/watt/lib.rs @@ -27,9 +27,8 @@ pub enum Command { #[command(flatten)] verbosity: clap_verbosity_flag::Verbosity, - /// The daemon config path. - #[arg(long, env = "WATT_CONFIG")] - config: Option, + #[clap(flatten)] + command: WattCommand, }, /// CPU metadata and modification utility. @@ -51,6 +50,13 @@ pub enum Command { }, } +#[derive(clap::Parser, Debug)] +pub struct WattCommand { + /// The daemon config path. + #[arg(long, env = "WATT_CONFIG")] + config: Option, +} + #[derive(clap::Parser, Debug)] pub enum CpuCommand { /// Modify CPU attributes. @@ -79,7 +85,10 @@ pub fn main() -> anyhow::Result<()> { .init(); match cli.command { - Command::Watt { config, .. } => { + Command::Watt { + command: WattCommand { config }, + .. + } => { let config = config::DaemonConfig::load_from(config.as_deref()) .context("failed to load daemon config")?; diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..b2c5bef --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "xtask" +description.workspace = true +version.workspace = true +edition.workspace = true +authors.workspace = true +rust-version.workspace = true +publish = false + +[[bin]] +name = "xtask" +path = "main.rs" + +[dependencies] +watt.path = "../watt" + +clap.workspace = true +clap_complete.workspace = true +clap_complete_nushell.workspace = true diff --git a/xtask/main.rs b/xtask/main.rs new file mode 100644 index 0000000..565463e --- /dev/null +++ b/xtask/main.rs @@ -0,0 +1,69 @@ +use std::io; + +use clap::{ + CommandFactory, + Parser as _, +}; + +#[derive(clap::Parser)] +struct Cli { + #[clap(subcommand)] + command: Command, +} + +#[derive(clap::Subcommand)] +enum Command { + /// Generate completions for the specified shell. + GenerateCompletions { + #[arg(long)] + shell: Shell, + + #[arg(long)] + binary: Binary, + }, +} + +#[expect(clippy::enum_variant_names)] +#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)] +enum Shell { + Bash, + Elvish, + Fish, + PowerShell, + Zsh, + Nushell, +} + +#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)] +enum Binary { + Watt, + Cpu, + Power, +} + +fn main() { + let cli = Cli::parse(); + + match cli.command { + Command::GenerateCompletions { shell, binary } => { + let mut command = match binary { + Binary::Watt => watt::WattCommand::command(), + Binary::Cpu => watt::CpuCommand::command(), + Binary::Power => watt::PowerCommand::command(), + }; + command.set_bin_name(format!("{binary:?}").to_lowercase()); + command.build(); + + let shell: &dyn clap_complete::Generator = match shell { + Shell::Bash => &clap_complete::Shell::Bash, + Shell::Elvish => &clap_complete::Shell::Elvish, + Shell::Fish => &clap_complete::Shell::Fish, + Shell::PowerShell => &clap_complete::Shell::PowerShell, + Shell::Zsh => &clap_complete::Shell::Zsh, + Shell::Nushell => &clap_complete_nushell::Nushell, + }; + + shell.generate(&command, &mut io::stdout()); + }, + } +}