rust/xtask/src/lib.rs

169 lines
4.5 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-10-23 18:13:40 +03:00
pub mod codegen;
2019-08-18 21:33:31 +03:00
use anyhow::Context;
pub use anyhow::Result;
use std::{
env, fs,
io::{Error as IoError, ErrorKind},
2018-10-16 00:54:27 +06:00
path::{Path, PathBuf},
2019-03-05 18:19:36 -03:00
process::{Command, Output, Stdio},
};
2019-10-23 18:13:40 +03:00
use crate::codegen::Mode;
2019-08-18 21:34:55 +03:00
2019-01-24 07:58:38 -05:00
const TOOLCHAIN: &str = "stable";
2018-10-16 00:54:27 +06:00
pub fn project_root() -> PathBuf {
2019-11-01 22:20:44 +02:00
Path::new(
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
)
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
2018-10-16 00:54:27 +06:00
}
2019-09-18 14:24:20 +03:00
pub struct Cmd<'a> {
pub unix: &'a str,
pub windows: &'a str,
pub work_dir: &'a str,
}
2019-09-18 14:24:20 +03:00
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<()> {
2019-03-05 18:19:36 -03:00
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<()> {
2018-10-31 23:57:38 +03:00
match Command::new("rustup")
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
.stderr(Stdio::null())
.stdout(Stdio::null())
.status()
{
Ok(status) if status.success() => (),
_ => install_rustfmt().context("install rustfmt")?,
2018-10-31 23:57:38 +03:00
};
2019-10-23 18:13:40 +03:00
if mode == Mode::Verify {
2019-02-08 14:49:43 +03:00
run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?;
} else {
run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
}
Ok(())
}
2018-10-31 23:57:38 +03:00
2018-12-29 22:19:35 +01:00
pub fn install_rustfmt() -> Result<()> {
2018-10-31 23:57:38 +03:00
run(&format!("rustup install {}", TOOLCHAIN), ".")?;
2019-02-08 14:49:43 +03:00
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
2018-10-31 23:57:38 +03:00
}
2018-12-09 10:29:13 +00:00
pub fn install_format_hook() -> Result<()> {
let result_path = Path::new(if cfg!(windows) {
"./.git/hooks/pre-commit.exe"
} else {
"./.git/hooks/pre-commit"
});
if !result_path.exists() {
2019-10-17 23:01:53 +03:00
run("cargo build --package xtask --bin pre-commit", ".")?;
if cfg!(windows) {
fs::copy("./target/debug/pre-commit.exe", result_path)?;
} else {
fs::copy("./target/debug/pre-commit", result_path)?;
2018-12-09 10:29:13 +00:00
}
} else {
2019-06-16 00:48:50 +06:00
Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?;
2018-12-09 10:29:13 +00:00
}
Ok(())
}
2018-12-31 13:14:06 +00:00
2019-06-03 09:43:06 -04:00
pub fn run_clippy() -> Result<()> {
match Command::new("rustup")
.args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"])
.stderr(Stdio::null())
.stdout(Stdio::null())
.status()
{
Ok(status) if status.success() => (),
_ => install_clippy().context("install clippy")?,
2019-06-03 09:43:06 -04:00
};
let allowed_lints = [
"clippy::collapsible_if",
"clippy::map_clone", // FIXME: remove when Iterator::copied stabilizes (1.36.0)
"clippy::needless_pass_by_value",
"clippy::nonminimal_bool",
"clippy::redundant_pattern_matching",
];
2019-06-03 09:43:06 -04:00
run(
&format!(
"rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
TOOLCHAIN,
allowed_lints.join(" -A ")
),
".",
)?;
Ok(())
}
pub fn install_clippy() -> Result<()> {
run(&format!("rustup install {}", TOOLCHAIN), ".")?;
run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".")
}
2018-12-31 13:14:06 +00:00
pub fn run_fuzzer() -> Result<()> {
match Command::new("cargo")
.args(&["fuzz", "--help"])
.stderr(Stdio::null())
.stdout(Stdio::null())
.status()
{
Ok(status) if status.success() => (),
_ => run("cargo install cargo-fuzz", ".")?,
};
2019-02-08 14:49:43 +03:00
run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
2018-12-31 13:14:06 +00:00
}
2019-03-05 18:19:36 -03:00
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))?;
2019-03-05 18:19:36 -03:00
if !output.status.success() {
anyhow::bail!("`{}` exited with {}", cmdline, output.status);
2019-03-05 18:19:36 -03:00
}
Ok(output)
}