|
| 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 | + |
| 24 | +set -e |
| 25 | + |
| 26 | +html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html" |
| 27 | + |
| 28 | +if [ ! -d "$html_dir" ] |
| 29 | +then |
| 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 |
| 33 | +fi |
| 34 | + |
| 35 | +# Avoid failure caused by newer mdbook. |
| 36 | +export MDBOOK_OUTPUT__HTML__INPUT_404="" |
| 37 | + |
| 38 | +book_name="" |
| 39 | +# Default to the current directory |
| 40 | +book_path="." |
| 41 | +# Iterative will avoid cleaning up, so you can quickly run it repeatedly. |
| 42 | +iterative=0 |
| 43 | +# If "1", test all books, else only this book. |
| 44 | +all_books=0 |
| 45 | + |
| 46 | +while [ "$1" != "" ] |
| 47 | +do |
| 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 |
| 69 | +done |
| 70 | + |
| 71 | +if [ -z "$book_name" ] |
| 72 | +then |
| 73 | + echo "usage: $0 <name-of-book>" |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | + |
| 77 | +if [ ! -f "$book_path/book.toml" ] && [ ! -f "$book_path/src/SUMMARY.md" ] |
| 78 | +then |
| 79 | + echo "Run command in root directory of the book or provide a path to the book" |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +if [ ! -d "$html_dir/$book_name" ] |
| 84 | +then |
| 85 | + echo "book name \"$book_name\" not found in sysroot \"$html_dir\"" |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | +if [ "$iterative" = "0" ] |
| 90 | +then |
| 91 | + echo "Cleaning old directories..." |
| 92 | + rm -rf linkcheck linkchecker |
| 93 | +fi |
| 94 | + |
| 95 | +if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ] |
| 96 | +then |
| 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.lock ${url}/${nightly_hash}/Cargo.lock |
| 102 | + curl -o linkchecker/Cargo.toml ${url}/${nightly_hash}/src/tools/linkchecker/Cargo.toml |
| 103 | + curl -o linkchecker/main.rs ${url}/${nightly_hash}/src/tools/linkchecker/main.rs |
| 104 | +fi |
| 105 | + |
| 106 | +echo "Building book \"$book_name\"..." |
| 107 | +mdbook build "$book_path" |
| 108 | + |
| 109 | +cp -R "$html_dir" linkcheck |
| 110 | +rm -rf "linkcheck/$book_name" |
| 111 | +cp -R "$book_path/book" "linkcheck/$book_name" |
| 112 | + |
| 113 | +if [ "$all_books" = "1" ] |
| 114 | +then |
| 115 | + check_path="linkcheck" |
| 116 | +else |
| 117 | + check_path="linkcheck/$book_name" |
| 118 | +fi |
| 119 | +echo "Running linkchecker on \"$check_path\"..." |
| 120 | +cargo run --release --manifest-path=linkchecker/Cargo.toml -- "$check_path" |
| 121 | + |
| 122 | +if [ "$iterative" = "0" ] |
| 123 | +then |
| 124 | + rm -rf linkcheck linkchecker |
| 125 | +fi |
| 126 | + |
| 127 | +echo "Link check completed successfully!" |
0 commit comments