]> git.lizzy.rs Git - rust.git/blob - xtask/src/codegen/gen_features.rs
Cleanup feature generation
[rust.git] / xtask / src / codegen / gen_features.rs
1 //! Generates descriptors structure for unstable feature from Unstable Book
2 use std::path::{Path, PathBuf};
3
4 use quote::quote;
5 use walkdir::WalkDir;
6
7 use crate::{
8     codegen::{project_root, reformat, update, Mode, Result},
9     not_bash::{fs2, run},
10 };
11
12 pub fn generate_features(mode: Mode) -> Result<()> {
13     if !Path::new("./target/rust").exists() {
14         run!("git clone https://github.com/rust-lang/rust ./target/rust")?;
15     }
16
17     let contents = generate_descriptor("./target/rust/src/doc/unstable-book/src".into())?;
18
19     let destination = project_root().join("crates/ide/src/completion/generated_features.rs");
20     update(destination.as_path(), &contents, mode)?;
21
22     Ok(())
23 }
24
25 fn generate_descriptor(src_dir: PathBuf) -> Result<String> {
26     let definitions = ["language-features", "library-features"]
27         .iter()
28         .flat_map(|it| WalkDir::new(src_dir.join(it)))
29         .filter_map(|e| e.ok())
30         .filter(|entry| {
31             // Get all `.md ` files
32             entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
33         })
34         .map(|entry| {
35             let path = entry.path();
36             let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
37             let doc = fs2::read_to_string(path).unwrap();
38
39             quote! { LintCompletion { label: #feature_ident, description: #doc } }
40         });
41
42     let ts = quote! {
43         use crate::completion::complete_attribute::LintCompletion;
44
45         pub(super) const FEATURES:  &[LintCompletion] = &[
46             #(#definitions),*
47         ];
48     };
49     reformat(ts)
50 }