about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2019-04-07 09:59:33 -0300
committerAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2019-07-06 11:05:22 -0300
commitf80697215fb1596ea6ccdbace46050fea661bffe (patch)
treef59aac0d48490f1f2388a681ad135797c6769512 /src
parentd8a6ccfb4a1e41978fcf98b8cf03146b8b0264f3 (diff)
Add linkcheck command to rustbook tool
Diffstat (limited to 'src')
-rw-r--r--src/bootstrap/test.rs26
-rw-r--r--src/tools/rustbook/Cargo.toml2
-rw-r--r--src/tools/rustbook/src/main.rs35
3 files changed, 60 insertions, 3 deletions
diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs
index 423acea016b..6c0c770bf08 100644
--- a/src/bootstrap/test.rs
+++ b/src/bootstrap/test.rs
@@ -1446,7 +1446,6 @@ test_book!(
     TheBook, "src/doc/book", "book", default=false;
     UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
     EditionGuide, "src/doc/edition-guide", "edition-guide", default=false;
-    RustcGuide, "src/doc/rustc-guide", "rustc-guide", default=false;
 );
 
 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -1532,6 +1531,31 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
 }
 
 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub struct RustcGuide;
+
+impl Step for RustcGuide {
+    type Output = ();
+    const DEFAULT: bool = false;
+    const ONLY_HOSTS: bool = true;
+
+    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
+        run.path("src/doc/rustc-guide")
+    }
+
+    fn make_run(run: RunConfig<'_>) {
+        run.builder.ensure(RustcGuide);
+    }
+
+    fn run(self, builder: &Builder<'_>) {
+        let src = builder.src.join("src/doc/rustc-guide");
+        let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
+        builder.run(rustbook_cmd
+                       .arg("linkcheck")
+                       .arg(&src));
+    }
+}
+
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
 pub struct CrateLibrustc {
     compiler: Compiler,
     target: Interned<String>,
diff --git a/src/tools/rustbook/Cargo.toml b/src/tools/rustbook/Cargo.toml
index 54549e4c7e9..0805dc74f8d 100644
--- a/src/tools/rustbook/Cargo.toml
+++ b/src/tools/rustbook/Cargo.toml
@@ -7,6 +7,8 @@ edition = "2018"
 
 [dependencies]
 clap = "2.25.0"
+mdbook-linkcheck = "0.3.0"
+failure = "0.1"
 
 [dependencies.mdbook]
 version = "0.3.0"
diff --git a/src/tools/rustbook/src/main.rs b/src/tools/rustbook/src/main.rs
index 04e48dde86b..ecba45058e2 100644
--- a/src/tools/rustbook/src/main.rs
+++ b/src/tools/rustbook/src/main.rs
@@ -11,7 +11,13 @@ use mdbook_1::{MDBook as MDBook1};
 use mdbook_1::errors::{Result as Result1};
 
 use mdbook::MDBook;
-use mdbook::errors::Result;
+use mdbook::errors::{Result as Result3};
+use mdbook::renderer::RenderContext;
+
+use mdbook_linkcheck;
+use mdbook_linkcheck::errors::BrokenLinks;
+use failure::Error;
+
 
 fn main() {
     let d_message = "-d, --dest-dir=[dest-dir]
@@ -31,6 +37,9 @@ fn main() {
                         .arg_from_usage(d_message)
                         .arg_from_usage(dir_message)
                         .arg_from_usage(vers_message))
+                    .subcommand(SubCommand::with_name("linkcheck")
+                        .about("Run linkcheck with mdBook 3")
+                        .arg_from_usage(dir_message))
                     .get_matches();
 
     // Check which subcomamnd the user ran...
@@ -64,10 +73,32 @@ fn main() {
                 }
             };
         },
+        ("linkcheck", Some(sub_matches)) => {
+            if let Err(err) = linkcheck(sub_matches) {
+                eprintln!("Error: {}", err);
+
+                if let Ok(broken_links) = err.downcast::<BrokenLinks>() {
+                    for cause in broken_links.links().iter() {
+                        eprintln!("\tCaused By: {}", cause);
+                    }
+                }
+
+                ::std::process::exit(101);
+            }
+        },
         (_, _) => unreachable!(),
     };
 }
 
+pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> {
+    let book_dir = get_book_dir(args);
+    let book = MDBook::load(&book_dir).unwrap();
+    let cfg = book.config;
+    let render_ctx = RenderContext::new(&book_dir, book.book, cfg, &book_dir);
+
+    mdbook_linkcheck::check_links(&render_ctx)
+}
+
 // Build command implementation
 pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
     let book_dir = get_book_dir(args);
@@ -86,7 +117,7 @@ pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
 }
 
 // Build command implementation
-pub fn build(args: &ArgMatches<'_>) -> Result<()> {
+pub fn build(args: &ArgMatches<'_>) -> Result3<()> {
     let book_dir = get_book_dir(args);
     let mut book = MDBook::load(&book_dir)?;