From 0f6f458cc1b460076093efda903bf1a1b9062697 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 16:35:27 +0300 Subject: Make working with codegen less annoying We probably should look into removing `xtask codegen` altogether. The test workflow works perfectly for package.json config. There are two things preventing that: * Lint completions are generated on demand. * Docs are not committed to the repository. --- xtask/src/codegen.rs | 17 ++++++++++++----- xtask/src/main.rs | 13 +++++++++---- xtask/src/tidy.rs | 16 ++++------------ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 2f56c5ad0..e3f9ccada 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -32,7 +32,7 @@ pub(crate) use self::{ #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(crate) enum Mode { Overwrite, - Verify, + Ensure, } impl flags::Codegen { @@ -59,12 +59,19 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { } _ => (), } - if mode == Mode::Verify { - anyhow::bail!("`{}` is not up-to-date", path.display()); - } + let return_error = match mode { + Mode::Overwrite => false, + Mode::Ensure => true, + }; eprintln!("updating {}", path.display()); write_file(path, contents)?; - return Ok(()); + + return if return_error { + let path = path.strip_prefix(&project_root()).unwrap_or(path); + anyhow::bail!("`{}` was not up-to-date, updating", path.display()); + } else { + Ok(()) + }; fn normalize(s: &str) -> String { s.replace("\r\n", "\n") diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 3c4332f75..25fd32f92 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -89,11 +89,16 @@ fn run_rustfmt(mode: Mode) -> Result<()> { let _dir = pushd(project_root())?; let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); ensure_rustfmt()?; - let check = match mode { - Mode::Overwrite => &[][..], - Mode::Verify => &["--", "--check"], + match mode { + Mode::Overwrite => cmd!("cargo fmt").run()?, + Mode::Ensure => { + let res = cmd!("cargo fmt -- --check").run(); + if !res.is_ok() { + let _ = cmd!("cargo fmt").run(); + } + res?; + } }; - cmd!("cargo fmt {check...}").run()?; Ok(()) } diff --git a/xtask/src/tidy.rs b/xtask/src/tidy.rs index 349ca14d0..3818b9e0f 100644 --- a/xtask/src/tidy.rs +++ b/xtask/src/tidy.rs @@ -13,30 +13,22 @@ use crate::{ #[test] fn generated_grammar_is_fresh() { - if let Err(error) = codegen::generate_syntax(Mode::Verify) { - panic!("{}. Please update it by running `cargo xtask codegen`", error); - } + codegen::generate_syntax(Mode::Ensure).unwrap() } #[test] fn generated_tests_are_fresh() { - if let Err(error) = codegen::generate_parser_tests(Mode::Verify) { - panic!("{}. Please update tests by running `cargo xtask codegen`", error); - } + codegen::generate_parser_tests(Mode::Ensure).unwrap() } #[test] fn generated_assists_are_fresh() { - if let Err(error) = codegen::generate_assists_tests(Mode::Verify) { - panic!("{}. Please update assists by running `cargo xtask codegen`", error); - } + codegen::generate_assists_tests(Mode::Ensure).unwrap(); } #[test] fn check_code_formatting() { - if let Err(error) = run_rustfmt(Mode::Verify) { - panic!("{}. Please format the code by running `cargo format`", error); - } + run_rustfmt(Mode::Ensure).unwrap() } #[test] -- cgit v1.2.3