Skip to content

Commit e89c0e3

Browse files
committed
Remove now dead code
1 parent 095b911 commit e89c0e3

File tree

9 files changed

+76
-90
lines changed

9 files changed

+76
-90
lines changed

xtask/src/codegen.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
fmt, mem,
1717
path::{Path, PathBuf},
1818
};
19-
use xshell::{cmd, pushenv, read_file, write_file};
19+
use xshell::{cmd, pushenv};
2020

2121
use crate::{ensure_rustfmt, project_root, Result};
2222

@@ -35,44 +35,38 @@ pub(crate) fn docs() -> Result<()> {
3535

3636
#[allow(unused)]
3737
fn used() {
38-
generate_parser_tests(Mode::Overwrite);
39-
generate_assists_tests(Mode::Overwrite);
40-
generate_syntax(Mode::Overwrite);
41-
generate_lint_completions(Mode::Overwrite);
38+
generate_parser_tests();
39+
generate_assists_tests();
40+
generate_syntax();
41+
generate_lint_completions();
4242
}
4343

44-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
45-
pub(crate) enum Mode {
46-
Overwrite,
47-
Ensure,
48-
}
49-
50-
/// A helper to update file on disk if it has changed.
51-
/// With verify = false,
52-
fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
53-
match read_file(path) {
54-
Ok(old_contents) if normalize(&old_contents) == normalize(contents) => {
55-
return Ok(());
44+
/// Checks that the `file` has the specified `contents`. If that is not the
45+
/// case, updates the file and then fails the test.
46+
pub(crate) fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> {
47+
match std::fs::read_to_string(file) {
48+
Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
49+
return Ok(())
5650
}
5751
_ => (),
5852
}
59-
let return_error = match mode {
60-
Mode::Overwrite => false,
61-
Mode::Ensure => true,
62-
};
63-
eprintln!("updating {}", path.display());
64-
write_file(path, contents)?;
65-
66-
return if return_error {
67-
let path = path.strip_prefix(&project_root()).unwrap_or(path);
68-
anyhow::bail!("`{}` was not up-to-date, updating", path.display());
69-
} else {
70-
Ok(())
71-
};
72-
73-
fn normalize(s: &str) -> String {
74-
s.replace("\r\n", "\n")
53+
let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
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!("\n NOTE: run `cargo test` locally and commit the updated files\n");
60+
}
61+
if let Some(parent) = file.parent() {
62+
let _ = std::fs::create_dir_all(parent);
7563
}
64+
std::fs::write(file, contents).unwrap();
65+
anyhow::bail!("some file were not up to date")
66+
}
67+
68+
fn normalize_newlines(s: &str) -> String {
69+
s.replace("\r\n", "\n")
7670
}
7771

7872
const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`";

xtask/src/codegen/gen_assists_docs.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22
33
use std::{fmt, path::Path};
44

5+
use xshell::write_file;
6+
57
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},
79
project_root, rust_files_in, Result,
810
};
911

10-
pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> {
12+
pub(crate) fn generate_assists_tests() -> Result<()> {
1113
let assists = Assist::collect()?;
12-
generate_tests(&assists, mode)
14+
generate_tests(&assists)
1315
}
1416

1517
pub(crate) fn generate_assists_docs() -> Result<()> {
1618
let assists = Assist::collect()?;
1719
let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
1820
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
1921
let dst = project_root().join("docs/user/generated_assists.adoc");
20-
codegen::update(&dst, &contents, Mode::Overwrite)
22+
write_file(dst, &contents)?;
23+
Ok(())
2124
}
2225

2326
#[derive(Debug)]
@@ -111,7 +114,7 @@ impl fmt::Display for Assist {
111114
}
112115
}
113116

114-
fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
117+
fn generate_tests(assists: &[Assist]) -> Result<()> {
115118
let mut buf = String::from("use super::check_doc_test;\n");
116119

117120
for assist in assists.iter() {
@@ -135,7 +138,10 @@ r#####"
135138
buf.push_str(&test)
136139
}
137140
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+
)
139145
}
140146

141147
fn hide_hash_comments(text: &str) -> String {

xtask/src/codegen/gen_diagnostic_docs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
33
use std::{fmt, path::PathBuf};
44

5+
use xshell::write_file;
6+
57
use crate::{
6-
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE},
8+
codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE},
79
project_root, rust_files, Result,
810
};
911

@@ -13,7 +15,7 @@ pub(crate) fn generate_diagnostic_docs() -> Result<()> {
1315
diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
1416
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
1517
let dst = project_root().join("docs/user/generated_diagnostic.adoc");
16-
codegen::update(&dst, &contents, Mode::Overwrite)?;
18+
write_file(&dst, &contents)?;
1719
Ok(())
1820
}
1921

xtask/src/codegen/gen_feature_docs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
33
use std::{fmt, path::PathBuf};
44

5+
use xshell::write_file;
6+
57
use crate::{
6-
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE},
8+
codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE},
79
project_root, rust_files, Result,
810
};
911

@@ -12,7 +14,7 @@ pub(crate) fn generate_feature_docs() -> Result<()> {
1214
let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
1315
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
1416
let dst = project_root().join("docs/user/generated_features.adoc");
15-
codegen::update(&dst, &contents, Mode::Overwrite)?;
17+
write_file(&dst, &contents)?;
1618
Ok(())
1719
}
1820

xtask/src/codegen/gen_lint_completions.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ use std::path::{Path, PathBuf};
55
use walkdir::WalkDir;
66
use xshell::{cmd, read_file};
77

8-
use crate::{
9-
codegen::{project_root, reformat, update, Mode, Result},
10-
run_rustfmt,
11-
};
8+
use crate::codegen::{ensure_file_contents, project_root, reformat, Result};
129

13-
pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> {
10+
pub(crate) fn generate_lint_completions() -> Result<()> {
1411
if !Path::new("./target/rust").exists() {
1512
cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
1613
}
@@ -25,8 +22,7 @@ pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> {
2522

2623
let destination =
2724
project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
28-
update(destination.as_path(), &contents, mode)?;
29-
run_rustfmt(mode)?;
25+
ensure_file_contents(destination.as_path(), &contents)?;
3026

3127
Ok(())
3228
}

xtask/src/codegen/gen_parser_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use std::{
88
};
99

1010
use crate::{
11-
codegen::{extract_comment_blocks, update, Mode},
11+
codegen::{ensure_file_contents, extract_comment_blocks},
1212
project_root, Result,
1313
};
1414

15-
pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> {
15+
pub(crate) fn generate_parser_tests() -> Result<()> {
1616
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<()> {
1818
let tests_dir = project_root().join(into);
1919
if !tests_dir.is_dir() {
2020
fs::create_dir_all(&tests_dir)?;
@@ -35,12 +35,12 @@ pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> {
3535
tests_dir.join(file_name)
3636
}
3737
};
38-
update(&path, &test.text, mode)?;
38+
ensure_file_contents(&path, &test.text)?;
3939
}
4040
Ok(())
4141
}
42-
install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?;
43-
install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", 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")
4444
}
4545

4646
#[derive(Debug)]

xtask/src/codegen/gen_syntax.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ use ungrammar::{rust_grammar, Grammar, Rule};
1414

1515
use crate::{
1616
ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC},
17-
codegen::{reformat, update, Mode},
17+
codegen::{ensure_file_contents, reformat},
1818
project_root, Result,
1919
};
2020

21-
pub(crate) fn generate_syntax(mode: Mode) -> Result<()> {
21+
pub(crate) fn generate_syntax() -> Result<()> {
2222
let grammar = rust_grammar();
2323
let ast = lower(&grammar);
2424

2525
let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs");
2626
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)?;
2828

2929
let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs");
3030
let contents = generate_tokens(&ast)?;
31-
update(ast_tokens_file.as_path(), &contents, mode)?;
31+
ensure_file_contents(ast_tokens_file.as_path(), &contents)?;
3232

3333
let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs");
3434
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)?;
3636

3737
Ok(())
3838
}

xtask/src/main.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::{
2828
use walkdir::{DirEntry, WalkDir};
2929
use xshell::{cmd, cp, pushd, pushenv};
3030

31-
use crate::{codegen::Mode, dist::DistCmd};
31+
use crate::dist::DistCmd;
3232

3333
fn main() -> Result<()> {
3434
let _d = pushd(project_root())?;
@@ -84,23 +84,6 @@ fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
8484
files_in(path, "rs")
8585
}
8686

87-
fn run_rustfmt(mode: Mode) -> Result<()> {
88-
let _dir = pushd(project_root())?;
89-
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
90-
ensure_rustfmt()?;
91-
match mode {
92-
Mode::Overwrite => cmd!("cargo fmt").run()?,
93-
Mode::Ensure => {
94-
let res = cmd!("cargo fmt -- --check").run();
95-
if !res.is_ok() {
96-
let _ = cmd!("cargo fmt").run();
97-
}
98-
res?;
99-
}
100-
};
101-
Ok(())
102-
}
103-
10487
fn ensure_rustfmt() -> Result<()> {
10588
let out = cmd!("rustfmt --version").read()?;
10689
if !out.contains("stable") {

xtask/src/tidy.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,43 @@ use std::{
33
path::{Path, PathBuf},
44
};
55

6-
use xshell::{cmd, read_file};
6+
use xshell::{cmd, pushd, pushenv, read_file};
77

8-
use crate::{
9-
cargo_files,
10-
codegen::{self, Mode},
11-
project_root, run_rustfmt, rust_files,
12-
};
8+
use crate::{cargo_files, codegen, project_root, rust_files};
139

1410
#[test]
1511
fn generate_grammar() {
16-
codegen::generate_syntax(Mode::Ensure).unwrap()
12+
codegen::generate_syntax().unwrap()
1713
}
1814

1915
#[test]
2016
fn generate_parser_tests() {
21-
codegen::generate_parser_tests(Mode::Ensure).unwrap()
17+
codegen::generate_parser_tests().unwrap()
2218
}
2319

2420
#[test]
2521
fn generate_assists_tests() {
26-
codegen::generate_assists_tests(Mode::Ensure).unwrap();
22+
codegen::generate_assists_tests().unwrap();
2723
}
2824

2925
/// This clones rustc repo, and so is not worth to keep up-to-date. We update
3026
/// manually by un-ignoring the test from time to time.
3127
#[test]
3228
#[ignore]
3329
fn generate_lint_completions() {
34-
codegen::generate_lint_completions(Mode::Ensure).unwrap()
30+
codegen::generate_lint_completions().unwrap()
3531
}
3632

3733
#[test]
3834
fn check_code_formatting() {
39-
run_rustfmt(Mode::Ensure).unwrap()
35+
let _dir = pushd(project_root()).unwrap();
36+
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
37+
crate::ensure_rustfmt().unwrap();
38+
let res = cmd!("cargo fmt -- --check").run();
39+
if !res.is_ok() {
40+
let _ = cmd!("cargo fmt").run();
41+
}
42+
res.unwrap()
4043
}
4144

4245
#[test]

0 commit comments

Comments
 (0)