]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
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 | ||
29967ef6 XL |
37 | # Avoid failure caused by newer mdbook. |
38 | export MDBOOK_OUTPUT__HTML__INPUT_404="" | |
39 | ||
74b04a01 XL |
40 | book_name="" |
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 | *) | |
56 | if [ -n "$book_name" ] | |
57 | then | |
58 | echo "only one argument allowed" | |
59 | exit 1 | |
60 | fi | |
61 | book_name="$1" | |
62 | ;; | |
63 | esac | |
64 | shift | |
65 | done | |
66 | ||
67 | if [ -z "$book_name" ] | |
68 | then | |
69 | echo "usage: $0 <name-of-book>" | |
70 | exit 1 | |
71 | fi | |
72 | ||
73 | if [ ! -d "$html_dir/$book_name" ] | |
74 | then | |
75 | echo "book name \"$book_name\" not found in sysroot \"$html_dir\"" | |
76 | exit 1 | |
77 | fi | |
78 | ||
79 | if [ "$iterative" = "0" ] | |
80 | then | |
81 | echo "Cleaning old directories..." | |
82 | rm -rf linkcheck linkchecker | |
83 | fi | |
84 | ||
85 | if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ] | |
86 | then | |
87 | echo "Downloading linkchecker source..." | |
17df50a5 XL |
88 | nightly_hash=$(rustc +nightly -Vv | grep commit-hash | cut -f2 -d" ") |
89 | url="https://raw.githubusercontent.com/rust-lang/rust" | |
74b04a01 | 90 | mkdir linkchecker |
17df50a5 XL |
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 | |
74b04a01 XL |
93 | fi |
94 | ||
95 | echo "Building book \"$book_name\"..." | |
96 | mdbook build | |
97 | ||
98 | cp -R "$html_dir" linkcheck | |
99 | rm -rf "linkcheck/$book_name" | |
100 | cp -R book "linkcheck/$book_name" | |
101 | ||
102 | if [ "$all_books" = "1" ] | |
103 | then | |
104 | check_path="linkcheck" | |
105 | else | |
106 | check_path="linkcheck/$book_name" | |
107 | fi | |
108 | echo "Running linkchecker on \"$check_path\"..." | |
17df50a5 | 109 | cargo run --release --manifest-path=linkchecker/Cargo.toml -- "$check_path" |
74b04a01 XL |
110 | |
111 | if [ "$iterative" = "0" ] | |
112 | then | |
113 | rm -rf linkcheck linkchecker | |
114 | fi | |
115 | ||
116 | echo "Link check completed successfully!" |