Move cmd to a separate dir

This commit is contained in:
Aleksey Kladov 2020-01-07 15:33:09 +01:00
parent 91f9bc2b86
commit 64f0510d18
2 changed files with 58 additions and 53 deletions

53
xtask/src/cmd.rs Normal file
View File

@ -0,0 +1,53 @@
use std::process::{Command, Output, Stdio};
use anyhow::{Context, Result};
use crate::project_root;
pub struct Cmd<'a> {
pub unix: &'a str,
pub windows: &'a str,
pub work_dir: &'a str,
}
impl Cmd<'_> {
pub fn run(self) -> Result<()> {
if cfg!(windows) {
run(self.windows, self.work_dir)
} else {
run(self.unix, self.work_dir)
}
}
pub fn run_with_output(self) -> Result<Output> {
if cfg!(windows) {
run_with_output(self.windows, self.work_dir)
} else {
run_with_output(self.unix, self.work_dir)
}
}
}
pub fn run(cmdline: &str, dir: &str) -> Result<()> {
do_run(cmdline, dir, &mut |c| {
c.stdout(Stdio::inherit());
})
.map(|_| ())
}
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
do_run(cmdline, dir, &mut |_| {})
}
fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result<Output> {
eprintln!("\nwill run: {}", cmdline);
let proj_dir = project_root().join(dir);
let mut args = cmdline.split_whitespace();
let exec = args.next().unwrap();
let mut cmd = Command::new(exec);
f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
if !output.status.success() {
anyhow::bail!("`{}` exited with {}", cmdline, output.status);
}
Ok(output)
}

View File

@ -1,20 +1,23 @@
//! FIXME: write short doc here //! FIXME: write short doc here
mod cmd;
pub mod codegen; pub mod codegen;
pub mod install; pub mod install;
pub mod pre_commit; pub mod pre_commit;
mod ast_src; mod ast_src;
use anyhow::Context; use anyhow::Context;
pub use anyhow::Result;
use std::{ use std::{
env, env,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, Output, Stdio}, process::{Command, Stdio},
}; };
use crate::codegen::Mode; use crate::codegen::Mode;
pub use anyhow::Result;
pub use cmd::{run, run_with_output, Cmd};
const TOOLCHAIN: &str = "stable"; const TOOLCHAIN: &str = "stable";
pub fn project_root() -> PathBuf { pub fn project_root() -> PathBuf {
@ -27,40 +30,6 @@ pub fn project_root() -> PathBuf {
.to_path_buf() .to_path_buf()
} }
pub struct Cmd<'a> {
pub unix: &'a str,
pub windows: &'a str,
pub work_dir: &'a str,
}
impl Cmd<'_> {
pub fn run(self) -> Result<()> {
if cfg!(windows) {
run(self.windows, self.work_dir)
} else {
run(self.unix, self.work_dir)
}
}
pub fn run_with_output(self) -> Result<Output> {
if cfg!(windows) {
run_with_output(self.windows, self.work_dir)
} else {
run_with_output(self.unix, self.work_dir)
}
}
}
pub fn run(cmdline: &str, dir: &str) -> Result<()> {
do_run(cmdline, dir, |c| {
c.stdout(Stdio::inherit());
})
.map(|_| ())
}
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
do_run(cmdline, dir, |_| {})
}
pub fn run_rustfmt(mode: Mode) -> Result<()> { pub fn run_rustfmt(mode: Mode) -> Result<()> {
match Command::new("rustup") match Command::new("rustup")
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
@ -132,20 +101,3 @@ pub fn run_fuzzer() -> Result<()> {
run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
} }
fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
where
F: FnMut(&mut Command),
{
eprintln!("\nwill run: {}", cmdline);
let proj_dir = project_root().join(dir);
let mut args = cmdline.split_whitespace();
let exec = args.next().unwrap();
let mut cmd = Command::new(exec);
f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
if !output.status.success() {
anyhow::bail!("`{}` exited with {}", cmdline, output.status);
}
Ok(output)
}