Skip to content

Commit

Permalink
Add support for external cargo subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 26, 2023
1 parent 28cd46e commit 44f3930
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/bin/cargo-xwin.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<OsString>),
}

fn main() -> anyhow::Result<()> {
Expand All @@ -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(())
Expand Down

0 comments on commit 44f3930

Please sign in to comment.