• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# This is a script that can be used in each book's CI to validate links using
4# the same tool as rust-lang/rust.
5#
6# This requires the rust-docs rustup component to be installed in the nightly
7# toolchain.
8#
9# Usage:
10#   ./linkcheck.sh <name-of-book>
11#
12# Options:
13#
14# -i        "Iterative" mode. The script will not clean up after it is done so
15#           you can inspect the result, and re-run more quickly.
16#
17# --all     Check all books. This can help make sure you don't break links
18#           from other books into your book.
19#
20# --path <book-path>
21#           Path to the root directory for the book. Default to the current
22#           working directory if omitted.
23
24set -e
25
26html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html"
27
28if [ ! -d "$html_dir" ]
29then
30    echo "HTML docs are missing from sysroot: $html_dir"
31    echo "Make sure the nightly rust-docs rustup component is installed."
32    exit 1
33fi
34
35# Avoid failure caused by newer mdbook.
36export MDBOOK_OUTPUT__HTML__INPUT_404=""
37
38book_name=""
39# Default to the current directory
40book_path="."
41# Iterative will avoid cleaning up, so you can quickly run it repeatedly.
42iterative=0
43# If "1", test all books, else only this book.
44all_books=0
45
46while [ "$1" != "" ]
47do
48    case "$1" in
49        -i)
50            iterative=1
51            ;;
52        --all)
53            all_books=1
54            ;;
55        --path)
56            book_path="${2:-.}"
57            shift
58            ;;
59        *)
60            if [ -n "$book_name" ]
61            then
62                echo "only one argument allowed"
63                exit 1
64            fi
65            book_name="$1"
66            ;;
67    esac
68    shift
69done
70
71if [ -z "$book_name" ]
72then
73    echo "usage: $0 <name-of-book>"
74    exit 1
75fi
76
77if [ ! -f "$book_path/book.toml" ] && [ ! -f "$book_path/src/SUMMARY.md" ]
78then
79    echo "Run command in root directory of the book or provide a path to the book"
80    exit 1
81fi
82
83if [ ! -d "$html_dir/$book_name" ]
84then
85    echo "book name \"$book_name\" not found in sysroot \"$html_dir\""
86    exit 1
87fi
88
89if [ "$iterative" = "0" ]
90then
91    echo "Cleaning old directories..."
92    rm -rf linkcheck linkchecker
93fi
94
95if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ]
96then
97    echo "Downloading linkchecker source..."
98    nightly_hash=$(rustc +nightly -Vv | grep commit-hash | cut -f2 -d" ")
99    url="https://raw.githubusercontent.com/rust-lang/rust"
100    mkdir linkchecker
101    curl -o linkchecker/Cargo.toml ${url}/${nightly_hash}/src/tools/linkchecker/Cargo.toml
102    curl -o linkchecker/main.rs ${url}/${nightly_hash}/src/tools/linkchecker/main.rs
103fi
104
105echo "Building book \"$book_name\"..."
106mdbook build "$book_path"
107
108cp -R "$html_dir" linkcheck
109rm -rf "linkcheck/$book_name"
110cp -R "$book_path/book" "linkcheck/$book_name"
111
112if [ "$all_books" = "1" ]
113then
114    check_path="linkcheck"
115else
116    check_path="linkcheck/$book_name"
117fi
118echo "Running linkchecker on \"$check_path\"..."
119cargo run --release --manifest-path=linkchecker/Cargo.toml -- "$check_path"
120
121if [ "$iterative" = "0" ]
122then
123    rm -rf linkcheck linkchecker
124fi
125
126echo "Link check completed successfully!"
127