diff options
author | Aleksey Kladov <[email protected]> | 2020-08-18 18:31:06 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-18 18:36:27 +0100 |
commit | 27ccc95c60d5652d5e7ef0dd7bd50cf221385d00 (patch) | |
tree | 7ce49ed16d73f8183f4456be2299c2dea9d36f40 /xtask/src/codegen | |
parent | f18f9da7d80c669cf14bc2e85e40d883c621262a (diff) |
Cleanup feature generation
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r-- | xtask/src/codegen/gen_assists_docs.rs | 4 | ||||
-rw-r--r-- | xtask/src/codegen/gen_features.rs | 50 | ||||
-rw-r--r-- | xtask/src/codegen/gen_parser_tests.rs | 8 | ||||
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 8 | ||||
-rw-r--r-- | xtask/src/codegen/gen_unstable_future_descriptor.rs | 61 |
5 files changed, 60 insertions, 71 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 4f4968594..f0ded8b87 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs | |||
@@ -32,7 +32,7 @@ struct Assist { | |||
32 | impl Assist { | 32 | impl Assist { |
33 | fn collect() -> Result<Vec<Assist>> { | 33 | fn collect() -> Result<Vec<Assist>> { |
34 | let mut res = Vec::new(); | 34 | let mut res = Vec::new(); |
35 | for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) { | 35 | for path in rust_files(&project_root().join("crates/assists/src/handlers")) { |
36 | collect_file(&mut res, path.as_path())?; | 36 | collect_file(&mut res, path.as_path())?; |
37 | } | 37 | } |
38 | res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id)); | 38 | res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id)); |
@@ -135,7 +135,7 @@ r#####" | |||
135 | buf.push_str(&test) | 135 | buf.push_str(&test) |
136 | } | 136 | } |
137 | let buf = reformat(buf)?; | 137 | let buf = reformat(buf)?; |
138 | codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode) | 138 | codegen::update(&project_root().join("crates/assists/src/tests/generated.rs"), &buf, mode) |
139 | } | 139 | } |
140 | 140 | ||
141 | fn hide_hash_comments(text: &str) -> String { | 141 | fn hide_hash_comments(text: &str) -> String { |
diff --git a/xtask/src/codegen/gen_features.rs b/xtask/src/codegen/gen_features.rs new file mode 100644 index 000000000..78268308b --- /dev/null +++ b/xtask/src/codegen/gen_features.rs | |||
@@ -0,0 +1,50 @@ | |||
1 | //! Generates descriptors structure for unstable feature from Unstable Book | ||
2 | use std::path::{Path, PathBuf}; | ||
3 | |||
4 | use quote::quote; | ||
5 | use walkdir::WalkDir; | ||
6 | |||
7 | use crate::{ | ||
8 | codegen::{project_root, reformat, update, Mode, Result}, | ||
9 | not_bash::{fs2, run}, | ||
10 | }; | ||
11 | |||
12 | pub fn generate_features(mode: Mode) -> Result<()> { | ||
13 | if !Path::new("./target/rust").exists() { | ||
14 | run!("git clone https://github.com/rust-lang/rust ./target/rust")?; | ||
15 | } | ||
16 | |||
17 | let contents = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?; | ||
18 | |||
19 | let destination = project_root().join("crates/ide/src/completion/generated_features.rs"); | ||
20 | update(destination.as_path(), &contents, mode)?; | ||
21 | |||
22 | Ok(()) | ||
23 | } | ||
24 | |||
25 | fn generate_descriptor(src_dir: PathBuf) -> Result<String> { | ||
26 | let definitions = ["language-features", "library-features"] | ||
27 | .iter() | ||
28 | .flat_map(|it| WalkDir::new(src_dir.join(it))) | ||
29 | .filter_map(|e| e.ok()) | ||
30 | .filter(|entry| { | ||
31 | // Get all `.md ` files | ||
32 | entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" | ||
33 | }) | ||
34 | .map(|entry| { | ||
35 | let path = entry.path(); | ||
36 | let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); | ||
37 | let doc = fs2::read_to_string(path).unwrap(); | ||
38 | |||
39 | quote! { LintCompletion { label: #feature_ident, description: #doc } } | ||
40 | }); | ||
41 | |||
42 | let ts = quote! { | ||
43 | use crate::completion::complete_attribute::LintCompletion; | ||
44 | |||
45 | pub(super) const FEATURES: &[LintCompletion] = &[ | ||
46 | #(#definitions),* | ||
47 | ]; | ||
48 | }; | ||
49 | reformat(ts) | ||
50 | } | ||
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index 2977da2fa..96fdd9216 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs | |||
@@ -8,12 +8,12 @@ use std::{ | |||
8 | }; | 8 | }; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | codegen::{self, extract_comment_blocks, update, Mode}, | 11 | codegen::{extract_comment_blocks, update, Mode}, |
12 | project_root, Result, | 12 | project_root, Result, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | pub fn generate_parser_tests(mode: Mode) -> Result<()> { | 15 | pub fn generate_parser_tests(mode: Mode) -> Result<()> { |
16 | let tests = tests_from_dir(&project_root().join(Path::new(codegen::GRAMMAR_DIR)))?; | 16 | let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; |
17 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { | 17 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { |
18 | let tests_dir = project_root().join(into); | 18 | let tests_dir = project_root().join(into); |
19 | if !tests_dir.is_dir() { | 19 | if !tests_dir.is_dir() { |
@@ -39,8 +39,8 @@ pub fn generate_parser_tests(mode: Mode) -> Result<()> { | |||
39 | } | 39 | } |
40 | Ok(()) | 40 | Ok(()) |
41 | } | 41 | } |
42 | install_tests(&tests.ok, codegen::OK_INLINE_TESTS_DIR, mode)?; | 42 | install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?; |
43 | install_tests(&tests.err, codegen::ERR_INLINE_TESTS_DIR, mode) | 43 | install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", mode) |
44 | } | 44 | } |
45 | 45 | ||
46 | #[derive(Debug)] | 46 | #[derive(Debug)] |
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index df3ec22c8..53ae9f11c 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -14,7 +14,7 @@ use ungrammar::{rust_grammar, Grammar, Rule}; | |||
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, | 16 | ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, |
17 | codegen::{self, reformat, update, Mode}, | 17 | codegen::{reformat, update, Mode}, |
18 | project_root, Result, | 18 | project_root, Result, |
19 | }; | 19 | }; |
20 | 20 | ||
@@ -22,15 +22,15 @@ pub fn generate_syntax(mode: Mode) -> Result<()> { | |||
22 | let grammar = rust_grammar(); | 22 | let grammar = rust_grammar(); |
23 | let ast = lower(&grammar); | 23 | let ast = lower(&grammar); |
24 | 24 | ||
25 | let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); | 25 | let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs"); |
26 | let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; | 26 | let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; |
27 | update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; | 27 | update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; |
28 | 28 | ||
29 | let ast_tokens_file = project_root().join(codegen::AST_TOKENS); | 29 | let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs"); |
30 | let contents = generate_tokens(&ast)?; | 30 | let contents = generate_tokens(&ast)?; |
31 | update(ast_tokens_file.as_path(), &contents, mode)?; | 31 | update(ast_tokens_file.as_path(), &contents, mode)?; |
32 | 32 | ||
33 | let ast_nodes_file = project_root().join(codegen::AST_NODES); | 33 | let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs"); |
34 | let contents = generate_nodes(KINDS_SRC, &ast)?; | 34 | let contents = generate_nodes(KINDS_SRC, &ast)?; |
35 | update(ast_nodes_file.as_path(), &contents, mode)?; | 35 | update(ast_nodes_file.as_path(), &contents, mode)?; |
36 | 36 | ||
diff --git a/xtask/src/codegen/gen_unstable_future_descriptor.rs b/xtask/src/codegen/gen_unstable_future_descriptor.rs deleted file mode 100644 index 907a9afae..000000000 --- a/xtask/src/codegen/gen_unstable_future_descriptor.rs +++ /dev/null | |||
@@ -1,61 +0,0 @@ | |||
1 | //! Generates descriptors structure for unstable feature from Unstable Book | ||
2 | |||
3 | use crate::codegen::{self, project_root, Mode, Result}; | ||
4 | use crate::codegen::{reformat, update}; | ||
5 | use crate::not_bash::{fs2, pushd, run}; | ||
6 | use proc_macro2::TokenStream; | ||
7 | use quote::quote; | ||
8 | use std::path::PathBuf; | ||
9 | use walkdir::WalkDir; | ||
10 | |||
11 | fn generate_descriptor(src_dir: PathBuf) -> Result<TokenStream> { | ||
12 | let files = WalkDir::new(src_dir.join("language-features")) | ||
13 | .into_iter() | ||
14 | .chain(WalkDir::new(src_dir.join("library-features"))) | ||
15 | .filter_map(|e| e.ok()) | ||
16 | .filter(|entry| { | ||
17 | // Get all `.md ` files | ||
18 | entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" | ||
19 | }) | ||
20 | .collect::<Vec<_>>(); | ||
21 | |||
22 | let definitions = files | ||
23 | .iter() | ||
24 | .map(|entry| { | ||
25 | let path = entry.path(); | ||
26 | let feature_ident = | ||
27 | format!("{}", path.file_stem().unwrap().to_str().unwrap().replace("-", "_")); | ||
28 | let doc = format!("{}", std::fs::read_to_string(path).unwrap()); | ||
29 | |||
30 | quote! { LintCompletion { label: #feature_ident, description: #doc } } | ||
31 | }) | ||
32 | .collect::<Vec<_>>(); | ||
33 | |||
34 | let ts = quote! { | ||
35 | use crate::completion::LintCompletion; | ||
36 | |||
37 | pub(crate) const UNSTABLE_FEATURE_DESCRIPTOR: &[LintCompletion] = &[ | ||
38 | #(#definitions),* | ||
39 | ]; | ||
40 | }; | ||
41 | Ok(ts) | ||
42 | } | ||
43 | |||
44 | pub fn generate_unstable_future_descriptor(mode: Mode) -> Result<()> { | ||
45 | let path = project_root().join(codegen::STORAGE); | ||
46 | fs2::create_dir_all(path.clone())?; | ||
47 | |||
48 | let _d = pushd(path.clone()); | ||
49 | run!("git init")?; | ||
50 | run!("git remote add -f origin {}", codegen::REPOSITORY_URL)?; | ||
51 | run!("git pull origin master")?; | ||
52 | |||
53 | let src_dir = path.join(codegen::REPO_PATH); | ||
54 | let content = generate_descriptor(src_dir)?.to_string(); | ||
55 | |||
56 | let contents = reformat(content)?; | ||
57 | let destination = project_root().join(codegen::UNSTABLE_FEATURE); | ||
58 | update(destination.as_path(), &contents, mode)?; | ||
59 | |||
60 | Ok(()) | ||
61 | } | ||