aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_unstable_future_descriptor.rs
blob: 907a9afaeee90a685d63e463c11fc2475c3dd00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! 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<TokenStream> {
    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::<Vec<_>>();

    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::<Vec<_>>();

    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(())
}