aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_unstable_future_descriptor.rs61
1 files changed, 61 insertions, 0 deletions
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..907a9afae
--- /dev/null
+++ b/xtask/src/codegen/gen_unstable_future_descriptor.rs
@@ -0,0 +1,61 @@
1//! Generates descriptors structure for unstable feature from Unstable Book
2
3use crate::codegen::{self, project_root, Mode, Result};
4use crate::codegen::{reformat, update};
5use crate::not_bash::{fs2, pushd, run};
6use proc_macro2::TokenStream;
7use quote::quote;
8use std::path::PathBuf;
9use walkdir::WalkDir;
10
11fn generate_descriptor(src_dir: PathBuf) -> Result<TokenStream> {
12 let files = WalkDir::new(src_dir.join("language-features"))
13 .into_iter()
14 .chain(WalkDir::new(src_dir.join("library-features")))
15 .filter_map(|e| e.ok())
16 .filter(|entry| {
17 // Get all `.md ` files
18 entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
19 })
20 .collect::<Vec<_>>();
21
22 let definitions = files
23 .iter()
24 .map(|entry| {
25 let path = entry.path();
26 let feature_ident =
27 format!("{}", path.file_stem().unwrap().to_str().unwrap().replace("-", "_"));
28 let doc = format!("{}", std::fs::read_to_string(path).unwrap());
29
30 quote! { LintCompletion { label: #feature_ident, description: #doc } }
31 })
32 .collect::<Vec<_>>();
33
34 let ts = quote! {
35 use crate::completion::LintCompletion;
36
37 pub(crate) const UNSTABLE_FEATURE_DESCRIPTOR: &[LintCompletion] = &[
38 #(#definitions),*
39 ];
40 };
41 Ok(ts)
42}
43
44pub fn generate_unstable_future_descriptor(mode: Mode) -> Result<()> {
45 let path = project_root().join(codegen::STORAGE);
46 fs2::create_dir_all(path.clone())?;
47
48 let _d = pushd(path.clone());
49 run!("git init")?;
50 run!("git remote add -f origin {}", codegen::REPOSITORY_URL)?;
51 run!("git pull origin master")?;
52
53 let src_dir = path.join(codegen::REPO_PATH);
54 let content = generate_descriptor(src_dir)?.to_string();
55
56 let contents = reformat(content)?;
57 let destination = project_root().join(codegen::UNSTABLE_FEATURE);
58 update(destination.as_path(), &contents, mode)?;
59
60 Ok(())
61}