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, true,
)), )),
); );
#[allow(unknown_lints, unnecessary_min_or_max)] #[allow(unknown_lints, clippy::unnecessary_min_or_max)]
check_number( check_number(
r#" r#"
extern "rust-intrinsic" { extern "rust-intrinsic" {

View file

@ -11,11 +11,8 @@
#![allow(clippy::disallowed_types)] #![allow(clippy::disallowed_types)]
mod ratoml; mod ratoml;
#[cfg(not(feature = "in-rust-tree"))]
mod sourcegen;
mod support; mod support;
mod testdir; mod testdir;
mod tidy;
use std::{collections::HashMap, path::PathBuf, time::Instant}; 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)] #[derive(Clone)]
pub(crate) struct CommentBlock { pub(crate) struct CommentBlock {
pub(crate) id: String, pub(crate) id: String,

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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