Skip to content

Commit 7389f97

Browse files
James Munnssteveklabnik
James Munns
authored andcommitted
Only the compatibility items from the embedded book PR
PR: rust-lang#56291
1 parent 43b4c4a commit 7389f97

File tree

3 files changed

+100
-25
lines changed

3 files changed

+100
-25
lines changed

src/bootstrap/doc.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::cache::{INTERNER, Interned};
2323
use crate::config::Config;
2424

2525
macro_rules! book {
26-
($($name:ident, $path:expr, $book_name:expr;)+) => {
26+
($($name:ident, $path:expr, $book_name:expr, $book_ver:expr;)+) => {
2727
$(
2828
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2929
pub struct $name {
@@ -49,26 +49,36 @@ macro_rules! book {
4949
builder.ensure(Rustbook {
5050
target: self.target,
5151
name: INTERNER.intern_str($book_name),
52+
version: $book_ver,
5253
})
5354
}
5455
}
5556
)+
5657
}
5758
}
5859

60+
// NOTE: When adding a book here, make sure to ALSO build the book by
61+
// adding a build step in `src/bootstrap/builder.rs`!
5962
book!(
60-
Nomicon, "src/doc/nomicon", "nomicon";
61-
Reference, "src/doc/reference", "reference";
62-
EditionGuide, "src/doc/edition-guide", "edition-guide";
63-
RustdocBook, "src/doc/rustdoc", "rustdoc";
64-
RustcBook, "src/doc/rustc", "rustc";
65-
RustByExample, "src/doc/rust-by-example", "rust-by-example";
63+
EditionGuide, "src/doc/edition-guide", "edition-guide", RustbookVersion::MdBook1;
64+
Nomicon, "src/doc/nomicon", "nomicon", RustbookVersion::MdBook1;
65+
Reference, "src/doc/reference", "reference", RustbookVersion::MdBook1;
66+
RustByExample, "src/doc/rust-by-example", "rust-by-example", RustbookVersion::MdBook1;
67+
RustcBook, "src/doc/rustc", "rustc", RustbookVersion::MdBook1;
68+
RustdocBook, "src/doc/rustdoc", "rustdoc", RustbookVersion::MdBook1;
6669
);
6770

71+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
72+
enum RustbookVersion {
73+
MdBook1,
74+
MdBook2,
75+
}
76+
6877
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
6978
struct Rustbook {
7079
target: Interned<String>,
7180
name: Interned<String>,
81+
version: RustbookVersion,
7282
}
7383

7484
impl Step for Rustbook {
@@ -90,6 +100,7 @@ impl Step for Rustbook {
90100
target: self.target,
91101
name: self.name,
92102
src: INTERNER.intern_path(src),
103+
version: self.version,
93104
});
94105
}
95106
}
@@ -122,6 +133,7 @@ impl Step for UnstableBook {
122133
target: self.target,
123134
name: INTERNER.intern_str("unstable-book"),
124135
src: builder.md_doc_out(self.target),
136+
version: RustbookVersion::MdBook1,
125137
})
126138
}
127139
}
@@ -175,6 +187,7 @@ struct RustbookSrc {
175187
target: Interned<String>,
176188
name: Interned<String>,
177189
src: Interned<PathBuf>,
190+
version: RustbookVersion,
178191
}
179192

180193
impl Step for RustbookSrc {
@@ -205,11 +218,19 @@ impl Step for RustbookSrc {
205218
}
206219
builder.info(&format!("Rustbook ({}) - {}", target, name));
207220
let _ = fs::remove_dir_all(&out);
221+
222+
let vers = match self.version {
223+
RustbookVersion::MdBook1 => "1",
224+
RustbookVersion::MdBook2 => "2",
225+
};
226+
208227
builder.run(rustbook_cmd
209228
.arg("build")
210229
.arg(&src)
211230
.arg("-d")
212-
.arg(out));
231+
.arg(out)
232+
.arg("-m")
233+
.arg(vers));
213234
}
214235
}
215236

