-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.sh
executable file
·272 lines (234 loc) · 6.02 KB
/
compile.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env sh
# Configs
script_name=$0
build_dir=build
input_file=""
# Arg flags
is_skip_bitex=false
is_dry_run=false
is_no_cache=false
is_no_spin=false
# State flags
is_cached=false
has_input=false
is_clean=false
# Path variables
filepath=""
filename=""
name=""
ext=""
rel_location=""
latex_build_dir=""
pdf_dir=""
pdf_latex_dir=""
abs_latex_build_dir=""
abs_pdf_dir=""
file_deps=""
help() {
cat << EOF
usage: $arg0 [options] <file.tex>
options:
-h, --help Show this menu.
--dry-run Dry run the commands.
--build-dir=<directory> Specify build directory.
--skip-bibtex Skip generating bibtex.
--no-cache This action skips the cache check.
--clean Clean before build. This will remove
output pdf and the build intermediate steps.
EOF
}
parse_args() {
if [[ $# = 0 ]]; then
printf "error: no input file\n"
help
exit 1
fi
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--help | -h)
help
exit 0
;;
--dry-run)
is_dry_run=true
;;
--build-dir=*)
build_dir="${key#*=}"
;;
--skip-bibtex)
is_skip_bitex=true
;;
--no-cache)
is_no_cache=true
;;
--no-spin)
is_no_spin=true
;;
--clean)
is_clean=true
;;
*)
if [[ $has_input = true ]]; then
printf "Only one input file is allowed!\n"
exit 1
fi
# Assume any other argument is a filename
input_file=$key
has_input=true
;;
esac
shift
done
}
recursive_index() {
# Check if file exist before we analyze it
if ! [[ -e "$1" ]]; then
return 0
fi
_grep_regex='((input)|(includegraphics\[.*\])|(addbibresource))\{.+\}'
_replace_regex='s/\\((input)|(includegraphics\[.*\])|(addbibresource))\{(.+)\}/\5/'
if ! [ -d "$1" ]; then
_deps=$(cat "$1" | grep -E "$_grep_regex" | sed -r "$_replace_regex")
else
_deps=""
fi
if ! [[ -z "$_deps" ]]; then
if ! [[ -z "$file_deps" ]]; then
file_deps="$file_deps\n$_deps"
else
file_deps="$_deps"
fi
for file in "$_deps"; do
filepath="$(dirname $1)/$file"
recursive_index $filepath
done
fi
}
prebuild() {
if ! [[ -e $input_file ]]; then
printf "File $input_file does not exist!\n"
exit 1
fi
filepath="$(realpath $input_file)"
filename="$(basename "$input_file")"
name="${filename%.*}"
ext="${filename##*.}"
rel_location="$(dirname $input_file)"
latex_build_dir="$build_dir/tex/$rel_location"
pdf_dir="$build_dir/pdf/$rel_location"
if ! [[ -e $build_dir ]]; then
mkdir -p $build_dir
fi
if ! [[ -e $latex_build_dir ]]; then
mkdir -p $latex_build_dir
elif [[ -e $latex_build_dir ]] && [[ $is_clean = true ]]; then
rm -rf $latex_build_dir
mkdir -p $latex_build_dir
fi
if ! [[ -e $pdf_dir ]]; then
mkdir -p $pdf_dir
elif [[ -e $pdf_dir ]] && [[ $is_clean = true ]]; then
rm -rf $pdf_dir
mkdir -p $pdf_dir
fi
# Convert output path to absolute
abs_latex_build_dir=$(realpath "$build_dir/tex/$rel_location")
abs_pdf_dir=$(realpath "$build_dir/pdf/$rel_location")
pdf_latex_dir="$abs_pdf_dir/$name"
if ! [[ -e "$pdf_latex_dir" ]]; then
mkdir -p "$pdf_latex_dir"
elif [[ -e $pdf_latex_dir ]] && [[ $is_clean = true ]]; then
rm -rf $pdf_latex_dir
mkdir -p "$pdf_latex_dir"
fi
# Generate local registry for dependencies
recursive_index $filepath
file_deps=$(echo "$file_deps" | sed -r "s/[ %]*//" | sort -u)
}
parse_info() {
if [ $1 -ne 0 ]; then
printf "\r$2: failed, check $latex_build_dir/$2.log\n"
cat $abs_latex_build_dir/$2.log | awk 'BEGIN{IGNORECASE = 1}/warning|!|Runaway argument\?/,/^$/;'
exit 1
fi
}
exec_build_cmd() {
exec_name=$1
cmd=$2
log_file="$abs_latex_build_dir/$exec_name.log"
$cmd > $log_file
exit_code=$?
parse_info $exit_code "$exec_name"
}
check_cache() {
is_cached=false
if ! [[ -e "$abs_pdf_dir/$name.pdf" ]] || ! [[ -e "$pdf_latex_dir/$filename" ]]; then
return 0
fi
input_check_sum=$(sha256sum $input_file | cut -d ' ' -f 1)
output_check_sum=$(sha256sum "$pdf_latex_dir/$filename" | cut -d ' ' -f 1)
if ! [[ $input_check_sum = $output_check_sum ]]; then
return 0
fi
for file in $file_deps; do
if [[ -e $rel_location/$file ]] && [[ -e $pdf_latex_dir/$file ]]; then
asum=$(sha256sum $rel_location/$file | cut -d ' ' -f 1)
bsum=$(sha256sum $pdf_latex_dir/$file | cut -d ' ' -f 1)
if ! [[ $asum = $bsum ]]; then
return 0
fi
else
# Special case for inline bibtex addbibresource{}
if ! [[ "${file##*.}" = "bib" ]]; then
return 0
fi
fi
done
is_cached=true
}
build() {
if [[ $is_cached = true ]] && [[ $is_no_cache = false ]]; then
printf "$input_file is cached.\n"
return 0
fi
printf "compiling $input_file..."
current_dir=$(pwd)
cd $rel_location
exec_build_cmd "stage1" "pdflatex -halt-on-error --interaction=nonstopmode -output-directory=$abs_latex_build_dir $filename"
exec_build_cmd "stage2" "biber $abs_latex_build_dir/$name"
exec_build_cmd "stage3" "pdflatex -halt-on-error --interaction=nonstopmode -output-directory=$abs_latex_build_dir $filename"
cd $current_dir
printf "done!\n"
}
postbuild() {
if [[ $is_cached = true ]] && [[ $is_no_cache = false ]]; then
return 0
fi
if ! [[ -e $abs_pdf_dir ]]; then
printf "PDF output path does not exist!\n"
return 1
fi
if [[ -e "$abs_latex_build_dir/$name.pdf" ]]; then
cp "$abs_latex_build_dir/$name.pdf" $abs_pdf_dir
fi
# Copy LaTeX and dependency files
cp $input_file $pdf_latex_dir
for file in $file_deps; do
file_dir=$(dirname $file)
if ! [[ $file_dir = '.' ]]; then
mkdir -p "$pdf_latex_dir/$file_dir"
else
file_dir=""
fi
if [[ -e "$rel_location/$file" ]]; then
cp "$rel_location/$file" "$pdf_latex_dir/$file_dir"
fi
file_dir=""
done
}
parse_args $@
prebuild
check_cache
build
postbuild