diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-08 18:52:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-08 18:52:08 +0000 |
commit | 8b7e82b012c417ec40a896203ad79f20cf5530ef (patch) | |
tree | 85413766cc45708d77dfa9cb5a3a00767af8461a /xtask/src/codegen | |
parent | 071dde1c1da10e3580bded99dc2d529074356536 (diff) | |
parent | d2bb2268d3a9cc0e2a6970c85c45724af5eb255c (diff) |
Merge #7918
7918: Generalize file ensuring infrastructure r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r-- | xtask/src/codegen/gen_assists_docs.rs | 20 | ||||
-rw-r--r-- | xtask/src/codegen/gen_diagnostic_docs.rs | 8 | ||||
-rw-r--r-- | xtask/src/codegen/gen_feature_docs.rs | 8 | ||||
-rw-r--r-- | xtask/src/codegen/gen_lint_completions.rs | 12 | ||||
-rw-r--r-- | xtask/src/codegen/gen_parser_tests.rs | 12 | ||||
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 10 |
6 files changed, 38 insertions, 32 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index c469b388d..158680993 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs | |||
@@ -2,22 +2,25 @@ | |||
2 | 2 | ||
3 | use std::{fmt, path::Path}; | 3 | use std::{fmt, path::Path}; |
4 | 4 | ||
5 | use xshell::write_file; | ||
6 | |||
5 | use crate::{ | 7 | use crate::{ |
6 | codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, Mode, PREAMBLE}, | 8 | codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, PREAMBLE}, |
7 | project_root, rust_files_in, Result, | 9 | project_root, rust_files_in, Result, |
8 | }; | 10 | }; |
9 | 11 | ||
10 | pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> { | 12 | pub(crate) fn generate_assists_tests() -> Result<()> { |
11 | let assists = Assist::collect()?; | 13 | let assists = Assist::collect()?; |
12 | generate_tests(&assists, mode) | 14 | generate_tests(&assists) |
13 | } | 15 | } |
14 | 16 | ||
15 | pub(crate) fn generate_assists_docs(mode: Mode) -> Result<()> { | 17 | pub(crate) fn generate_assists_docs() -> Result<()> { |
16 | let assists = Assist::collect()?; | 18 | let assists = Assist::collect()?; |
17 | let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 19 | let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
18 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); | 20 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); |
19 | let dst = project_root().join("docs/user/generated_assists.adoc"); | 21 | let dst = project_root().join("docs/user/generated_assists.adoc"); |
20 | codegen::update(&dst, &contents, mode) | 22 | write_file(dst, &contents)?; |
23 | Ok(()) | ||
21 | } | 24 | } |
22 | 25 | ||
23 | #[derive(Debug)] | 26 | #[derive(Debug)] |
@@ -111,7 +114,7 @@ impl fmt::Display for Assist { | |||
111 | } | 114 | } |
112 | } | 115 | } |
113 | 116 | ||
114 | fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> { | 117 | fn generate_tests(assists: &[Assist]) -> Result<()> { |
115 | let mut buf = String::from("use super::check_doc_test;\n"); | 118 | let mut buf = String::from("use super::check_doc_test;\n"); |
116 | 119 | ||
117 | for assist in assists.iter() { | 120 | for assist in assists.iter() { |
@@ -135,7 +138,10 @@ r#####" | |||
135 | buf.push_str(&test) | 138 | buf.push_str(&test) |
136 | } | 139 | } |
137 | let buf = reformat(&buf)?; | 140 | let buf = reformat(&buf)?; |
138 | codegen::update(&project_root().join("crates/ide_assists/src/tests/generated.rs"), &buf, mode) | 141 | codegen::ensure_file_contents( |
142 | &project_root().join("crates/ide_assists/src/tests/generated.rs"), | ||
143 | &buf, | ||
144 | ) | ||
139 | } | 145 | } |
140 | 146 | ||
141 | fn hide_hash_comments(text: &str) -> String { | 147 | fn hide_hash_comments(text: &str) -> String { |
diff --git a/xtask/src/codegen/gen_diagnostic_docs.rs b/xtask/src/codegen/gen_diagnostic_docs.rs index a2561817b..9cf4d0a88 100644 --- a/xtask/src/codegen/gen_diagnostic_docs.rs +++ b/xtask/src/codegen/gen_diagnostic_docs.rs | |||
@@ -2,18 +2,20 @@ | |||
2 | 2 | ||
3 | use std::{fmt, path::PathBuf}; | 3 | use std::{fmt, path::PathBuf}; |
4 | 4 | ||
5 | use xshell::write_file; | ||
6 | |||
5 | use crate::{ | 7 | use crate::{ |
6 | codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE}, | 8 | codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE}, |
7 | project_root, rust_files, Result, | 9 | project_root, rust_files, Result, |
8 | }; | 10 | }; |
9 | 11 | ||
10 | pub(crate) fn generate_diagnostic_docs(mode: Mode) -> Result<()> { | 12 | pub(crate) fn generate_diagnostic_docs() -> Result<()> { |
11 | let diagnostics = Diagnostic::collect()?; | 13 | let diagnostics = Diagnostic::collect()?; |
12 | let contents = | 14 | let contents = |
13 | diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 15 | diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
14 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); | 16 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); |
15 | let dst = project_root().join("docs/user/generated_diagnostic.adoc"); | 17 | let dst = project_root().join("docs/user/generated_diagnostic.adoc"); |
16 | codegen::update(&dst, &contents, mode)?; | 18 | write_file(&dst, &contents)?; |
17 | Ok(()) | 19 | Ok(()) |
18 | } | 20 | } |
19 | 21 | ||
diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs index cad7ff477..c373d7d70 100644 --- a/xtask/src/codegen/gen_feature_docs.rs +++ b/xtask/src/codegen/gen_feature_docs.rs | |||
@@ -2,17 +2,19 @@ | |||
2 | 2 | ||
3 | use std::{fmt, path::PathBuf}; | 3 | use std::{fmt, path::PathBuf}; |
4 | 4 | ||
5 | use xshell::write_file; | ||
6 | |||
5 | use crate::{ | 7 | use crate::{ |
6 | codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE}, | 8 | codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE}, |
7 | project_root, rust_files, Result, | 9 | project_root, rust_files, Result, |
8 | }; | 10 | }; |
9 | 11 | ||
10 | pub(crate) fn generate_feature_docs(mode: Mode) -> Result<()> { | 12 | pub(crate) fn generate_feature_docs() -> Result<()> { |
11 | let features = Feature::collect()?; | 13 | let features = Feature::collect()?; |
12 | let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); | 14 | let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); |
13 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); | 15 | let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); |
14 | let dst = project_root().join("docs/user/generated_features.adoc"); | 16 | let dst = project_root().join("docs/user/generated_features.adoc"); |
15 | codegen::update(&dst, &contents, mode)?; | 17 | write_file(&dst, &contents)?; |
16 | Ok(()) | 18 | Ok(()) |
17 | } | 19 | } |
18 | 20 | ||
diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs index b1c057037..24dbc6a39 100644 --- a/xtask/src/codegen/gen_lint_completions.rs +++ b/xtask/src/codegen/gen_lint_completions.rs | |||
@@ -5,13 +5,10 @@ use std::path::{Path, PathBuf}; | |||
5 | use walkdir::WalkDir; | 5 | use walkdir::WalkDir; |
6 | use xshell::{cmd, read_file}; | 6 | use xshell::{cmd, read_file}; |
7 | 7 | ||
8 | use crate::{ | 8 | use crate::codegen::{ensure_file_contents, project_root, reformat, Result}; |
9 | codegen::{project_root, reformat, update, Mode, Result}, | ||
10 | run_rustfmt, | ||
11 | }; | ||
12 | 9 | ||
13 | pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> { | 10 | pub(crate) fn generate_lint_completions() -> Result<()> { |
14 | if !Path::new("./target/rust").exists() { | 11 | if !project_root().join("./target/rust").exists() { |
15 | cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; | 12 | cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; |
16 | } | 13 | } |
17 | 14 | ||
@@ -25,8 +22,7 @@ pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> { | |||
25 | 22 | ||
26 | let destination = | 23 | let destination = |
27 | project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); | 24 | project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); |
28 | update(destination.as_path(), &contents, mode)?; | 25 | ensure_file_contents(destination.as_path(), &contents)?; |
29 | run_rustfmt(mode)?; | ||
30 | 26 | ||
31 | Ok(()) | 27 | Ok(()) |
32 | } | 28 | } |
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index cb8939063..096590653 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs | |||
@@ -8,13 +8,13 @@ use std::{ | |||
8 | }; | 8 | }; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | codegen::{extract_comment_blocks, update, Mode}, | 11 | codegen::{ensure_file_contents, extract_comment_blocks}, |
12 | project_root, Result, | 12 | project_root, Result, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> { | 15 | pub(crate) fn generate_parser_tests() -> Result<()> { |
16 | let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; | 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) -> 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() { |
20 | fs::create_dir_all(&tests_dir)?; | 20 | fs::create_dir_all(&tests_dir)?; |
@@ -35,12 +35,12 @@ pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> { | |||
35 | tests_dir.join(file_name) | 35 | tests_dir.join(file_name) |
36 | } | 36 | } |
37 | }; | 37 | }; |
38 | update(&path, &test.text, mode)?; | 38 | ensure_file_contents(&path, &test.text)?; |
39 | } | 39 | } |
40 | Ok(()) | 40 | Ok(()) |
41 | } | 41 | } |
42 | install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?; | 42 | install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok")?; |
43 | install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", mode) | 43 | install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err") |
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 191bc0e9d..80f26e8f5 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -14,25 +14,25 @@ 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::{reformat, update, Mode}, | 17 | codegen::{ensure_file_contents, reformat}, |
18 | project_root, Result, | 18 | project_root, Result, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | pub(crate) fn generate_syntax(mode: Mode) -> Result<()> { | 21 | pub(crate) fn generate_syntax() -> 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("crates/parser/src/syntax_kind/generated.rs"); | 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 | ensure_file_contents(syntax_kinds_file.as_path(), &syntax_kinds)?; |
28 | 28 | ||
29 | let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs"); | 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 | ensure_file_contents(ast_tokens_file.as_path(), &contents)?; |
32 | 32 | ||
33 | let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs"); | 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 | ensure_file_contents(ast_nodes_file.as_path(), &contents)?; |
36 | 36 | ||
37 | Ok(()) | 37 | Ok(()) |
38 | } | 38 | } |