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