From 44f39305cb7b7f5a92837ddc55333d7470060eb8 Mon Sep 17 00:00:00 2001 From: messense Date: Thu, 26 Oct 2023 19:18:14 +0800 Subject: [PATCH] Add support for external cargo subcommand --- src/bin/cargo-xwin.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/bin/cargo-xwin.rs b/src/bin/cargo-xwin.rs index 853b808..b8bfe7a 100644 --- a/src/bin/cargo-xwin.rs +++ b/src/bin/cargo-xwin.rs @@ -1,5 +1,7 @@ -use anyhow::Context; -use cargo_options::Metadata; +use std::env; +use std::ffi::OsString; +use std::process::Command; + use cargo_xwin::{Build, Check, Clippy, Run, Rustc, Test}; use clap::{Parser, Subcommand}; @@ -21,14 +23,14 @@ pub enum Opt { Build(Build), Check(Check), Clippy(Clippy), - #[command(name = "metadata")] - Metadata(Metadata), #[command(name = "run", alias = "r")] Run(Run), #[command(name = "rustc")] Rustc(Rustc), #[command(name = "test", alias = "t")] Test(Test), + #[command(external_subcommand)] + External(Vec), } fn main() -> anyhow::Result<()> { @@ -38,21 +40,21 @@ fn main() -> anyhow::Result<()> { match cli { Cli::Opt(opt) | Cli::Cargo(opt) => match opt { Opt::Build(build) => build.execute()?, - Opt::Metadata(metadata) => { - let mut cmd = metadata.command(); - let mut child = cmd.spawn().context("Failed to run cargo metadata")?; - let status = child - .wait() - .expect("Failed to wait on cargo metadata process"); - if !status.success() { - std::process::exit(status.code().unwrap_or(1)); - } - } Opt::Run(run) => run.execute()?, Opt::Rustc(rustc) => rustc.execute()?, Opt::Test(test) => test.execute()?, Opt::Check(check) => check.execute()?, Opt::Clippy(clippy) => clippy.execute()?, + Opt::External(args) => { + let mut child = Command::new(env::var_os("CARGO").unwrap_or("cargo".into())) + .args(args) + .env_remove("CARGO") + .spawn()?; + let status = child.wait().expect("Failed to wait on cargo process"); + if !status.success() { + std::process::exit(status.code().unwrap_or(1)); + } + } }, } Ok(())