summaryrefslogtreecommitdiffstats
path: root/src/tools/unstable-book-gen
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/unstable-book-gen
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/unstable-book-gen')
-rw-r--r--src/tools/unstable-book-gen/Cargo.toml12
-rw-r--r--src/tools/unstable-book-gen/src/SUMMARY.md8
-rw-r--r--src/tools/unstable-book-gen/src/main.rs130
-rw-r--r--src/tools/unstable-book-gen/src/stub-issue.md7
-rw-r--r--src/tools/unstable-book-gen/src/stub-no-issue.md5
5 files changed, 162 insertions, 0 deletions
diff --git a/src/tools/unstable-book-gen/Cargo.toml b/src/tools/unstable-book-gen/Cargo.toml
new file mode 100644
index 000000000..73e5a91be
--- /dev/null
+++ b/src/tools/unstable-book-gen/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "unstable-book-gen"
+version = "0.1.0"
+license = "MIT OR Apache-2.0"
+edition = "2021"
+
+[dependencies]
+tidy = { path = "../tidy" }
+
+# not actually needed but required for now to unify the feature selection of
+# `num-traits` between this and `rustbook`
+num-traits = "0.2"
diff --git a/src/tools/unstable-book-gen/src/SUMMARY.md b/src/tools/unstable-book-gen/src/SUMMARY.md
new file mode 100644
index 000000000..933c928e2
--- /dev/null
+++ b/src/tools/unstable-book-gen/src/SUMMARY.md
@@ -0,0 +1,8 @@
+[The Unstable Book](the-unstable-book.md)
+
+- [Compiler flags](compiler-flags.md)
+{compiler_flags}
+- [Language features](language-features.md)
+{language_features}
+- [Library Features](library-features.md)
+{library_features}
diff --git a/src/tools/unstable-book-gen/src/main.rs b/src/tools/unstable-book-gen/src/main.rs
new file mode 100644
index 000000000..5a0477b4b
--- /dev/null
+++ b/src/tools/unstable-book-gen/src/main.rs
@@ -0,0 +1,130 @@
+//! Auto-generate stub docs for the unstable book
+
+use std::collections::BTreeSet;
+use std::env;
+use std::fs::{self, File};
+use std::io::Write;
+use std::path::Path;
+use tidy::features::{collect_lang_features, collect_lib_features, Features};
+use tidy::unstable_book::{
+ collect_unstable_book_section_file_names, collect_unstable_feature_names, LANG_FEATURES_DIR,
+ LIB_FEATURES_DIR, PATH_STR,
+};
+
+/// A helper macro to `unwrap` a result except also print out details like:
+///
+/// * The file/line of the panic
+/// * The expression that failed
+/// * The error itself
+macro_rules! t {
+ ($e:expr) => {
+ match $e {
+ Ok(e) => e,
+ Err(e) => panic!("{} failed with {}", stringify!($e), e),
+ }
+ };
+}
+
+fn generate_stub_issue(path: &Path, name: &str, issue: u32) {
+ let mut file = t!(File::create(path));
+ t!(write!(file, include_str!("stub-issue.md"), name = name, issue = issue));
+}
+
+fn generate_stub_no_issue(path: &Path, name: &str) {
+ let mut file = t!(File::create(path));
+ t!(write!(file, include_str!("stub-no-issue.md"), name = name));
+}
+
+fn set_to_summary_str(set: &BTreeSet<String>, dir: &str) -> String {
+ set.iter()
+ .map(|ref n| format!(" - [{}]({}/{}.md)", n.replace('-', "_"), dir, n))
+ .fold("".to_owned(), |s, a| s + &a + "\n")
+}
+
+fn generate_summary(path: &Path, lang_features: &Features, lib_features: &Features) {
+ let compiler_flags = collect_unstable_book_section_file_names(&path.join("src/compiler-flags"));
+
+ let compiler_flags_str = set_to_summary_str(&compiler_flags, "compiler-flags");
+
+ let unstable_lang_features = collect_unstable_feature_names(&lang_features);
+ let unstable_lib_features = collect_unstable_feature_names(&lib_features);
+
+ let lang_features_str = set_to_summary_str(&unstable_lang_features, "language-features");
+ let lib_features_str = set_to_summary_str(&unstable_lib_features, "library-features");
+
+ let mut file = t!(File::create(&path.join("src/SUMMARY.md")));
+ t!(file.write_fmt(format_args!(
+ include_str!("SUMMARY.md"),
+ compiler_flags = compiler_flags_str,
+ language_features = lang_features_str,
+ library_features = lib_features_str
+ )));
+}
+
+fn generate_unstable_book_files(src: &Path, out: &Path, features: &Features) {
+ let unstable_features = collect_unstable_feature_names(features);
+ let unstable_section_file_names = collect_unstable_book_section_file_names(src);
+ t!(fs::create_dir_all(&out));
+ for feature_name in &unstable_features - &unstable_section_file_names {
+ let feature_name_underscore = feature_name.replace('-', "_");
+ let file_name = format!("{feature_name}.md");
+ let out_file_path = out.join(&file_name);
+ let feature = &features[&feature_name_underscore];
+
+ if let Some(issue) = feature.tracking_issue {
+ generate_stub_issue(&out_file_path, &feature_name_underscore, issue.get());
+ } else {
+ generate_stub_no_issue(&out_file_path, &feature_name_underscore);
+ }
+ }
+}
+
+fn copy_recursive(from: &Path, to: &Path) {
+ for entry in t!(fs::read_dir(from)) {
+ let e = t!(entry);
+ let t = t!(e.metadata());
+ let dest = &to.join(e.file_name());
+ if t.is_file() {
+ t!(fs::copy(&e.path(), dest));
+ } else if t.is_dir() {
+ t!(fs::create_dir_all(dest));
+ copy_recursive(&e.path(), dest);
+ }
+ }
+}
+
+fn main() {
+ let library_path_str = env::args_os().nth(1).expect("library/ path required");
+ let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required");
+ let src_path_str = env::args_os().nth(3).expect("src/ path required");
+ let dest_path_str = env::args_os().nth(4).expect("destination path required");
+ let library_path = Path::new(&library_path_str);
+ let compiler_path = Path::new(&compiler_path_str);
+ let src_path = Path::new(&src_path_str);
+ let dest_path = Path::new(&dest_path_str);
+
+ let lang_features = collect_lang_features(compiler_path, &mut false);
+ let lib_features = collect_lib_features(library_path)
+ .into_iter()
+ .filter(|&(ref name, _)| !lang_features.contains_key(name))
+ .collect();
+
+ let doc_src_path = src_path.join(PATH_STR);
+
+ t!(fs::create_dir_all(&dest_path));
+
+ generate_unstable_book_files(
+ &doc_src_path.join(LANG_FEATURES_DIR),
+ &dest_path.join(LANG_FEATURES_DIR),
+ &lang_features,
+ );
+ generate_unstable_book_files(
+ &doc_src_path.join(LIB_FEATURES_DIR),
+ &dest_path.join(LIB_FEATURES_DIR),
+ &lib_features,
+ );
+
+ copy_recursive(&doc_src_path, &dest_path);
+
+ generate_summary(&dest_path, &lang_features, &lib_features);
+}
diff --git a/src/tools/unstable-book-gen/src/stub-issue.md b/src/tools/unstable-book-gen/src/stub-issue.md
new file mode 100644
index 000000000..8698fb727
--- /dev/null
+++ b/src/tools/unstable-book-gen/src/stub-issue.md
@@ -0,0 +1,7 @@
+# `{name}`
+
+The tracking issue for this feature is: [#{issue}]
+
+[#{issue}]: https://github.com/rust-lang/rust/issues/{issue}
+
+------------------------
diff --git a/src/tools/unstable-book-gen/src/stub-no-issue.md b/src/tools/unstable-book-gen/src/stub-no-issue.md
new file mode 100644
index 000000000..3da140633
--- /dev/null
+++ b/src/tools/unstable-book-gen/src/stub-no-issue.md
@@ -0,0 +1,5 @@
+# `{name}`
+
+This feature has no tracking issue, and is therefore likely internal to the compiler, not being intended for general use.
+
+------------------------