From 1a43a0f63e0008787225abb6fb2baef97b6a39e0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 9 Aug 2020 20:33:47 +0700 Subject: Added competition for unstable features Added xtask for download unstable book from rust repository and codegene for it. Also small changes from lint --- xtask/src/codegen.rs | 8 ++++ .../src/codegen/gen_unstable_future_descriptor.rs | 55 ++++++++++++++++++++++ xtask/src/main.rs | 1 + 3 files changed, 64 insertions(+) create mode 100644 xtask/src/codegen/gen_unstable_future_descriptor.rs (limited to 'xtask/src') diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index f5f4b964a..209caacc3 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -9,6 +9,7 @@ mod gen_syntax; mod gen_parser_tests; mod gen_assists_docs; mod gen_feature_docs; +mod gen_unstable_future_descriptor; use std::{ fmt, mem, @@ -22,8 +23,12 @@ pub use self::{ gen_feature_docs::generate_feature_docs, 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/ra_parser/src/grammar"; const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; @@ -35,6 +40,9 @@ const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs"; const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers"; const ASSISTS_TESTS: &str = "crates/ra_assists/src/tests/generated.rs"; +const REPOSITORY_URL: &str = "https://github.com/rust-lang/rust"; +const UNSTABLE_FEATURE: &str = "crates/ra_ide/src/completion/unstable_feature_descriptor.rs"; + #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Mode { Overwrite, diff --git a/xtask/src/codegen/gen_unstable_future_descriptor.rs b/xtask/src/codegen/gen_unstable_future_descriptor.rs new file mode 100644 index 000000000..09c4f61f2 --- /dev/null +++ b/xtask/src/codegen/gen_unstable_future_descriptor.rs @@ -0,0 +1,55 @@ +//! Generates descriptors structure for unstable feature from Unstable Book + +use crate::{ + codegen::{self, project_root, Mode, Result}, +}; +use std::process::Command; +use std::fs; +use std::path::{Path, PathBuf}; +use walkdir::{DirEntry, WalkDir}; +use quote::{format_ident, quote}; +use crate::codegen::update; + +pub fn generate_unstable_future_descriptor(mode: Mode) -> Result<()> { + let path = project_root().join(codegen::STORAGE); + fs::create_dir_all(path.clone())?; + + Command::new("git").current_dir(path.clone()).arg("init").output()?; + Command::new("git").current_dir(path.clone()).args(&["remote", "add", "-f", "origin", codegen::REPOSITORY_URL]).output()?; + Command::new("git").current_dir(path.clone()).args(&["sparse-checkout", "set", "/src/doc/unstable-book/src/"]).output()?; + Command::new("git").current_dir(path.clone()).args(&["pull", "origin", "master"]).output()?; + //TODO: check git, and do pull + + let src_dir = path.join("src/doc/unstable-book/src"); + 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().map(|ext| ext == "md").unwrap_or(false) + }) + .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; + + const UNSTABLE_FEATURE_DESCRIPTOR: &[LintCompletion] = &[ + #(#definitions),* + ]; + }; + + let destination = project_root().join(codegen::UNSTABLE_FEATURE); + let contents = crate::reformat(ts.to_string())?; + update(destination.as_path(), &contents, mode)?; + + Ok(()) +} \ No newline at end of file diff --git a/xtask/src/main.rs b/xtask/src/main.rs index b69b884e5..71caff248 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -76,6 +76,7 @@ FLAGS: "codegen" => { 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)?; -- cgit v1.2.3