Re-implement tidy as an xtask action

This commit is contained in:
Lukas Wirth 2024-07-07 09:11:46 +02:00
parent e752517814
commit 866102cdaf
12 changed files with 69 additions and 60 deletions

View file

@ -426,7 +426,7 @@ fn floating_point() {
true,
)),
);
#[allow(unknown_lints, unnecessary_min_or_max)]
#[allow(unknown_lints, clippy::unnecessary_min_or_max)]
check_number(
r#"
extern "rust-intrinsic" {

View file

@ -11,11 +11,8 @@
#![allow(clippy::disallowed_types)]
mod ratoml;
#[cfg(not(feature = "in-rust-tree"))]
mod sourcegen;
mod support;
mod testdir;
mod tidy;
use std::{collections::HashMap, path::PathBuf, time::Instant};

View file

@ -39,36 +39,6 @@ impl flags::Codegen {
}
}
fn list_rust_files(dir: &Path) -> Vec<PathBuf> {
let mut res = list_files(dir);
res.retain(|it| {
it.file_name().unwrap_or_default().to_str().unwrap_or_default().ends_with(".rs")
});
res
}
fn list_files(dir: &Path) -> Vec<PathBuf> {
let mut res = Vec::new();
let mut work = vec![dir.to_path_buf()];
while let Some(dir) = work.pop() {
for entry in dir.read_dir().unwrap() {
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
let path = entry.path();
let is_hidden =
path.file_name().unwrap_or_default().to_str().unwrap_or_default().starts_with('.');
if !is_hidden {
if file_type.is_dir() {
work.push(path);
} else if file_type.is_file() {
res.push(path);
}
}
}
}
res
}
#[derive(Clone)]
pub(crate) struct CommentBlock {
pub(crate) id: String,

View file

@ -5,10 +5,9 @@ use std::{fmt, fs, path::Path};
use stdx::format_to_acc;
use crate::{
codegen::{
add_preamble, ensure_file_contents, list_rust_files, reformat, CommentBlock, Location,
},
codegen::{add_preamble, ensure_file_contents, reformat, CommentBlock, Location},
project_root,
util::list_rust_files,
};
pub(crate) fn generate(check: bool) {

View file

@ -3,8 +3,9 @@
use std::{fmt, fs, io, path::PathBuf};
use crate::{
codegen::{add_preamble, list_rust_files, CommentBlock, Location},
codegen::{add_preamble, CommentBlock, Location},
project_root,
util::list_rust_files,
};
pub(crate) fn generate(check: bool) {

View file

@ -3,8 +3,9 @@
use std::{fmt, fs, io, path::PathBuf};
use crate::{
codegen::{list_rust_files, CommentBlock, Location},
codegen::{CommentBlock, Location},
project_root,
util::list_rust_files,
};
pub(crate) fn generate(_check: bool) {

View file

@ -6,8 +6,9 @@ use stdx::format_to;
use xshell::{cmd, Shell};
use crate::{
codegen::{add_preamble, ensure_file_contents, list_files, reformat},
codegen::{add_preamble, ensure_file_contents, reformat},
project_root,
util::list_files,
};
const DESTINATION: &str = "crates/ide-db/src/generated/lints.rs";

View file

@ -9,8 +9,9 @@ use std::{
};
use crate::{
codegen::{ensure_file_contents, list_rust_files, CommentBlock},
codegen::{ensure_file_contents, CommentBlock},
project_root,
util::list_rust_files,
};
pub(crate) fn generate(check: bool) {

View file

@ -73,6 +73,8 @@ xflags::xflags! {
optional codegen_type: CodegenType
optional --check
}
cmd tidy {}
}
}
@ -96,8 +98,12 @@ pub enum XtaskCmd {
Metrics(Metrics),
Bb(Bb),
Codegen(Codegen),
Tidy(Tidy),
}
#[derive(Debug)]
pub struct Tidy {}
#[derive(Debug)]
pub struct Install {
pub client: bool,

View file

@ -19,6 +19,8 @@ mod install;
mod metrics;
mod publish;
mod release;
mod tidy;
mod util;
use anyhow::bail;
use std::{env, path::PathBuf};
@ -51,6 +53,7 @@ fn main() -> anyhow::Result<()> {
)?;
Ok(())
}
flags::XtaskCmd::Tidy(cmd) => cmd.run(sh),
}
}

View file

@ -6,23 +6,29 @@ use std::{
use xshell::Shell;
#[cfg(not(feature = "in-rust-tree"))]
use xshell::cmd;
#[test]
fn check_lsp_extensions_docs() {
let sh = &Shell::new().unwrap();
use crate::{flags::Tidy, project_root, util::list_files};
impl Tidy {
pub(crate) fn run(&self, sh: &Shell) -> anyhow::Result<()> {
check_lsp_extensions_docs(sh);
files_are_tidy(sh);
check_licenses(sh);
Ok(())
}
}
fn check_lsp_extensions_docs(sh: &Shell) {
let expected_hash = {
let lsp_ext_rs = sh
.read_file(sourcegen::project_root().join("crates/rust-analyzer/src/lsp/ext.rs"))
.unwrap();
let lsp_ext_rs =
sh.read_file(project_root().join("crates/rust-analyzer/src/lsp/ext.rs")).unwrap();
stable_hash(lsp_ext_rs.as_str())
};
let actual_hash = {
let lsp_extensions_md =
sh.read_file(sourcegen::project_root().join("docs/dev/lsp-extensions.md")).unwrap();
sh.read_file(project_root().join("docs/dev/lsp-extensions.md")).unwrap();
let text = lsp_extensions_md
.lines()
.find_map(|line| line.strip_prefix("lsp/ext.rs hash:"))
@ -45,11 +51,8 @@ Please adjust docs/dev/lsp-extensions.md.
}
}
#[test]
fn files_are_tidy() {
let sh = &Shell::new().unwrap();
let files = sourcegen::list_files(&sourcegen::project_root().join("crates"));
fn files_are_tidy(sh: &Shell) {
let files = list_files(&project_root().join("crates"));
let mut tidy_docs = TidyDocs::default();
let mut tidy_marks = TidyMarks::default();
@ -121,11 +124,7 @@ fn check_cargo_toml(path: &Path, text: String) {
}
}
#[cfg(not(feature = "in-rust-tree"))]
#[test]
fn check_licenses() {
let sh = &Shell::new().unwrap();
fn check_licenses(sh: &Shell) {
let expected = "
(MIT OR Apache-2.0) AND Unicode-DFS-2016
0BSD OR MIT OR Apache-2.0
@ -277,7 +276,7 @@ impl TidyDocs {
}
fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
p.strip_prefix(sourcegen::project_root())
p.strip_prefix(project_root())
.unwrap()
.components()
.rev()

31
xtask/src/util.rs Normal file
View file

@ -0,0 +1,31 @@
use std::path::{Path, PathBuf};
pub(crate) fn list_rust_files(dir: &Path) -> Vec<PathBuf> {
let mut res = list_files(dir);
res.retain(|it| {
it.file_name().unwrap_or_default().to_str().unwrap_or_default().ends_with(".rs")
});
res
}
pub(crate) fn list_files(dir: &Path) -> Vec<PathBuf> {
let mut res = Vec::new();
let mut work = vec![dir.to_path_buf()];
while let Some(dir) = work.pop() {
for entry in dir.read_dir().unwrap() {
let entry = entry.unwrap();
let file_type = entry.file_type().unwrap();
let path = entry.path();
let is_hidden =
path.file_name().unwrap_or_default().to_str().unwrap_or_default().starts_with('.');
if !is_hidden {
if file_type.is_dir() {
work.push(path);
} else if file_type.is_file() {
res.push(path);
}
}
}
}
res
}