diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /xtask/src/codegen.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'xtask/src/codegen.rs')
-rw-r--r-- | xtask/src/codegen.rs | 49 |
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; | |||
9 | mod gen_parser_tests; | 9 | mod gen_parser_tests; |
10 | mod gen_assists_docs; | 10 | mod gen_assists_docs; |
11 | mod gen_feature_docs; | 11 | mod gen_feature_docs; |
12 | mod gen_features; | ||
12 | 13 | ||
13 | use std::{ | 14 | use std::{ |
14 | fmt, mem, | 15 | fmt, mem, |
15 | path::{Path, PathBuf}, | 16 | path::{Path, PathBuf}, |
16 | }; | 17 | }; |
17 | 18 | ||
18 | use crate::{not_bash::fs2, project_root, Result}; | 19 | use crate::{ |
20 | ensure_rustfmt, | ||
21 | not_bash::{fs2, pushenv, run}, | ||
22 | project_root, Result, | ||
23 | }; | ||
19 | 24 | ||
20 | pub use self::{ | 25 | pub 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 | ||
27 | const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; | ||
28 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; | ||
29 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; | ||
30 | |||
31 | const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; | ||
32 | const AST_NODES: &str = "crates/ra_syntax/src/ast/generated/nodes.rs"; | ||
33 | const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs"; | ||
34 | |||
35 | const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers"; | ||
36 | const 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)] |
39 | pub enum Mode { | 34 | pub enum Mode { |
40 | Overwrite, | 35 | Overwrite, |
41 | Verify, | 36 | Verify, |
42 | } | 37 | } |
43 | 38 | ||
39 | pub struct CodegenCmd { | ||
40 | pub features: bool, | ||
41 | } | ||
42 | |||
43 | impl 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, |
46 | fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | 59 | fn 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 | ||
78 | const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; | ||
79 | |||
80 | fn 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 | |||
65 | fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { | 90 | fn 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 | } |