@@ -255,6 +276,7 @@ impl Step for TheBook {
255276
builder.ensure(Rustbook {
256277
target,
257278
name: INTERNER.intern_string(name.to_string()),
279+
version: RustbookVersion::MdBook1,
258280
});
259281

260282
// building older edition redirects
@@ -263,18 +285,21 @@ impl Step for TheBook {
263285
builder.ensure(Rustbook {
264286
target,
265287
name: INTERNER.intern_string(source_name),
288+
version: RustbookVersion::MdBook1,
266289
});
267290

268291
let source_name = format!("{}/second-edition", name);
269292
builder.ensure(Rustbook {
270293
target,
271294
name: INTERNER.intern_string(source_name),
295+
version: RustbookVersion::MdBook1,
272296
});
273297

274298
let source_name = format!("{}/2018-edition", name);
275299
builder.ensure(Rustbook {
276300
target,
277301
name: INTERNER.intern_string(source_name),
302+
version: RustbookVersion::MdBook1,
278303
});
279304

280305
// build the version info page and CSS

src/tools/rustbook/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
cargo-features = ["rename-dependency"]
2+
13
[package]
24
authors = ["The Rust Project Developers"]
35
name = "rustbook"
46
version = "0.1.0"
57
license = "MIT/Apache-2.0"
8+
edition = "2018"
69

710
[dependencies]
811
clap = "2.25.0"
912

10-
[dependencies.mdbook]
13+
[dependencies.mdbook_2]
14+
package = "mdbook"
15+
version = "0.2.2"
16+
default-features = false
17+
features = ["search"]
18+
19+
[dependencies.mdbook_1]
20+
package = "mdbook"
1121
version = "0.1.7"
1222
default-features = false
1323
features = ["search"]

src/tools/rustbook/src/main.rs

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
//
2-
extern crate mdbook;
3-
#[macro_use]
4-
extern crate clap;
2+
use clap::{crate_version};
53

64
use std::env;
75
use std::path::{Path, PathBuf};
86

97
use clap::{App, ArgMatches, SubCommand, AppSettings};
108

11-
use mdbook::MDBook;
12-
use mdbook::errors::Result;
9+
use mdbook_1::{MDBook as MDBook1};
10+
use mdbook_1::errors::{Result as Result1};
11+
12+
use mdbook_2::{MDBook as MDBook2};
13+
use mdbook_2::errors::{Result as Result2};
1314

1415
fn main() {
1516
let d_message = "-d, --dest-dir=[dest-dir]
1617
'The output directory for your book{n}(Defaults to ./book when omitted)'";
1718
let dir_message = "[dir]
1819
'A directory for your book{n}(Defaults to Current Directory when omitted)'";
20+
let vers_message = "-m, --mdbook-vers=[md-version]
21+
'The version of mdbook to use for your book{n}(Defaults to 1 when omitted)'";
1922

2023
let matches = App::new("rustbook")
2124
.about("Build a book with mdBook")
@@ -25,29 +28,66 @@ fn main() {
2528
.subcommand(SubCommand::with_name("build")
2629
.about("Build the book from the markdown files")
2730
.arg_from_usage(d_message)
28-
.arg_from_usage(dir_message))
31+
.arg_from_usage(dir_message)
32+
.arg_from_usage(vers_message))
2933
.get_matches();
3034

3135
// Check which subcomamnd the user ran...
32-
let res = match matches.subcommand() {
33-
("build", Some(sub_matches)) => build(sub_matches),
36+
match matches.subcommand() {
37+
("build", Some(sub_matches)) => {
38+
match sub_matches.value_of("mdbook-vers") {
39+
None | Some("1") => {
40+
if let Err(e) = build_1(sub_matches) {
41+
eprintln!("Error: {}", e);
42+
43+
for cause in e.iter().skip(1) {
44+
eprintln!("\tCaused By: {}", cause);
45+
}
46+
47+
::std::process::exit(101);
48+
}
49+
}
50+
Some("2") => {
51+
if let Err(e) = build_2(sub_matches) {
52+
eprintln!("Error: {}", e);
53+
54+
for cause in e.iter().skip(1) {
55+
eprintln!("\tCaused By: {}", cause);
56+
}
57+
58+
::std::process::exit(101);
59+
}
60+
}
61+
_ => {
62+
panic!("Invalid mdBook version! Select '1' or '2'");
63+
}
64+
};
65+
},
3466
(_, _) => unreachable!(),
3567
};
68+
}
3669

37-
if let Err(e) = res {
38-
eprintln!("Error: {}", e);
70+
// Build command implementation
71+
pub fn build_1(args: &ArgMatches) -> Result1<()> {
72+
let book_dir = get_book_dir(args);
73+
let mut book = MDBook1::load(&book_dir)?;
3974

40-
for cause in e.iter().skip(1) {
41-
eprintln!("\tCaused By: {}", cause);
42-
}
75+
// Set this to allow us to catch bugs in advance.
76+
book.config.build.create_missing = false;
4377

44-
::std::process::exit(101);
78+
if let Some(dest_dir) = args.value_of("dest-dir") {
79+
book.config.build.build_dir = PathBuf::from(dest_dir);
4580
}
81+
82+
book.build()?;
83+
84+
Ok(())
4685
}
86+
4787
// Build command implementation
48-
pub fn build(args: &ArgMatches) -> Result<()> {
88+
pub fn build_2(args: &ArgMatches) -> Result2<()> {
4989
let book_dir = get_book_dir(args);
50-
let mut book = MDBook::load(&book_dir)?;
90+
let mut book = MDBook2::load(&book_dir)?;
5191

5292
// Set this to allow us to catch bugs in advance.
5393
book.config.build.create_missing = false;

0 commit comments

Comments
 (0)