Skip to content

Commit 97e7058

Browse files
authored
docs: add script to download mdbook (#8351)
We add a bash script to download and install mdbook. For linux or mac x86_64, we download prebuilts from github. Otherwise, we use `cargo install` to build it with rust.
1 parent ac0aea9 commit 97e7058

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

docs_src/build/install_mdbook.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/bash
2+
3+
# Copyright (C) 2025 The Android Open Source Project
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
DARWIN_X86=1
18+
LINUX_X86=2
19+
DARWIN_ARM=3
20+
LINUX_ARM=4
21+
22+
function add_cargo_path() {
23+
local BASH_P="${HOME}/.bashrc"
24+
local PATH_LINE='export PATH=${PATH}:~/.cargo/bin'
25+
local TYPE=$1
26+
if [ ${TYPE} == ${DARWIN_ARM} ] || [ ${TYPE} == ${DARWIN_X86} ]; then
27+
BASH_P="${HOME}/.bash_profile"
28+
fi
29+
if ! (grep "${PATH_LINE}" ${BASH_P}); then
30+
echo "${PATH_LINE}" >> ${BASH_P}
31+
fi
32+
source ${BASH_P}
33+
}
34+
35+
function download_mdbook() {
36+
if command -v mdbook >/dev/null 2>&1; then
37+
echo "mdbook already installed"
38+
exit 0
39+
fi
40+
41+
local CHECK_UNAME="
42+
import sys;
43+
parts=[a.lower() for a in sys.stdin.read().strip().split(' ')];
44+
def get_type():
45+
if 'darwin' in parts:
46+
if 'x86_64' in parts:
47+
return ${DARWIN_X86}
48+
elif 'aarch' in parts:
49+
return ${DARWIN_ARM}
50+
elif 'linux' in parts:
51+
if 'x86_64' in parts:
52+
return ${LINUX_X86}
53+
elif 'aarch' in parts:
54+
return ${LINUX_ARM}
55+
return 0
56+
print(get_type())
57+
"
58+
local TYPE=`uname -a | python3 -c "${CHECK_UNAME}"`
59+
if [ ${TYPE} == ${DARWIN_ARM} ] || [ ${TYPE} == ${LINUX_ARM} ]; then
60+
# No github prebuilts are available, we build it with rust from source.
61+
# First, need to install rust and cargo
62+
if ! (command -v rustc >/dev/null 2>&1) || ! (command -v cargo >/dev/null 2>&1); then
63+
echo "*** Need to install Rust ***"
64+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
65+
fi
66+
if ! (command -v cargo >/dev/null 2>&1); then
67+
echo "*** Still cannot find `cargo` ***"
68+
exit 1
69+
fi
70+
cargo install mdbook
71+
else
72+
# Download prebuilts from github
73+
mkdir -p ${HOME}/.cargo/bin
74+
echo "*** Downloading mdbook from github release ***"
75+
DL_URL='https://github.com/rust-lang/mdBook/releases/download/v0.4.43/mdbook-v0.4.43-x86_64-apple-darwin.tar.gz '
76+
if [ ${TYPE} == ${LINUX_X86} ]; then
77+
DL_URL='https://github.com/rust-lang/mdBook/releases/download/v0.4.43/mdbook-v0.4.43-x86_64-unknown-linux-gnu.tar.gz'
78+
fi
79+
curl -L -o ~/Downloads/mdbook.tar.gz ${DL_URL}
80+
mkdir -p /tmp/mdbook
81+
tar -xvzf ~/Downloads/mdbook.tar.gz -C /tmp/mdbook >/dev/null 2>&1
82+
mv /tmp/mdbook/mdbook ~/.cargo/bin/
83+
fi
84+
add_cargo_path ${TYPE}
85+
}
86+
87+
download_mdbook

0 commit comments

Comments
 (0)