support verbose & quiet flag

close #25
This commit is contained in:
Zeyi Fan 2020-06-14 21:56:58 -07:00
parent db6b67fd11
commit 65db163b1f
4 changed files with 59 additions and 21 deletions

View file

@ -59,19 +59,19 @@ fn main() -> Result<(), CargoPlayError> {
rmtemp(&temp);
}
mktemp(&temp);
write_cargo_toml(&temp, package_name, dependencies, opt.edition, infers)?;
write_cargo_toml(
&temp,
package_name,
dependencies,
opt.edition.clone(),
infers,
)?;
copy_sources(&temp, &opt.src)?;
let end = if let Some(save) = opt.save {
copy_project(&temp, &save)?
} else {
run_cargo_build(
opt.toolchain,
&temp,
opt.release,
opt.cargo_option,
&opt.args,
)?
run_cargo_build(&opt, &temp)?
};
match end.code() {

View file

@ -7,7 +7,7 @@ use structopt::StructOpt;
use crate::errors::CargoPlayError;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum RustEdition {
E2015,
E2018,
@ -50,17 +50,21 @@ impl Default for RustEdition {
pub struct Opt {
#[structopt(short = "d", long = "debug", hidden = true)]
pub debug: bool,
#[structopt(short = "c", long = "clean")]
/// Rebuild the cargo project without the cache from previous run
pub clean: bool,
#[structopt(short = "t", long = "toolchain", hidden = true)]
pub toolchain: Option<String>,
#[structopt(
parse(try_from_os_str = "osstr_to_abspath"),
raw(required = "true", validator = "file_exist")
)]
/// Paths to your source code files
pub src: Vec<PathBuf>,
#[structopt(
short = "e",
long = "edition",
@ -69,20 +73,34 @@ pub struct Opt {
)]
/// Specify Rust edition
pub edition: RustEdition,
#[structopt(long = "release")]
/// Build program in release mode
pub release: bool,
#[structopt(long = "cached", hidden = true)]
pub cached: bool,
#[structopt(long = "quiet")]
/// Disable output from Cargo (equivlant to `cargo run --quiet`)
pub quiet: bool,
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
/// Set Cargo verbose level
pub verbose: u16,
#[structopt(long = "cargo-option")]
/// Custom flags passing to cargo
pub cargo_option: Option<String>,
#[structopt(long = "save")]
/// Generate a Cargo project based on inputs
pub save: Option<PathBuf>,
/// [experimental] Automatically infers dependency
#[structopt(long = "infer", short = "i")]
pub infer: bool,
#[structopt(multiple = true, last = true)]
/// Arguments passed to the underlying program
pub args: Vec<String>,

View file

@ -12,7 +12,7 @@ use pathdiff::diff_paths;
use crate::cargo::CargoManifest;
use crate::errors::CargoPlayError;
use crate::opt::RustEdition;
use crate::opt::{Opt, RustEdition};
pub fn parse_inputs(inputs: &[PathBuf]) -> Result<Vec<String>, CargoPlayError> {
inputs
@ -118,16 +118,10 @@ pub fn copy_sources(temp: &PathBuf, sources: &[PathBuf]) -> Result<(), CargoPlay
Ok(())
}
pub fn run_cargo_build(
toolchain: Option<String>,
project: &PathBuf,
release: bool,
cargo_option: Option<String>,
program_args: &[String],
) -> Result<ExitStatus, CargoPlayError> {
pub fn run_cargo_build(options: &Opt, project: &PathBuf) -> Result<ExitStatus, CargoPlayError> {
let mut cargo = Command::new("cargo");
if let Some(toolchain) = toolchain {
if let Some(toolchain) = options.toolchain.as_ref() {
cargo.arg(format!("+{}", toolchain));
}
@ -136,18 +130,28 @@ pub fn run_cargo_build(
.arg("--manifest-path")
.arg(project.join("Cargo.toml"));
if let Some(cargo_option) = cargo_option {
if let Some(cargo_option) = options.cargo_option.as_ref() {
// FIXME: proper escaping
cargo.args(cargo_option.split_ascii_whitespace());
}
if release {
if options.release {
cargo.arg("--release");
}
if options.quiet {
cargo.arg("--quiet");
}
if options.verbose != 0 {
for _ in 0..options.verbose {
cargo.arg("-v");
}
}
cargo
.arg("--")
.args(program_args)
.args(options.args.clone())
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.status()

View file

@ -175,6 +175,22 @@ fn release_mode() -> Result<()> {
Ok(())
}
#[test]
fn quiet_mode() -> Result<()> {
let rt = TestRuntime::new()?;
let output = rt.run(&["--quiet", "fixtures/hello.rs"])?;
assert!(!output.stderr.contains("Running"));
Ok(())
}
#[test]
fn verbose_mode() -> Result<()> {
let rt = TestRuntime::new()?;
let output = rt.run(&["-v", "fixtures/hello.rs"])?;
assert!(output.stderr.contains("rustc"));
Ok(())
}
#[test]
fn cargo_option() -> Result<()> {
let rt = TestRuntime::new()?;