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.
6 # This requires the rust-docs rustup component to be installed in the nightly
10 # ./linkcheck.sh <name-of-book>
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.
17 # --all Check all books. This can help make sure you don't break links
18 # from other books into your book.
22 if [ ! -f book.toml ] && [ ! -f src/SUMMARY.md ]
24 echo "Run command in root directory of the book."
28 html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html"
30 if [ ! -d "$html_dir" ]
32 echo "HTML docs are missing from sysroot: $html_dir"
33 echo "Make sure the nightly rust-docs rustup component is installed."
37 # Avoid failure caused by newer mdbook.
38 export MDBOOK_OUTPUT__HTML__INPUT_404=""
41 # Iterative will avoid cleaning up, so you can quickly run it repeatedly.
43 # If "1", test all books, else only this book.
56 if [ -n "$book_name" ]
58 echo "only one argument allowed"
67 if [ -z "$book_name" ]
69 echo "usage: $0 <name-of-book>"
73 if [ ! -d "$html_dir/$book_name" ]
75 echo "book name \"$book_name\" not found in sysroot \"$html_dir\""
79 if [ "$iterative" = "0" ]
81 echo "Cleaning old directories..."
82 rm -rf linkcheck linkchecker
85 if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ]
87 echo "Downloading linkchecker source..."
88 nightly_hash=$(rustc +nightly -Vv | grep commit-hash | cut -f2 -d" ")
89 url="https://raw.githubusercontent.com/rust-lang/rust"
91 curl -o linkchecker/Cargo.toml ${url}/${nightly_hash}/src/tools/linkchecker/Cargo.toml
92 curl -o linkchecker/main.rs ${url}/${nightly_hash}/src/tools/linkchecker/main.rs
95 echo "Building book \"$book_name\"..."
98 cp -R "$html_dir" linkcheck
99 rm -rf "linkcheck/$book_name"
100 cp -R book "linkcheck/$book_name"
102 if [ "$all_books" = "1" ]
104 check_path="linkcheck"
106 check_path="linkcheck/$book_name"
108 echo "Running linkchecker on \"$check_path\"..."
109 cargo run --release --manifest-path=linkchecker/Cargo.toml -- "$check_path"
111 if [ "$iterative" = "0" ]
113 rm -rf linkcheck linkchecker
116 echo "Link check completed successfully!"