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,
path::{Path, PathBuf},
};
use xshell::{cmd, pushenv, read_file, write_file};
use xshell::{cmd, pushenv};
use crate::{ensure_rustfmt, project_root, Result};
@ -35,44 +35,38 @@ pub(crate) fn docs() -> Result<()> {
#[allow(unused)]
fn used() {
generate_parser_tests(Mode::Overwrite);
generate_assists_tests(Mode::Overwrite);
generate_syntax(Mode::Overwrite);
generate_lint_completions(Mode::Overwrite);
generate_parser_tests();
generate_assists_tests();
generate_syntax();
generate_lint_completions();
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(crate) enum Mode {
Overwrite,
Ensure,
}
/// 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(());
/// Checks that the `file` has the specified `contents`. If that is not the
/// case, updates the file and then fails the test.
pub(crate) fn ensure_file_contents(file: &Path, contents: &str) -> Result<()> {
match std::fs::read_to_string(file) {
Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
return Ok(())
}
_ => (),
}
let return_error = match mode {
Mode::Overwrite => false,
Mode::Ensure => true,
};
eprintln!("updating {}", path.display());
write_file(path, contents)?;
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")
let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
eprintln!(
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
display_path.display()
);
if std::env::var("CI").is_ok() {
eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\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`";

View File

@ -2,14 +2,16 @@
use std::{fmt, path::Path};
use xshell::write_file;
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,
};
pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> {
pub(crate) fn generate_assists_tests() -> Result<()> {
let assists = Assist::collect()?;
generate_tests(&assists, mode)
generate_tests(&assists)
}
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 = format!("//{}\n{}\n", PREAMBLE, contents.trim());
let dst = project_root().join("docs/user/generated_assists.adoc");
codegen::update(&dst, &contents, Mode::Overwrite)
write_file(dst, &contents)?;
Ok(())
}
#[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");
for assist in assists.iter() {
@ -135,7 +138,10 @@ r#####"
buf.push_str(&test)
}
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 {

View File

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

View File

@ -2,8 +2,10 @@
use std::{fmt, path::PathBuf};
use xshell::write_file;
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,
};
@ -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 = format!("//{}\n{}\n", PREAMBLE, contents.trim());
let dst = project_root().join("docs/user/generated_features.adoc");
codegen::update(&dst, &contents, Mode::Overwrite)?;
write_file(&dst, &contents)?;
Ok(())
}

View File

@ -5,12 +5,9 @@ use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use xshell::{cmd, read_file};
use crate::{
codegen::{project_root, reformat, update, Mode, Result},
run_rustfmt,
};
use crate::codegen::{ensure_file_contents, project_root, reformat, Result};
pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> {
pub(crate) fn generate_lint_completions() -> Result<()> {
if !Path::new("./target/rust").exists() {
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 =
project_root().join("crates/ide_completion/src/generated_lint_completions.rs");
update(destination.as_path(), &contents, mode)?;
run_rustfmt(mode)?;
ensure_file_contents(destination.as_path(), &contents)?;
Ok(())
}

View File

@ -8,13 +8,13 @@ use std::{
};
use crate::{
codegen::{extract_comment_blocks, update, Mode},
codegen::{ensure_file_contents, extract_comment_blocks},
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")))?;
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);
if !tests_dir.is_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)
}
};
update(&path, &test.text, mode)?;
ensure_file_contents(&path, &test.text)?;
}
Ok(())
}
install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?;
install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", mode)
install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok")?;
install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err")
}
#[derive(Debug)]

View File

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

View File

@ -28,7 +28,7 @@ use std::{
use walkdir::{DirEntry, WalkDir};
use xshell::{cmd, cp, pushd, pushenv};
use crate::{codegen::Mode, dist::DistCmd};
use crate::dist::DistCmd;
fn main() -> Result<()> {
let _d = pushd(project_root())?;
@ -84,23 +84,6 @@ fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
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<()> {
let out = cmd!("rustfmt --version").read()?;
if !out.contains("stable") {

View File

@ -3,27 +3,23 @@ use std::{
path::{Path, PathBuf},
};
use xshell::{cmd, read_file};
use xshell::{cmd, pushd, pushenv, read_file};
use crate::{
cargo_files,
codegen::{self, Mode},
project_root, run_rustfmt, rust_files,
};
use crate::{cargo_files, codegen, project_root, rust_files};
#[test]
fn generate_grammar() {
codegen::generate_syntax(Mode::Ensure).unwrap()
codegen::generate_syntax().unwrap()
}
#[test]
fn generate_parser_tests() {
codegen::generate_parser_tests(Mode::Ensure).unwrap()
codegen::generate_parser_tests().unwrap()
}
#[test]
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
@ -31,12 +27,19 @@ fn generate_assists_tests() {
#[test]
#[ignore]
fn generate_lint_completions() {
codegen::generate_lint_completions(Mode::Ensure).unwrap()
codegen::generate_lint_completions().unwrap()
}
#[test]
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]