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.rs49
1 files changed, 37 insertions, 12 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index f5f4b964a..45b17bb48 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -9,38 +9,51 @@ mod gen_syntax;
9mod gen_parser_tests; 9mod gen_parser_tests;
10mod gen_assists_docs; 10mod gen_assists_docs;
11mod gen_feature_docs; 11mod gen_feature_docs;
12mod gen_features;
12 13
13use std::{ 14use std::{
14 fmt, mem, 15 fmt, mem,
15 path::{Path, PathBuf}, 16 path::{Path, PathBuf},
16}; 17};
17 18
18use crate::{not_bash::fs2, project_root, Result}; 19use crate::{
20 ensure_rustfmt,
21 not_bash::{fs2, pushenv, run},
22 project_root, Result,
23};
19 24
20pub use self::{ 25pub use self::{
21 gen_assists_docs::{generate_assists_docs, generate_assists_tests}, 26 gen_assists_docs::{generate_assists_docs, generate_assists_tests},
22 gen_feature_docs::generate_feature_docs, 27 gen_feature_docs::generate_feature_docs,
28 gen_features::generate_features,
23 gen_parser_tests::generate_parser_tests, 29 gen_parser_tests::generate_parser_tests,
24 gen_syntax::generate_syntax, 30 gen_syntax::generate_syntax,
25}; 31};
26 32
27const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
28const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok";
29const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err";
30
31const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs";
32const AST_NODES: &str = "crates/ra_syntax/src/ast/generated/nodes.rs";
33const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs";
34
35const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers";
36const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs";
37
38#[derive(Debug, PartialEq, Eq, Clone, Copy)] 33#[derive(Debug, PartialEq, Eq, Clone, Copy)]
39pub enum Mode { 34pub enum Mode {
40 Overwrite, 35 Overwrite,
41 Verify, 36 Verify,
42} 37}
43 38
39pub struct CodegenCmd {
40 pub features: bool,
41}
42
43impl CodegenCmd {
44 pub fn run(self) -> Result<()> {
45 if self.features {
46 generate_features(Mode::Overwrite)?;
47 }
48 generate_syntax(Mode::Overwrite)?;
49 generate_parser_tests(Mode::Overwrite)?;
50 generate_assists_tests(Mode::Overwrite)?;
51 generate_assists_docs(Mode::Overwrite)?;
52 generate_feature_docs(Mode::Overwrite)?;
53 Ok(())
54 }
55}
56
44/// A helper to update file on disk if it has changed. 57/// A helper to update file on disk if it has changed.
45/// With verify = false, 58/// With verify = false,
46fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { 59fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
@@ -62,6 +75,18 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
62 } 75 }
63} 76}
64 77
78const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`";
79
80fn reformat(text: impl std::fmt::Display) -> Result<String> {
81 let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
82 ensure_rustfmt()?;
83 let stdout = run!(
84 "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display();
85 <text.to_string().as_bytes()
86 )?;
87 Ok(format!("//! {}\n\n{}\n", PREAMBLE, stdout))
88}
89
65fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { 90fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
66 do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect() 91 do_extract_comment_blocks(text, false).into_iter().map(|(_line, block)| block).collect()
67} 92}