]> git.lizzy.rs Git - rust.git/blob - src/tools/linkchecker/linkcheck.sh
Merge branch 'master' into relnotes-1.42.0
[rust.git] / src / tools / linkchecker / linkcheck.sh
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 set -e
21
22 if [ ! -f book.toml ] && [ ! -f src/SUMMARY.md ]
23 then
24     echo "Run command in root directory of the book."
25     exit 1
26 fi
27
28 html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html"
29
30 if [ ! -d "$html_dir" ]
31 then
32     echo "HTML docs are missing from sysroot: $html_dir"
33     echo "Make sure the nightly rust-docs rustup component is installed."
34     exit 1
35 fi
36
37 book_name=""
38 # Iterative will avoid cleaning up, so you can quickly run it repeatedly.
39 iterative=0
40 # If "1", test all books, else only this book.
41 all_books=0
42
43 while [ "$1" != "" ]
44 do
45     case "$1" in
46         -i)
47             iterative=1
48             ;;
49         --all)
50             all_books=1
51             ;;
52         *)
53             if [ -n "$book_name" ]
54             then
55                 echo "only one argument allowed"
56                 exit 1
57             fi
58             book_name="$1"
59             ;;
60     esac
61     shift
62 done
63
64 if [ -z "$book_name" ]
65 then
66     echo "usage: $0 <name-of-book>"
67     exit 1
68 fi
69
70 if [ ! -d "$html_dir/$book_name" ]
71 then
72     echo "book name \"$book_name\" not found in sysroot \"$html_dir\""
73     exit 1
74 fi
75
76 if [ "$iterative" = "0" ]
77 then
78     echo "Cleaning old directories..."
79     rm -rf linkcheck linkchecker
80 fi
81
82 if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ]
83 then
84     echo "Downloading linkchecker source..."
85     mkdir linkchecker
86     curl -o linkchecker/Cargo.toml \
87         https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/Cargo.toml
88     curl -o linkchecker/main.rs \
89         https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/main.rs
90 fi
91
92 echo "Building book \"$book_name\"..."
93 mdbook build
94
95 cp -R "$html_dir" linkcheck
96 rm -rf "linkcheck/$book_name"
97 cp -R book "linkcheck/$book_name"
98
99 if [ "$all_books" = "1" ]
100 then
101     check_path="linkcheck"
102 else
103     check_path="linkcheck/$book_name"
104 fi
105 echo "Running linkchecker on \"$check_path\"..."
106 cargo run --manifest-path=linkchecker/Cargo.toml -- "$check_path"
107
108 if [ "$iterative" = "0" ]
109 then
110     rm -rf linkcheck linkchecker
111 fi
112
113 echo "Link check completed successfully!"