Remove now dead code

This commit is contained in:
Aleksey Kladov 2021-03-08 21:39:09 +03:00
parent 095b9110b5
commit e89c0e3961
9 changed files with 76 additions and 90 deletions

View File

@ -16,7 +16,7 @@ use std::{
fmt, mem, fmt, mem,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use xshell::{cmd, pushenv, read_file, write_file}; use xshell::{cmd, pushenv};
use crate::{ensure_rustfmt, project_root, Result}; use crate::{ensure_rustfmt, project_root, Result};
@ -35,44 +35,38 @@ pub(crate) fn docs() -> Result<()> {
#[allow(unused)] #[allow(unused)]
fn used() { fn used() {
generate_parser_tests(Mode::Overwrite); generate_parser_tests();
generate_assists_tests(Mode::Overwrite); generate_assists_tests();
generate_syntax(Mode::Overwrite); generate_syntax();
generate_lint_completions(Mode::Overwrite); generate_lint_completions();
} }
#[derive(Debug, PartialEq, Eq, Clone, Copy)] /// Checks that the `file` has the specified `contents`. If that is not the
pub(crate) enum Mode { /// case, updates the file and then fails the test.
Overwrite, pub(crate) fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> {
Ensure, match std::fs::read_to_string(file) {
} Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
return Ok(())
/// A helper to update file on disk if it has changed.
/// With verify = false,
fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
match read_file(path) {
Ok(old_contents) if normalize(&old_contents) == normalize(contents) => {
return Ok(());
} }
_ => (), _ => (),
} }
let return_error = match mode { let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
Mode::Overwrite => false, eprintln!(
Mode::Ensure => true, "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
}; display_path.display()
eprintln!("updating {}", path.display()); );
write_file(path, contents)?; if std::env::var("CI").is_ok() {
eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n");
return if return_error {
let path = path.strip_prefix(&project_root()).unwrap_or(path);
anyhow::bail!("`{}` was not up-to-date, updating", path.display());
} else {
Ok(())
};
fn normalize(s: &str) -> String {
s.replace("\r\n", "\n")
} }
if let Some(parent) = file.parent() {
let _ = std::fs::create_dir_all(parent);
}
std::fs::write(file, contents).unwrap();
anyhow::bail!("some file were not up to date")
}
fn normalize_newlines(s: &str) -> String {
s.replace("\r\n", "\n")
} }
const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`"; const PREAMBLE: &str = "Generated file, do not edit by hand, see `xtask/src/codegen`";

View File

@ -2,14 +2,16 @@
use std::{fmt, path::Path}; use std::{fmt, path::Path};
use xshell::write_file;
use crate::{ use crate::{
codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, Mode, PREAMBLE}, codegen::{self, extract_comment_blocks_with_empty_lines, reformat, Location, PREAMBLE},
project_root, rust_files_in, Result, project_root, rust_files_in, Result,
}; };
pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> { pub(crate) fn generate_assists_tests() -> Result<()> {
let assists = Assist::collect()?; let assists = Assist::collect()?;
generate_tests(&assists, mode) generate_tests(&assists)
} }
pub(crate) fn generate_assists_docs() -> Result<()> { pub(crate) fn generate_assists_docs() -> Result<()> {
@ -17,7 +19,8 @@ pub(crate) fn generate_assists_docs() -> Result<()> {
let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
let dst = project_root().join("docs/user/generated_assists.adoc"); let dst = project_root().join("docs/user/generated_assists.adoc");
codegen::update(&dst, &contents, Mode::Overwrite) write_file(dst, &contents)?;
Ok(())
} }
#[derive(Debug)] #[derive(Debug)]
@ -111,7 +114,7 @@ impl fmt::Display for Assist {
} }
} }
fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> { fn generate_tests(assists: &[Assist]) -> Result<()> {
let mut buf = String::from("use super::check_doc_test;\n"); let mut buf = String::from("use super::check_doc_test;\n");
for assist in assists.iter() { for assist in assists.iter() {
@ -135,7 +138,10 @@ r#####"
buf.push_str(&test) buf.push_str(&test)
} }
let buf = reformat(&buf)?; let buf = reformat(&buf)?;
codegen::update(&project_root().join("crates/ide_assists/src/tests/generated.rs"), &buf, mode) codegen::ensure_file_contents(
&project_root().join("crates/ide_assists/src/tests/generated.rs"),
&buf,
)
} }
fn hide_hash_comments(text: &str) -> String { fn hide_hash_comments(text: &str) -> String {

View File

@ -2,8 +2,10 @@
use std::{fmt, path::PathBuf}; use std::{fmt, path::PathBuf};
use xshell::write_file;
use crate::{ use crate::{
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE}, codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE},
project_root, rust_files, Result, project_root, rust_files, Result,
}; };
@ -13,7 +15,7 @@ pub(crate) fn generate_diagnostic_docs() -> Result<()> {
diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
let dst = project_root().join("docs/user/generated_diagnostic.adoc"); let dst = project_root().join("docs/user/generated_diagnostic.adoc");
codegen::update(&dst, &contents, Mode::Overwrite)?; write_file(&dst, &contents)?;
Ok(()) Ok(())
} }

View File

@ -2,8 +2,10 @@
use std::{fmt, path::PathBuf}; use std::{fmt, path::PathBuf};
use xshell::write_file;
use crate::{ use crate::{
codegen::{self, extract_comment_blocks_with_empty_lines, Location, Mode, PREAMBLE}, codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE},
project_root, rust_files, Result, project_root, rust_files, Result,
}; };
@ -12,7 +14,7 @@ pub(crate) fn generate_feature_docs() -> Result<()> {
let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
let dst = project_root().join("docs/user/generated_features.adoc"); let dst = project_root().join("docs/user/generated_features.adoc");
codegen::update(&dst, &contents, Mode::Overwrite)?; write_file(&dst, &contents)?;
Ok(()) Ok(())
} }

View File

@ -5,12 +5,9 @@ use std::path::{Path, PathBuf};
use walkdir::WalkDir; use walkdir::WalkDir;
use xshell::{cmd, read_file}; use xshell::{cmd, read_file};
use crate::{ use crate::codegen::{ensure_file_contents, project_root, reformat, Result};
codegen::{project_root, reformat, update, Mode, Result},
run_rustfmt,
};
pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> { pub(crate) fn generate_lint_completions() -> Result<()> {
if !Path::new("./target/rust").exists() { if !Path::new("./target/rust").exists() {
cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
} }
@ -25,8 +22,7 @@ pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> {
let destination = let destination =
project_root().join("crates/ide_completion/src/generated_lint_completions.rs"); project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
update(destination.as_path(), &contents, mode)?; ensure_file_contents(destination.as_path(), &contents)?;
run_rustfmt(mode)?;
Ok(()) Ok(())
} }

View File

@ -8,13 +8,13 @@ use std::{
}; };
use crate::{ use crate::{
codegen::{extract_comment_blocks, update, Mode}, codegen::{ensure_file_contents, extract_comment_blocks},
project_root, Result, project_root, Result,
}; };
pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> { pub(crate) fn generate_parser_tests() -> Result<()> {
let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?;
fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { fn install_tests(tests: &HashMap<String, Test>, into: &str) -> Result<()> {
let tests_dir = project_root().join(into); let tests_dir = project_root().join(into);
if !tests_dir.is_dir() { if !tests_dir.is_dir() {
fs::create_dir_all(&tests_dir)?; fs::create_dir_all(&tests_dir)?;
@ -35,12 +35,12 @@ pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> {
tests_dir.join(file_name) tests_dir.join(file_name)
} }
}; };
update(&path, &test.text, mode)?; ensure_file_contents(&path, &test.text)?;
} }
Ok(()) Ok(())
} }
install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?; install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok")?;
install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", mode) install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err")
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -14,25 +14,25 @@ use ungrammar::{rust_grammar, Grammar, Rule};
use crate::{ use crate::{
ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC},
codegen::{reformat, update, Mode}, codegen::{ensure_file_contents, reformat},
project_root, Result, project_root, Result,
}; };
pub(crate) fn generate_syntax(mode: Mode) -> Result<()> { pub(crate) fn generate_syntax() -> Result<()> {
let grammar = rust_grammar(); let grammar = rust_grammar();
let ast = lower(&grammar); let ast = lower(&grammar);
let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs"); let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs");
let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?;
update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; ensure_file_contents(syntax_kinds_file.as_path(), &syntax_kinds)?;
let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs"); let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs");
let contents = generate_tokens(&ast)?; let contents = generate_tokens(&ast)?;
update(ast_tokens_file.as_path(), &contents, mode)?; ensure_file_contents(ast_tokens_file.as_path(), &contents)?;
let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs"); let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs");
let contents = generate_nodes(KINDS_SRC, &ast)?; let contents = generate_nodes(KINDS_SRC, &ast)?;
update(ast_nodes_file.as_path(), &contents, mode)?; ensure_file_contents(ast_nodes_file.as_path(), &contents)?;
Ok(()) Ok(())
} }

View File

@ -28,7 +28,7 @@ use std::{
use walkdir::{DirEntry, WalkDir}; use walkdir::{DirEntry, WalkDir};
use xshell::{cmd, cp, pushd, pushenv}; use xshell::{cmd, cp, pushd, pushenv};
use crate::{codegen::Mode, dist::DistCmd}; use crate::dist::DistCmd;
fn main() -> Result<()> { fn main() -> Result<()> {
let _d = pushd(project_root())?; let _d = pushd(project_root())?;
@ -84,23 +84,6 @@ fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
files_in(path, "rs") files_in(path, "rs")
} }
fn run_rustfmt(mode: Mode) -> Result<()> {
let _dir = pushd(project_root())?;
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
ensure_rustfmt()?;
match mode {
Mode::Overwrite => cmd!("cargo fmt").run()?,
Mode::Ensure => {
let res = cmd!("cargo fmt -- --check").run();
if !res.is_ok() {
let _ = cmd!("cargo fmt").run();
}
res?;
}
};
Ok(())
}
fn ensure_rustfmt() -> Result<()> { fn ensure_rustfmt() -> Result<()> {
let out = cmd!("rustfmt --version").read()?; let out = cmd!("rustfmt --version").read()?;
if !out.contains("stable") { if !out.contains("stable") {

View File

@ -3,27 +3,23 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use xshell::{cmd, read_file}; use xshell::{cmd, pushd, pushenv, read_file};
use crate::{ use crate::{cargo_files, codegen, project_root, rust_files};
cargo_files,
codegen::{self, Mode},
project_root, run_rustfmt, rust_files,
};
#[test] #[test]
fn generate_grammar() { fn generate_grammar() {
codegen::generate_syntax(Mode::Ensure).unwrap() codegen::generate_syntax().unwrap()
} }
#[test] #[test]
fn generate_parser_tests() { fn generate_parser_tests() {
codegen::generate_parser_tests(Mode::Ensure).unwrap() codegen::generate_parser_tests().unwrap()
} }
#[test] #[test]
fn generate_assists_tests() { fn generate_assists_tests() {
codegen::generate_assists_tests(Mode::Ensure).unwrap(); codegen::generate_assists_tests().unwrap();
} }
/// This clones rustc repo, and so is not worth to keep up-to-date. We update /// This clones rustc repo, and so is not worth to keep up-to-date. We update
@ -31,12 +27,19 @@ fn generate_assists_tests() {
#[test] #[test]
#[ignore] #[ignore]
fn generate_lint_completions() { fn generate_lint_completions() {
codegen::generate_lint_completions(Mode::Ensure).unwrap() codegen::generate_lint_completions().unwrap()
} }
#[test] #[test]
fn check_code_formatting() { fn check_code_formatting() {
run_rustfmt(Mode::Ensure).unwrap() let _dir = pushd(project_root()).unwrap();
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
crate::ensure_rustfmt().unwrap();
let res = cmd!("cargo fmt -- --check").run();
if !res.is_ok() {
let _ = cmd!("cargo fmt").run();
}
res.unwrap()
} }
#[test] #[test]