aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r--xtask/src/codegen.rs78
1 files changed, 38 insertions, 40 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index 2f56c5ad0..2cf3c6fdc 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -7,68 +7,66 @@
7 7
8mod gen_syntax; 8mod gen_syntax;
9mod gen_parser_tests; 9mod gen_parser_tests;
10mod gen_lint_completions;
10mod gen_assists_docs; 11mod gen_assists_docs;
11mod gen_feature_docs; 12mod gen_feature_docs;
12mod gen_lint_completions;
13mod gen_diagnostic_docs; 13mod gen_diagnostic_docs;
14 14
15use std::{ 15use std::{
16 fmt, mem, 16 fmt, mem,
17 path::{Path, PathBuf}, 17 path::{Path, PathBuf},
18}; 18};
19use xshell::{cmd, pushenv, read_file, write_file}; 19use xshell::{cmd, pushenv};
20 20
21use crate::{ensure_rustfmt, flags, project_root, Result}; 21use crate::{ensure_rustfmt, project_root, Result};
22 22
23pub(crate) use self::{ 23pub(crate) use self::{
24 gen_assists_docs::{generate_assists_docs, generate_assists_tests}, 24 gen_assists_docs::generate_assists_tests, gen_lint_completions::generate_lint_completions,
25 gen_diagnostic_docs::generate_diagnostic_docs, 25 gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax,
26 gen_feature_docs::generate_feature_docs,
27 gen_lint_completions::generate_lint_completions,
28 gen_parser_tests::generate_parser_tests,
29 gen_syntax::generate_syntax,
30}; 26};
31 27
32#[derive(Debug, PartialEq, Eq, Clone, Copy)] 28pub(crate) fn docs() -> Result<()> {
33pub(crate) enum Mode { 29 // We don't commit docs to the repo, so we can just overwrite them.
34 Overwrite, 30 gen_assists_docs::generate_assists_docs()?;
35 Verify, 31 gen_feature_docs::generate_feature_docs()?;
32 gen_diagnostic_docs::generate_diagnostic_docs()?;
33 Ok(())
36} 34}
37 35
38impl flags::Codegen { 36#[allow(unused)]
39 pub(crate) fn run(self) -> Result<()> { 37fn used() {
40 if self.features { 38 generate_parser_tests();
41 generate_lint_completions(Mode::Overwrite)?; 39 generate_assists_tests();
42 } 40 generate_syntax();
43 generate_syntax(Mode::Overwrite)?; 41 generate_lint_completions();
44 generate_parser_tests(Mode::Overwrite)?;
45 generate_assists_tests(Mode::Overwrite)?;
46 generate_assists_docs(Mode::Overwrite)?;
47 generate_feature_docs(Mode::Overwrite)?;
48 generate_diagnostic_docs(Mode::Overwrite)?;
49 Ok(())
50 }
51} 42}
52 43
53/// A helper to update file on disk if it has changed. 44/// Checks that the `file` has the specified `contents`. If that is not the
54/// With verify = false, 45/// case, updates the file and then fails the test.
55fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { 46pub(crate) fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> {
56 match read_file(path) { 47 match std::fs::read_to_string(file) {
57 Ok(old_contents) if normalize(&old_contents) == normalize(contents) => { 48 Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
58 return Ok(()); 49 return Ok(())
59 } 50 }
60 _ => (), 51 _ => (),
61 } 52 }
62 if mode == Mode::Verify { 53 let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
63 anyhow::bail!("`{}` is not up-to-date", path.display()); 54 eprintln!(
55 "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
56 display_path.display()
57 );
58 if std::env::var("CI").is_ok() {
59 eprintln!(" NOTE: run `cargo test` locally and commit the updated files\n");
64 } 60 }
65 eprintln!("updating {}", path.display()); 61 if let Some(parent) = file.parent() {
66 write_file(path, contents)?; 62 let _ = std::fs::create_dir_all(parent);
67 return Ok(());
68
69 fn normalize(s: &str) -> String {
70 s.replace("\r\n", "\n")
71 } 63 }
64 std::fs::write(file, contents).unwrap();
65 anyhow::bail!("some file were not up to date")
66}
67
68fn normalize_newlines(s: &str) -> String {
69 s.replace("\r\n", "\n")
72} 70}
73 71
74const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; 72const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`";