]> git.lizzy.rs Git - rust.git/blob - src/tools/unstable-book-gen/src/main.rs
Rollup merge of #98204 - Kixiron:stable-unzip, r=thomcc
[rust.git] / src / tools / unstable-book-gen / src / main.rs
1 //! Auto-generate stub docs for the unstable book
2
3 use std::collections::BTreeSet;
4 use std::env;
5 use std::fs::{self, write};
6 use std::path::Path;
7 use tidy::features::{collect_lang_features, collect_lib_features, Features};
8 use tidy::t;
9 use tidy::unstable_book::{
10     collect_unstable_book_section_file_names, collect_unstable_feature_names, LANG_FEATURES_DIR,
11     LIB_FEATURES_DIR, PATH_STR,
12 };
13
14 fn generate_stub_issue(path: &Path, name: &str, issue: u32) {
15     let content = format!(include_str!("stub-issue.md"), name = name, issue = issue);
16     t!(write(path, content), path);
17 }
18
19 fn generate_stub_no_issue(path: &Path, name: &str) {
20     let content = format!(include_str!("stub-no-issue.md"), name = name);
21     t!(write(path, content), path);
22 }
23
24 fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String {
25     set.iter()
26         .map(|ref n| format!("    - [{}]({}/{}.md)", n.replace('-', "_"), dir, n))
27         .fold("".to_owned(), |s, a| s + &a + "\n")
28 }
29
30 fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Features) {
31     let compiler_flags = collect_unstable_book_section_file_names(&path.join("src/compiler-flags"));
32
33     let compiler_flags_str = set_to_summary_str(&compiler_flags, "compiler-flags");
34
35     let unstable_lang_features = collect_unstable_feature_names(&lang_features);
36     let unstable_lib_features = collect_unstable_feature_names(&lib_features);
37
38     let lang_features_str = set_to_summary_str(&unstable_lang_features, "language-features");
39     let lib_features_str = set_to_summary_str(&unstable_lib_features, "library-features");
40
41     let summary_path = path.join("src/SUMMARY.md");
42     let content = format!(
43         include_str!("SUMMARY.md"),
44         compiler_flags = compiler_flags_str,
45         language_features = lang_features_str,
46         library_features = lib_features_str
47     );
48     t!(write(&summary_path, content), summary_path);
49 }
50
51 fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) {
52     let unstable_features = collect_unstable_feature_names(features);
53     let unstable_section_file_names = collect_unstable_book_section_file_names(src);
54     t!(fs::create_dir_all(&out));
55     for feature_name in &unstable_features - &unstable_section_file_names {
56         let feature_name_underscore = feature_name.replace('-', "_");
57         let file_name = format!("{feature_name}.md");
58         let out_file_path = out.join(&file_name);
59         let feature = &features[&feature_name_underscore];
60
61         if let Some(issue) = feature.tracking_issue {
62             generate_stub_issue(&out_file_path, &feature_name_underscore, issue.get());
63         } else {
64             generate_stub_no_issue(&out_file_path, &feature_name_underscore);
65         }
66     }
67 }
68
69 fn copy_recursive(from: &Path, to: &Path) {
70     for entry in t!(fs::read_dir(from)) {
71         let e = t!(entry);
72         let t = t!(e.metadata());
73         let dest = &to.join(e.file_name());
74         if t.is_file() {
75             t!(fs::copy(&e.path(), dest));
76         } else if t.is_dir() {
77             t!(fs::create_dir_all(dest));
78             copy_recursive(&e.path(), dest);
79         }
80     }
81 }
82
83 fn main() {
84     let library_path_str = env::args_os().nth(1).expect("library/ path required");
85     let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required");
86     let src_path_str = env::args_os().nth(3).expect("src/ path required");
87     let dest_path_str = env::args_os().nth(4).expect("destination path required");
88     let library_path = Path::new(&library_path_str);
89     let compiler_path = Path::new(&compiler_path_str);
90     let src_path = Path::new(&src_path_str);
91     let dest_path = Path::new(&dest_path_str);
92
93     let lang_features = collect_lang_features(compiler_path, &mut false);
94     let lib_features = collect_lib_features(library_path)
95         .into_iter()
96         .filter(|&(ref name, _)| !lang_features.contains_key(name))
97         .collect();
98
99     let doc_src_path = src_path.join(PATH_STR);
100
101     t!(fs::create_dir_all(&dest_path));
102
103     generate_unstable_book_files(
104         &doc_src_path.join(LANG_FEATURES_DIR),
105         &dest_path.join(LANG_FEATURES_DIR),
106         &lang_features,
107     );
108     generate_unstable_book_files(
109         &doc_src_path.join(LIB_FEATURES_DIR),
110         &dest_path.join(LIB_FEATURES_DIR),
111         &lib_features,
112     );
113
114     copy_recursive(&doc_src_path, &dest_path);
115
116     generate_summary(&dest_path, &lang_features, &lib_features);
117 }