]> git.proxmox.com Git - rustc.git/blame - src/tools/linkchecker/linkcheck.sh
New upstream version 1.54.0+dfsg1
[rustc.git] / src / tools / linkchecker / linkcheck.sh
CommitLineData
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
20set -e
21
22if [ ! -f book.toml ] && [ ! -f src/SUMMARY.md ]
23then
24 echo "Run command in root directory of the book."
25 exit 1
26fi
27
28html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html"
29
30if [ ! -d "$html_dir" ]
31then
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
35fi
36
29967ef6
XL
37# Avoid failure caused by newer mdbook.
38export MDBOOK_OUTPUT__HTML__INPUT_404=""
39
74b04a01
XL
40book_name=""
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 *)
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
65done
66
67if [ -z "$book_name" ]
68then
69 echo "usage: $0 <name-of-book>"
70 exit 1
71fi
72
73if [ ! -d "$html_dir/$book_name" ]
74then
75 echo "book name \"$book_name\" not found in sysroot \"$html_dir\""
76 exit 1
77fi
78
79if [ "$iterative" = "0" ]
80then
81 echo "Cleaning old directories..."
82 rm -rf linkcheck linkchecker
83fi
84
85if [ ! -e "linkchecker/main.rs" ] || [ "$iterative" = "0" ]
86then
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
93fi
94
95echo "Building book \"$book_name\"..."
96mdbook build
97
98cp -R "$html_dir" linkcheck
99rm -rf "linkcheck/$book_name"
100cp -R book "linkcheck/$book_name"
101
102if [ "$all_books" = "1" ]
103then
104 check_path="linkcheck"
105else
106 check_path="linkcheck/$book_name"
107fi
108echo "Running linkchecker on \"$check_path\"..."
17df50a5 109cargo run --release --manifest-path=linkchecker/Cargo.toml -- "$check_path"
74b04a01
XL
110
111if [ "$iterative" = "0" ]
112then
113 rm -rf linkcheck linkchecker
114fi
115
116echo "Link check completed successfully!"