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/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 ---------------------- 5 files changed, 60 insertions(+), 71 deletions(-) create mode 100644 xtask/src/codegen/gen_features.rs delete mode 100644 xtask/src/codegen/gen_unstable_future_descriptor.rs (limited to 'xtask/src/codegen') 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(()) -} -- cgit v1.2.3