From 7389f97cde3da06fd7025e3eb93f4ab3c10c7d40 Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 10 Jan 2019 20:37:51 +0100 Subject: [PATCH] Only the compatibility items from the embedded book PR PR: https://github.com/rust-lang/rust/pull/56291 --- src/bootstrap/doc.rs | 41 +++++++++++++---- src/tools/rustbook/Cargo.toml | 12 ++++- src/tools/rustbook/src/main.rs | 80 +++++++++++++++++++++++++--------- 3 files changed, 104 insertions(+), 29 deletions(-) diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index eec193c21f5..c428f29d706 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -23,7 +23,7 @@ use crate::cache::{INTERNER, Interned}; use crate::config::Config; macro_rules! book { - ($($name:ident, $path:expr, $book_name:expr;)+) => { + ($($name:ident, $path:expr, $book_name:expr, $book_ver:expr;)+) => { $( #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct $name { @@ -49,6 +49,7 @@ macro_rules! book { builder.ensure(Rustbook { target: self.target, name: INTERNER.intern_str($book_name), + version: $book_ver, }) } } @@ -56,19 +57,28 @@ macro_rules! book { } } +// NOTE: When adding a book here, make sure to ALSO build the book by +// adding a build step in `src/bootstrap/builder.rs`! book!( - Nomicon, "src/doc/nomicon", "nomicon"; - Reference, "src/doc/reference", "reference"; - EditionGuide, "src/doc/edition-guide", "edition-guide"; - RustdocBook, "src/doc/rustdoc", "rustdoc"; - RustcBook, "src/doc/rustc", "rustc"; - RustByExample, "src/doc/rust-by-example", "rust-by-example"; + EditionGuide, "src/doc/edition-guide", "edition-guide", RustbookVersion::MdBook1; + Nomicon, "src/doc/nomicon", "nomicon", RustbookVersion::MdBook1; + Reference, "src/doc/reference", "reference", RustbookVersion::MdBook1; + RustByExample, "src/doc/rust-by-example", "rust-by-example", RustbookVersion::MdBook1; + RustcBook, "src/doc/rustc", "rustc", RustbookVersion::MdBook1; + RustdocBook, "src/doc/rustdoc", "rustdoc", RustbookVersion::MdBook1; ); +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +enum RustbookVersion { + MdBook1, + MdBook2, +} + #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] struct Rustbook { target: Interned, name: Interned, + version: RustbookVersion, } impl Step for Rustbook { @@ -90,6 +100,7 @@ impl Step for Rustbook { target: self.target, name: self.name, src: INTERNER.intern_path(src), + version: self.version, }); } } @@ -122,6 +133,7 @@ impl Step for UnstableBook { target: self.target, name: INTERNER.intern_str("unstable-book"), src: builder.md_doc_out(self.target), + version: RustbookVersion::MdBook1, }) } } @@ -175,6 +187,7 @@ struct RustbookSrc { target: Interned, name: Interned, src: Interned, + version: RustbookVersion, } impl Step for RustbookSrc { @@ -205,11 +218,19 @@ impl Step for RustbookSrc { } builder.info(&format!("Rustbook ({}) - {}", target, name)); let _ = fs::remove_dir_all(&out); + + let vers = match self.version { + RustbookVersion::MdBook1 => "1", + RustbookVersion::MdBook2 => "2", + }; + builder.run(rustbook_cmd .arg("build") .arg(&src) .arg("-d") - .arg(out)); + .arg(out) + .arg("-m") + .arg(vers)); } } @@ -255,6 +276,7 @@ impl Step for TheBook { builder.ensure(Rustbook { target, name: INTERNER.intern_string(name.to_string()), + version: RustbookVersion::MdBook1, }); // building older edition redirects @@ -263,18 +285,21 @@ impl Step for TheBook { builder.ensure(Rustbook { target, name: INTERNER.intern_string(source_name), + version: RustbookVersion::MdBook1, }); let source_name = format!("{}/second-edition", name); builder.ensure(Rustbook { target, name: INTERNER.intern_string(source_name), + version: RustbookVersion::MdBook1, }); let source_name = format!("{}/2018-edition", name); builder.ensure(Rustbook { target, name: INTERNER.intern_string(source_name), + version: RustbookVersion::MdBook1, }); // build the version info page and CSS diff --git a/src/tools/rustbook/Cargo.toml b/src/tools/rustbook/Cargo.toml index 5e11646ee4c..27454431228 100644 --- a/src/tools/rustbook/Cargo.toml +++ b/src/tools/rustbook/Cargo.toml @@ -1,13 +1,23 @@ +cargo-features = ["rename-dependency"] + [package] authors = ["The Rust Project Developers"] name = "rustbook" version = "0.1.0" license = "MIT/Apache-2.0" +edition = "2018" [dependencies] clap = "2.25.0" -[dependencies.mdbook] +[dependencies.mdbook_2] +package = "mdbook" +version = "0.2.2" +default-features = false +features = ["search"] + +[dependencies.mdbook_1] +package = "mdbook" version = "0.1.7" default-features = false features = ["search"] diff --git a/src/tools/rustbook/src/main.rs b/src/tools/rustbook/src/main.rs index 80a85dc2ac0..5a6246347cc 100644 --- a/src/tools/rustbook/src/main.rs +++ b/src/tools/rustbook/src/main.rs @@ -1,21 +1,24 @@ // -extern crate mdbook; -#[macro_use] -extern crate clap; +use clap::{crate_version}; use std::env; use std::path::{Path, PathBuf}; use clap::{App, ArgMatches, SubCommand, AppSettings}; -use mdbook::MDBook; -use mdbook::errors::Result; +use mdbook_1::{MDBook as MDBook1}; +use mdbook_1::errors::{Result as Result1}; + +use mdbook_2::{MDBook as MDBook2}; +use mdbook_2::errors::{Result as Result2}; fn main() { let d_message = "-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'"; let dir_message = "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'"; + let vers_message = "-m, --mdbook-vers=[md-version] +'The version of mdbook to use for your book{n}(Defaults to 1 when omitted)'"; let matches = App::new("rustbook") .about("Build a book with mdBook") @@ -25,29 +28,66 @@ fn main() { .subcommand(SubCommand::with_name("build") .about("Build the book from the markdown files") .arg_from_usage(d_message) - .arg_from_usage(dir_message)) + .arg_from_usage(dir_message) + .arg_from_usage(vers_message)) .get_matches(); // Check which subcomamnd the user ran... - let res = match matches.subcommand() { - ("build", Some(sub_matches)) => build(sub_matches), + match matches.subcommand() { + ("build", Some(sub_matches)) => { + match sub_matches.value_of("mdbook-vers") { + None | Some("1") => { + if let Err(e) = build_1(sub_matches) { + eprintln!("Error: {}", e); + + for cause in e.iter().skip(1) { + eprintln!("\tCaused By: {}", cause); + } + + ::std::process::exit(101); + } + } + Some("2") => { + if let Err(e) = build_2(sub_matches) { + eprintln!("Error: {}", e); + + for cause in e.iter().skip(1) { + eprintln!("\tCaused By: {}", cause); + } + + ::std::process::exit(101); + } + } + _ => { + panic!("Invalid mdBook version! Select '1' or '2'"); + } + }; + }, (_, _) => unreachable!(), }; - - if let Err(e) = res { - eprintln!("Error: {}", e); - - for cause in e.iter().skip(1) { - eprintln!("\tCaused By: {}", cause); - } - - ::std::process::exit(101); - } } + // Build command implementation -pub fn build(args: &ArgMatches) -> Result<()> { +pub fn build_1(args: &ArgMatches) -> Result1<()> { let book_dir = get_book_dir(args); - let mut book = MDBook::load(&book_dir)?; + let mut book = MDBook1::load(&book_dir)?; + + // Set this to allow us to catch bugs in advance. + book.config.build.create_missing = false; + + if let Some(dest_dir) = args.value_of("dest-dir") { + book.config.build.build_dir = PathBuf::from(dest_dir); + } + + book.build()?; + + Ok(()) +} + +// Build command implementation +pub fn build_2(args: &ArgMatches) -> Result2<()> { + let book_dir = get_book_dir(args); + let mut book = MDBook2::load(&book_dir)?; // Set this to allow us to catch bugs in advance. book.config.build.create_missing = false;