From 27ccc95c60d5652d5e7ef0dd7bd50cf221385d00 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Aug 2020 19:31:06 +0200 Subject: Cleanup feature generation --- xtask/src/codegen.rs | 40 +++++++------- xtask/src/codegen/gen_assists_docs.rs | 4 +- xtask/src/codegen/gen_features.rs | 50 ++++++++++++++++++ xtask/src/codegen/gen_parser_tests.rs | 8 +-- xtask/src/codegen/gen_syntax.rs | 8 +-- .../src/codegen/gen_unstable_future_descriptor.rs | 61 ---------------------- xtask/src/main.rs | 10 ++-- xtask/tests/tidy.rs | 2 +- 8 files changed, 84 insertions(+), 99 deletions(-) create mode 100644 xtask/src/codegen/gen_features.rs delete mode 100644 xtask/src/codegen/gen_unstable_future_descriptor.rs (limited to 'xtask') diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index c468468de..45b17bb48 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -9,7 +9,7 @@ mod gen_syntax; mod gen_parser_tests; mod gen_assists_docs; mod gen_feature_docs; -mod gen_unstable_future_descriptor; +mod gen_features; use std::{ fmt, mem, @@ -25,35 +25,35 @@ use crate::{ pub use self::{ gen_assists_docs::{generate_assists_docs, generate_assists_tests}, gen_feature_docs::generate_feature_docs, + gen_features::generate_features, gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax, - gen_unstable_future_descriptor::generate_unstable_future_descriptor, }; -// Directory used by xtask -const STORAGE: &str = ".xtask"; - -const GRAMMAR_DIR: &str = "crates/parser/src/grammar"; -const OK_INLINE_TESTS_DIR: &str = "crates/syntax/test_data/parser/inline/ok"; -const ERR_INLINE_TESTS_DIR: &str = "crates/syntax/test_data/parser/inline/err"; - -const SYNTAX_KINDS: &str = "crates/parser/src/syntax_kind/generated.rs"; -const AST_NODES: &str = "crates/syntax/src/ast/generated/nodes.rs"; -const AST_TOKENS: &str = "crates/syntax/src/ast/generated/tokens.rs"; - -const ASSISTS_DIR: &str = "crates/assists/src/handlers"; -const ASSISTS_TESTS: &str = "crates/assists/src/tests/generated.rs"; - -const REPOSITORY_URL: &str = "https://github.com/rust-lang/rust"; -const UNSTABLE_FEATURE: &str = "crates/ide/src/completion/unstable_feature_descriptor.rs"; -const REPO_PATH: &str = "src/doc/unstable-book/src"; - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Mode { Overwrite, Verify, } +pub struct CodegenCmd { + pub features: bool, +} + +impl CodegenCmd { + pub fn run(self) -> Result<()> { + if self.features { + generate_features(Mode::Overwrite)?; + } + generate_syntax(Mode::Overwrite)?; + generate_parser_tests(Mode::Overwrite)?; + generate_assists_tests(Mode::Overwrite)?; + generate_assists_docs(Mode::Overwrite)?; + generate_feature_docs(Mode::Overwrite)?; + Ok(()) + } +} + /// A helper to update file on disk if it has changed. /// With verify = false, fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 4f4968594..f0ded8b87 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs @@ -32,7 +32,7 @@ struct Assist { impl Assist { fn collect() -> Result> { let mut res = Vec::new(); - for path in rust_files(&project_root().join(codegen::ASSISTS_DIR)) { + for path in rust_files(&project_root().join("crates/assists/src/handlers")) { collect_file(&mut res, path.as_path())?; } res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id)); @@ -135,7 +135,7 @@ r#####" buf.push_str(&test) } let buf = reformat(buf)?; - codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode) + codegen::update(&project_root().join("crates/assists/src/tests/generated.rs"), &buf, mode) } fn hide_hash_comments(text: &str) -> String { diff --git a/xtask/src/codegen/gen_features.rs b/xtask/src/codegen/gen_features.rs new file mode 100644 index 000000000..78268308b --- /dev/null +++ b/xtask/src/codegen/gen_features.rs @@ -0,0 +1,50 @@ +//! Generates descriptors structure for unstable feature from Unstable Book +use std::path::{Path, PathBuf}; + +use quote::quote; +use walkdir::WalkDir; + +use crate::{ + codegen::{project_root, reformat, update, Mode, Result}, + not_bash::{fs2, run}, +}; + +pub fn generate_features(mode: Mode) -> Result<()> { + if !Path::new("./target/rust").exists() { + run!("git clone https://github.com/rust-lang/rust ./target/rust")?; + } + + let contents = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?; + + let destination = project_root().join("crates/ide/src/completion/generated_features.rs"); + update(destination.as_path(), &contents, mode)?; + + Ok(()) +} + +fn generate_descriptor(src_dir: PathBuf) -> Result { + let definitions = ["language-features", "library-features"] + .iter() + .flat_map(|it| WalkDir::new(src_dir.join(it))) + .filter_map(|e| e.ok()) + .filter(|entry| { + // Get all `.md ` files + entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" + }) + .map(|entry| { + let path = entry.path(); + let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_"); + let doc = fs2::read_to_string(path).unwrap(); + + quote! { LintCompletion { label: #feature_ident, description: #doc } } + }); + + let ts = quote! { + use crate::completion::complete_attribute::LintCompletion; + + pub(super) const FEATURES: &[LintCompletion] = &[ + #(#definitions),* + ]; + }; + reformat(ts) +} diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index 2977da2fa..96fdd9216 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs @@ -8,12 +8,12 @@ use std::{ }; use crate::{ - codegen::{self, extract_comment_blocks, update, Mode}, + codegen::{extract_comment_blocks, update, Mode}, project_root, Result, }; pub fn generate_parser_tests(mode: Mode) -> Result<()> { - let tests = tests_from_dir(&project_root().join(Path::new(codegen::GRAMMAR_DIR)))?; + let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; fn install_tests(tests: &HashMap, into: &str, mode: Mode) -> Result<()> { let tests_dir = project_root().join(into); if !tests_dir.is_dir() { @@ -39,8 +39,8 @@ pub fn generate_parser_tests(mode: Mode) -> Result<()> { } Ok(()) } - install_tests(&tests.ok, codegen::OK_INLINE_TESTS_DIR, mode)?; - install_tests(&tests.err, codegen::ERR_INLINE_TESTS_DIR, mode) + install_tests(&tests.ok, "crates/syntax/test_data/parser/inline/ok", mode)?; + install_tests(&tests.err, "crates/syntax/test_data/parser/inline/err", mode) } #[derive(Debug)] diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index df3ec22c8..53ae9f11c 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -14,7 +14,7 @@ use ungrammar::{rust_grammar, Grammar, Rule}; use crate::{ ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, - codegen::{self, reformat, update, Mode}, + codegen::{reformat, update, Mode}, project_root, Result, }; @@ -22,15 +22,15 @@ pub fn generate_syntax(mode: Mode) -> Result<()> { let grammar = rust_grammar(); let ast = lower(&grammar); - let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); + 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)?; - let ast_tokens_file = project_root().join(codegen::AST_TOKENS); + 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)?; - let ast_nodes_file = project_root().join(codegen::AST_NODES); + 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)?; diff --git a/xtask/src/codegen/gen_unstable_future_descriptor.rs b/xtask/src/codegen/gen_unstable_future_descriptor.rs deleted file mode 100644 index 907a9afae..000000000 --- a/xtask/src/codegen/gen_unstable_future_descriptor.rs +++ /dev/null @@ -1,61 +0,0 @@ -//! Generates descriptors structure for unstable feature from Unstable Book - -use crate::codegen::{self, project_root, Mode, Result}; -use crate::codegen::{reformat, update}; -use crate::not_bash::{fs2, pushd, run}; -use proc_macro2::TokenStream; -use quote::quote; -use std::path::PathBuf; -use walkdir::WalkDir; - -fn generate_descriptor(src_dir: PathBuf) -> Result { - let files = WalkDir::new(src_dir.join("language-features")) - .into_iter() - .chain(WalkDir::new(src_dir.join("library-features"))) - .filter_map(|e| e.ok()) - .filter(|entry| { - // Get all `.md ` files - entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md" - }) - .collect::>(); - - let definitions = files - .iter() - .map(|entry| { - let path = entry.path(); - let feature_ident = - format!("{}", path.file_stem().unwrap().to_str().unwrap().replace("-", "_")); - let doc = format!("{}", std::fs::read_to_string(path).unwrap()); - - quote! { LintCompletion { label: #feature_ident, description: #doc } } - }) - .collect::>(); - - let ts = quote! { - use crate::completion::LintCompletion; - - pub(crate) const UNSTABLE_FEATURE_DESCRIPTOR: &[LintCompletion] = &[ - #(#definitions),* - ]; - }; - Ok(ts) -} - -pub fn generate_unstable_future_descriptor(mode: Mode) -> Result<()> { - let path = project_root().join(codegen::STORAGE); - fs2::create_dir_all(path.clone())?; - - let _d = pushd(path.clone()); - run!("git init")?; - run!("git remote add -f origin {}", codegen::REPOSITORY_URL)?; - run!("git pull origin master")?; - - let src_dir = path.join(codegen::REPO_PATH); - let content = generate_descriptor(src_dir)?.to_string(); - - let contents = reformat(content)?; - let destination = project_root().join(codegen::UNSTABLE_FEATURE); - update(destination.as_path(), &contents, mode)?; - - Ok(()) -} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c4a15f4bd..3f4aa5497 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -10,6 +10,7 @@ use std::env; +use codegen::CodegenCmd; use pico_args::Arguments; use xtask::{ codegen::{self, Mode}, @@ -75,14 +76,9 @@ FLAGS: .run() } "codegen" => { + let features = args.contains("--features"); args.finish()?; - codegen::generate_syntax(Mode::Overwrite)?; - codegen::generate_unstable_future_descriptor(Mode::Overwrite)?; - codegen::generate_parser_tests(Mode::Overwrite)?; - codegen::generate_assists_tests(Mode::Overwrite)?; - codegen::generate_assists_docs(Mode::Overwrite)?; - codegen::generate_feature_docs(Mode::Overwrite)?; - Ok(()) + CodegenCmd { features }.run() } "format" => { args.finish()?; diff --git a/xtask/tests/tidy.rs b/xtask/tests/tidy.rs index bec3c630b..4fdff62ee 100644 --- a/xtask/tests/tidy.rs +++ b/xtask/tests/tidy.rs @@ -113,7 +113,7 @@ fn check_todo(path: &Path, text: &str) { // `ast::make`. "ast/make.rs", // The documentation in string literals may contain anything for its own purposes - "completion/unstable_feature_descriptor.rs", + "completion/generated_features.rs", ]; if need_todo.iter().any(|p| path.ends_with(p)) { return; -- cgit v1.2.3