diff options
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r-- | xtask/src/codegen.rs | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 2248a079f..e43d4fa73 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs | |||
@@ -16,7 +16,7 @@ use std::{ | |||
16 | fmt, mem, | 16 | fmt, mem, |
17 | path::{Path, PathBuf}, | 17 | path::{Path, PathBuf}, |
18 | }; | 18 | }; |
19 | use xshell::{cmd, pushenv, read_file, write_file}; | 19 | use xshell::{cmd, pushenv}; |
20 | 20 | ||
21 | use crate::{ensure_rustfmt, project_root, Result}; | 21 | use crate::{ensure_rustfmt, project_root, Result}; |
22 | 22 | ||
@@ -35,44 +35,38 @@ pub(crate) fn docs() -> Result<()> { | |||
35 | 35 | ||
36 | #[allow(unused)] | 36 | #[allow(unused)] |
37 | fn used() { | 37 | fn used() { |
38 | generate_parser_tests(Mode::Overwrite); | 38 | generate_parser_tests(); |
39 | generate_assists_tests(Mode::Overwrite); | 39 | generate_assists_tests(); |
40 | generate_syntax(Mode::Overwrite); | 40 | generate_syntax(); |
41 | generate_lint_completions(Mode::Overwrite); | 41 | generate_lint_completions(); |
42 | } | 42 | } |
43 | 43 | ||
44 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 44 | /// Checks that the `file` has the specified `contents`. If that is not the |
45 | pub(crate) enum Mode { | 45 | /// case, updates the file and then fails the test. |
46 | Overwrite, | 46 | pub(crate) fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> { |
47 | Ensure, | 47 | match std::fs::read_to_string(file) { |
48 | } | 48 | Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => { |
49 | 49 | return Ok(()) | |
50 | /// A helper to update file on disk if it has changed. | ||
51 | /// With verify = false, | ||
52 | fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | ||
53 | match read_file(path) { | ||
54 | Ok(old_contents) if normalize(&old_contents) == normalize(contents) => { | ||
55 | return Ok(()); | ||
56 | } | 50 | } |
57 | _ => (), | 51 | _ => (), |
58 | } | 52 | } |
59 | let return_error = match mode { | 53 | let display_path = file.strip_prefix(&project_root()).unwrap_or(file); |
60 | Mode::Overwrite => false, | 54 | eprintln!( |
61 | Mode::Ensure => true, | 55 | "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", |
62 | }; | 56 | display_path.display() |
63 | eprintln!("updating {}", path.display()); | 57 | ); |
64 | write_file(path, contents)?; | 58 | if std::env::var("CI").is_ok() { |
65 | 59 | eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n"); | |
66 | return if return_error { | 60 | } |
67 | let path = path.strip_prefix(&project_root()).unwrap_or(path); | 61 | if let Some(parent) = file.parent() { |
68 | anyhow::bail!("`{}` was not up-to-date, updating", path.display()); | 62 | let _ = std::fs::create_dir_all(parent); |
69 | } else { | ||
70 | Ok(()) | ||
71 | }; | ||
72 | |||
73 | fn normalize(s: &str) -> String { | ||
74 | s.replace("\r\n", "\n") | ||
75 | } | 63 | } |
64 | std::fs::write(file, contents).unwrap(); | ||
65 | anyhow::bail!("some file were not up to date") | ||
66 | } | ||
67 | |||
68 | fn normalize_newlines(s: &str) -> String { | ||
69 | s.replace("\r\n", "\n") | ||
76 | } | 70 | } |
77 | 71 | ||
78 | const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; | 72 | const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; |