-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·150 lines (133 loc) · 4.87 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env sh
# Check if the command 'mdbook' is available in the system
if test -z "$(command -v mdbook 2> /dev/null)"; then
# If 'mdbook' is not available, prompt the user to install it
printf "mdbook is required to build, do you want to install it? (y/n) "
read -r response
# Handle the user's response
case $response in
# If the user responds with 'y' or 'Y', install 'mdbook'
[Yy]*)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install mdbook
;;
# If the user responds with 'n' or 'N', exit the script
[Nn]*)
exit
;;
# If the user responds with anything else, display an error message and exit
*)
echo "Invalid response"
exit 1
;;
esac
fi
# Check if the './deploy' directory exists
if [ -d "./deploy" ]; then
# If it does, delete it
rm -rf ./deploy
fi
# Create a new './deploy' directory
mkdir ./deploy
# Go to the 'src/site' directory
cd src/site || exit 2
# Delete the 'guide' and 'theme' directories and recreate them
rm -rf guide theme
mkdir guide theme
# Copy the contents of '../theme/' to './theme/'
cp -r ../theme/* ./theme/
# Rename 'site-index.hbs' to 'index.hbs'
mv ./theme/site-index.hbs ./theme/index.hbs
# Delete 'guide-index.hbs'
rm ./theme/guide-index.hbs
# Build the 'mdbook'
mdbook build
# Go back to the parent directory
cd ../..
# Copy the contents of 'src/site/guide/' to './deploy'
cp -r src/site/guide/* ./deploy
# Copy the '.nojekyll' file to './deploy'
cp src/site/guide/.nojekyll ./deploy
# Set the list of directories to delete
folders="css FontAwesome fonts theme"
# Set the list of files to delete
files="404.html ayu-highlight.css book.js clipboard.min.js elasticlunr.min.js favicon.png favicon.svg highlight.css highlight.js mark.min.js print.html searcher.js tomorrow-night.css .nojekyll"
# Loop through all the directories in './src/guides/'
for guide in ./src/guides/*/
do
# Set the path of the current guide
guide_path=${guide%*/}
# Set the name of the current guide
guide_name=${guide_path##*/}
# Go to the current guide's directory
cd "$guide_path" || exit 2
# Delete the 'guide' and 'theme' directories and recreate them
rm -rf guide theme
mkdir guide theme
# Copy the contents of '../../theme/' to './theme/'
cp -r ../../theme/* ./theme/
# Rename 'guide-index.hbs' to 'index.hbs'
mv ./theme/guide-index.hbs ./theme/index.hbs
# Delete 'site-index.hbs'
rm ./theme/site-index.hbs
# Build the 'mdbook'
mdbook build
# Go to the 'guide' directory
cd guide || exit 2
# Loop through the list of directories to delete
for folder in $folders; do
# Delete the current directory
rm -rf "$folder"
done
# Loop through the list of files to delete
for file in $files; do
# Delete the current file
rm -rf "$file"
done
# Go back to the parent directory
cd ../../../..
# Create a new directory in './deploy' with the name of the current guide
mkdir deploy/"$guide_name"
# Copy the contents of the current guide's './guide/' directory to './deploy/[current guide name]'
cp -r "$guide_path"/guide/* ./deploy/"$guide_name"/
# Go to the './deploy/[current guide name]' directory
cd deploy/$guide_name/ || exit 2
# Loop through all the '.html' files in the current directory and its subdirectories
for file in `find -name '*.html'`
do
# Set the initial depth to 0
depth=0
# Set the initial index to 0
i=0
# While the index is less than the length of the file name
while [ $i -lt `expr length $file` ]; do
# Set the current character to the character at the current index
char=`expr substr "$file" $((i+1)) 1`
# If the current character is a '/', increase the depth by 1
if [ "$char" = '/' ]; then
depth=$((depth+1))
fi
# Increase the index by 1
i=$((i+1))
done
# Set the relative path to the root to an empty string
relative_to_root=""
# Set the index to 0
i=0
# While the index is less than the depth
while [ $i -lt $depth ]; do
# Append '../' to the relative path to the root
relative_to_root="$relative_to_root../"
# Increase the index by 1
i=$((i+1))
done
# Remove the trailing '/' from the relative path to the root
relative_to_root=$(echo "$relative_to_root" | awk '{sub(/\/$/,"",$1); print $1}')
# Replace all instances of '{mane}' in the current file with the relative path to the root
awk '{gsub(/\{mane\}/,"'"$relative_to_root"'")} 1' "$file" > "$file".tmp
# Rename the modified file to the original name
mv "$file".tmp "$file"
done
# Go back to the parent directory
cd ../..
done