-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·213 lines (185 loc) · 4.53 KB
/
deploy.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env sh
# Configs
registry_name="registry"
registry_path=""
registry_cache_path=""
build_dir="build"
dist_dir="dist"
cache_uri=""
root_dir=$(pwd)
# State flags
is_cached=false
# -------------
is_dry_run=false
is_clean=false
is_parallel=false
is_production=false
is_skip_load_cache=false
help() {
cat << EOF
usage: $arg0 [options]
options:
-h, --help Show this menu.
--dry-run Dry run the commands.
--clean Clean the output directories build and pdfs
before compiling.
--parallel Use parallel to run the compile scripts.
--production Create pdf in distribution directory.
--build-dir Specify build directory,
example: --build-dir=\"build\". Default: build.
--dist-dir Specify dist directory,
example: --dist-dir=\"dist\". Default: dist.
--cache-uri Set cache uri to download.
--skip-load-cache Skip loading cache.
EOF
}
generate_file_index() {
# Add files
printf "" > $registry_path
find ./cs | grep .tex >> $registry_path
find ./dsp | grep .tex >> $registry_path
find ./ee | grep .tex >> $registry_path
find ./maths | grep .tex >> $registry_path
find ./misc | grep .tex >> $registry_path
# Filter out dependencies from registry database
registry_data=$(cat $registry_path | sort)
input_deps=""
for path in $registry_data
do
input_dep=$(egrep '\\input\{.+\}' $path | sed -e 's/\\input{\(.*\)}/\1/g')
if ! [[ -z "$input_dep" ]]; then
if ! [[ -z "$input_deps" ]]; then
input_deps="$input_deps\n$input_dep"
else
input_deps="$input_dep"
fi
fi
done
input_deps=$(printf "$input_deps\n" | sort -u)
for path in $input_deps
do
# FIXME: Can't handle relative path ./
esc_path=$(printf "$path\n" | sed -r 's/\//\\\//')
registry_data=$(printf "$registry_data\n" | sed -r "s/^.*$esc_path//")
done
printf "$registry_data\n" | sort -u | sed -r '/^\s*$/d' > $registry_path
}
parse_args() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--help | -h)
help
exit 0
;;
--dry-run)
is_dry_run=true
;;
--clean)
is_clean=true
;;
--parallel)
is_parallel=true
;;
--production)
is_production=true
;;
--build-dir=*)
build_dir="${key#*=}"
;;
--dist-dir=*)
dist_dir="${key#*=}"
;;
--cache-uri=*)
cache_uri="${key#*=}"
;;
--skip-load-cache)
is_skip_load_cache=true
;;
*)
printf "Unsupported argument: $key\n"
exit 1
;;
esac
shift
done
}
prebuild() {
# clean
if [[ $is_clean = true ]] && [[ -e "$build_dir" ]]; then
rm -rf $build_dir
fi
if [[ $is_clean = true ]] && [[ $is_production = true ]] && [[ -e "$dist_dir/pdf" ]]; then
rm -rf "$dist_dir/pdf"
fi
# create path
if ! [[ -e $build_dir ]]; then
mkdir -p $build_dir
fi
# construct path strings
registry_path="$build_dir/$registry_name.txt"
registry_cache_path="$build_dir/$registry_name.cache.txt"
# check for cache
if [[ $is_skip_load_cache = true ]]; then
return 0
fi
if ! [[ -z $cache_uri ]]; then
cd $build_dir
wget $cache_uri
cd $root_dir
fi
# BUG: Removed LaTeX file won't be updated.
# BUG: On Windows docker, the expanded archive can't be renamed, permission
# denied.
if [[ -e "$build_dir/archive.tar.gz" ]]; then
cd $build_dir
tar -xzf "./archive.tar.gz"
cd $root_dir
fi
}
build() {
if ! [[ -e "$registry_path" ]]; then
printf "No registry file\n"
exit 1
fi
files=$(cat $registry_path)
args="--build-dir=$build_dir"
if [[ $is_dry_run = true ]]; then
for file in $files; do
echo "./compile.sh $args $file"
done
return 0
fi
if [[ $is_parallel = true ]]; then
parallel --will-cite sh ./compile.sh $args ::: $files
else
for file in $files; do
sh ./compile.sh $args $file
done
fi
}
postbuild() {
cd $build_dir
tar -czf archive.tar.gz pdf
cd $root_dir
if [[ $is_production = false ]]; then
return
fi
if ! [[ -e "$dist_dir" ]]; then
mkdir -p "$dist_dir"
fi
if [[ -e "$dist_dir/pdf" ]]; then
rm -rf "$dist_dir/pdf"
fi
if [[ -e "web/dist" ]]; then
cp -rf web/dist/* dist
fi
cp -rf "$build_dir/pdf" "$dist_dir"
cp "$registry_path" "$dist_dir/pdf"
cp "$build_dir/archive.tar.gz" "$dist_dir/pdf"
}
parse_args $@
prebuild
generate_file_index
build
postbuild