Skip to content

Commit 9190205

Browse files
authored
Always create a tmp directory and copy everything into it (#158)
* Always create a tmp directory and copy everything into it This change prepares for a future change that uses git history to render diffs. To avoid messing up the bind-mounted current directory (docker_run), copy everything in the current directory to the build directory, then copy what we need back. This change means that notmp is no longer supported. * clarify some comments
1 parent 20c2548 commit 9190205

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

build.sh

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env bash
22

3-
IS_TMP="yes" # default to no tmp directory
43
RESOURCE_DIR="/" #default to root of pandoc container buildout
54
DO_GITVERSION="yes"
65
DO_GITSTATUS="yes"
@@ -20,8 +19,8 @@ dbus-daemon --system || echo "Failed to start dbus daemon"
2019

2120
# Setup an EXIT handler
2221
on_exit() {
23-
if [[ "${IS_TMP}" == "yes" && -e "${build_dir}" ]]; then
24-
rm -rf "${build_dir}"
22+
if [[ -e "${BUILD_DIR}" ]]; then
23+
rm -rf "${BUILD_DIR}"
2524
fi
2625
}
2726

@@ -36,7 +35,7 @@ print_usage() {
3635
echo
3736
echo "Options:"
3837
echo
39-
echo "Output Control"
38+
echo "Output Control (note that output file names are always relative to the current directory)"
4039
echo " --docx=output: enable output of docx and specify the output file name."
4140
echo " --pdf=output: enable output of pdf and specify the output file name."
4241
echo " --latex=output: enable output of latex and specify the output file name."
@@ -46,7 +45,6 @@ print_usage() {
4645
echo
4746
echo "Miscellaneous"
4847
echo " --resourcedir=dir: Set the resource directory, defaults to root for pandoc containers"
49-
echo " --notmp: Do not use a tempory directory for processing steps, instead create a directory called \"build\" in CWD"
5048
echo " --gitversion: legacy flag, no effect (default starting with 0.9.0)"
5149
echo " --gitstatus: legacy flag, no effect (default starting with 0.9.0)"
5250
echo " --nogitversion: Do not use git to describe the generate document version and revision metadata."
@@ -59,7 +57,7 @@ print_usage() {
5957
}
6058

6159

62-
if ! options=$(getopt --longoptions=help,puppeteer,notmp,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diff:,pdf:,latex:,pdflog:,pdf_engine:,docx:,html:,resourcedir: --options="" -- "$@"); then
60+
if ! options=$(getopt --longoptions=help,puppeteer,gitversion,gitstatus,nogitversion,table_rules,plain_quotes,versioned_filenames,pr_number:,pr_repo:,diff:,pdf:,latex:,pdflog:,pdf_engine:,docx:,html:,resourcedir: --options="" -- "$@"); then
6361
echo "Incorrect options provided"
6462
print_usage
6563
exit 1
@@ -97,10 +95,6 @@ while true; do
9795
# legacy option; just ignore this
9896
shift
9997
;;
100-
--notmp)
101-
IS_TMP="no"
102-
shift
103-
;;
10498
--docx)
10599
DOCX_OUTPUT="${2}"
106100
shift 2
@@ -154,7 +148,6 @@ while true; do
154148
done
155149

156150
# Mark globals set from the command line as readonly when we're done updating them.
157-
readonly IS_TMP
158151
readonly RESOURCE_DIR
159152
readonly DO_GITVERSION
160153
readonly DO_GITSTATUS
@@ -193,13 +186,15 @@ if [ "${PDF_ENGINE}" != "xelatex" -a "${PDF_ENGINE}" != "lualatex" ]; then
193186
exit 1
194187
fi
195188

196-
# Set up the output directory, either tmp or build in pwd.
197-
if [ "${IS_TMP}" == "yes" ]; then
198-
build_dir="/tmp/tcg.pandoc"
199-
else
200-
build_dir="$(pwd)/build"
201-
fi
202-
mkdir -p "${build_dir}"
189+
# Set up the build directory.
190+
readonly BUILD_DIR="/tmp/tcg.pandoc"
191+
readonly SOURCE_DIR=$(pwd)
192+
mkdir -p "${BUILD_DIR}"
193+
# Copy everything into the build directory, then cd to that directory.
194+
# This will allow us to manipulate the Git state without side effects
195+
# to callers of docker_run.
196+
cp -r . "${BUILD_DIR}"
197+
cd "${BUILD_DIR}"
203198

204199
# Get the default browser
205200
if ! browser=$(command -v "chromium-browser"); then
@@ -369,9 +364,8 @@ echo "docx: ${DOCX_OUTPUT:-none}"
369364
echo "pdf: ${PDF_OUTPUT:-none} (engine: ${PDF_ENGINE})"
370365
echo "latex: ${latex_ouput:-none}"
371366
echo "html: ${html_ouput:-none}"
372-
echo "use tmp: ${IS_TMP}"
373367
echo "resource dir: ${RESOURCE_DIR}"
374-
echo "build dir: ${build_dir}"
368+
echo "build dir: ${BUILD_DIR}"
375369
echo "browser: ${browser}"
376370
echo "use git version: ${DO_GITVERSION}"
377371
echo "use table rules: ${TABLE_RULES}"
@@ -422,21 +416,18 @@ if [ "${BLOCK_QUOTES_ARE_INFORMATIVE_TEXT}" == "yes" ]; then
422416
extra_pandoc_options+=" --lua-filter=informative-quote-blocks.lua"
423417
fi
424418

425-
mkdir -p "${build_dir}/$(dirname ${input_file})"
426-
cp "${input_file}" "${build_dir}/${input_file}"
427-
428419
# Hacks
429420

430421
# \newpage is rendered as the string "\newpage" in GitHub markdown.
431422
# Transform horizontal rules into \newpages.
432423
# Exception: the YAML front matter of the document, so undo the instance on the first line.
433424
# TODO: Turn this into a Pandoc filter.
434-
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${build_dir}/${input_file}"
425+
sed -i.bak 's/^---$/\\newpage/g;1s/\\newpage/---/g' "${BUILD_DIR}/${input_file}"
435426

436427
# Transform sections before the table of contents into section*, which does not number them.
437428
# While we're doing this, transform the case to all-caps.
438429
# TODO: Turn this into a Pandoc filter.
439-
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${build_dir}/${input_file}"
430+
sed -i.bak '0,/\\tableofcontents/s/^# \(.*\)/\\section*\{\U\1\}/g' "${BUILD_DIR}/${input_file}"
440431

441432
if test "${DO_GITVERSION}" == "yes"; then
442433
# If using the git information for versioning, grab the date from there
@@ -493,10 +484,10 @@ retry () {
493484
return 1
494485
}
495486

496-
readonly TEMP_TEX_FILE="${build_dir}/${input_file}.tex"
487+
readonly TEMP_TEX_FILE="${BUILD_DIR}/${input_file}.tex"
497488
# LaTeX engines choose this filename based on TEMP_TEX_FILE's basename. It also emits a bunch of other files.
498489
readonly TEMP_PDF_FILE="$(basename ${input_file}).pdf"
499-
readonly LATEX_LOG="${build_dir}/latex.log"
490+
readonly LATEX_LOG="${BUILD_DIR}/latex.log"
500491

501492
analyze_latex_logs() {
502493
local logfile=$1
@@ -520,10 +511,10 @@ analyze_latex_logs() {
520511
# That way, LaTeX errors on PDF output point to lines that match the .tex.
521512
if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
522513
if [ -n "${PDF_OUTPUT}" ]; then
523-
mkdir -p "$(dirname ${PDF_OUTPUT})"
514+
mkdir -p "${SOURCE_DIR}/$(dirname ${PDF_OUTPUT})"
524515
fi
525516
if [ -n "${LATEX_OUTPUT}" ]; then
526-
mkdir -p "$(dirname ${LATEX_OUTPUT})"
517+
mkdir -p "${SOURCE_DIR}/$(dirname ${LATEX_OUTPUT})"
527518
fi
528519
echo "Generating LaTeX Output"
529520
start=$(date +%s)
@@ -561,7 +552,7 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
561552
${extra_pandoc_options}
562553
--to=latex
563554
--output="'${TEMP_TEX_FILE}'"
564-
"'${build_dir}/${input_file}'")
555+
"'${BUILD_DIR}/${input_file}'")
565556
retry 5 "${cmd[@]}"
566557
if [ $? -ne 0 ]; then
567558
FAILED=true
@@ -571,14 +562,14 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
571562
echo "Elapsed time: $(($end-$start)) seconds"
572563

573564
if [ -n "${LATEX_OUTPUT}" ]; then
574-
cp "${TEMP_TEX_FILE}" "${LATEX_OUTPUT}"
565+
cp "${TEMP_TEX_FILE}" "${SOURCE_DIR}/${LATEX_OUTPUT}"
575566
fi
576567

577568
if [ -n "${PDF_OUTPUT}" ]; then
578569
echo "Rendering PDF"
579570
start=$(date +%s)
580-
# Run twice to populate aux, lof, lot, toc, then update the page numbers due
581-
# to the impact of populating the lof, lot, toc.
571+
# latexmk takes care of repeatedly calling the PDF engine. A run may take multiple passes due to the need to
572+
# update .toc and other files.
582573
latexmk "${TEMP_TEX_FILE}" -pdflatex="${PDF_ENGINE}" -pdf -diagnostics > "${LATEX_LOG}"
583574
if [ $? -ne 0 ]; then
584575
FAILED=true
@@ -588,20 +579,23 @@ if [ -n "${PDF_OUTPUT}" -o -n "${LATEX_OUTPUT}" ]; then
588579
# Write any LaTeX errors to stderr.
589580
>&2 grep -A 5 "] ! " "${LATEX_LOG}"
590581

591-
# Clean up after latexmk. Deliberately leave behind aux, lof, lot, and toc to speed up future runs.
592-
rm -f *.fls
593-
rm -f *.log
582+
# Copy aux, lof, lot, and toc files back to the source directory so they can be cached and speed up future runs.
594583
if [ -n "${PDFLOG_OUTPUT}" ]; then
595584
cp "${LATEX_LOG}" "${PDFLOG_OUTPUT}"
596585
fi
586+
cp *.aux "${SOURCE_DIR}"
587+
cp *.lof "${SOURCE_DIR}"
588+
cp *.lot "${SOURCE_DIR}"
589+
cp *.toc "${SOURCE_DIR}"
590+
# Copy converted images so they can be cached as well.
591+
cp *.convert.pdf "${SOURCE_DIR}"
597592
echo "Elapsed time: $(($end-$start)) seconds"
598593
# Write any LaTeX errors to stderr.
599594
>&2 grep -A 5 "! " "${LATEX_LOG}"
600595
if [[ ! "${FAILED}" = "true" ]]; then
601-
mv "${TEMP_PDF_FILE}" "${PDF_OUTPUT}"
596+
mv "${TEMP_PDF_FILE}" "${SOURCE_DIR}/${PDF_OUTPUT}"
602597
analyze_latex_logs "${LATEX_LOG}"
603598
fi
604-
rm -f "${LATEX_LOG}"
605599
fi
606600
fi
607601

@@ -611,7 +605,7 @@ if [ -n "${DOCX_OUTPUT}" ]; then
611605
subtitle="Version ${GIT_VERSION:-${DATE}}, Revision ${GIT_REVISION:-0}"
612606
# Prefix the document with a Word page-break, since Pandoc doesn't do docx
613607
# title pages.
614-
cat <<- 'EOF' > "${build_dir}/${input_file}.prefixed"
608+
cat <<- 'EOF' > "${BUILD_DIR}/${input_file}.prefixed"
615609
```{=openxml}
616610
<w:p>
617611
<w:r>
@@ -620,9 +614,9 @@ if [ -n "${DOCX_OUTPUT}" ]; then
620614
</w:p>
621615
```
622616
EOF
623-
cat ${build_dir}/${input_file} >> ${build_dir}/${input_file}.prefixed
617+
cat ${BUILD_DIR}/${input_file} >> ${BUILD_DIR}/${input_file}.prefixed
624618

625-
mkdir -p "$(dirname ${DOCX_OUTPUT})"
619+
mkdir -p "${SOURCE_DIR}/$(dirname ${DOCX_OUTPUT})"
626620
echo "Generating DOCX Output"
627621
cmd=(pandoc
628622
--embed-resources
@@ -642,8 +636,8 @@ if [ -n "${DOCX_OUTPUT}" ]; then
642636
--reference-doc=/resources/templates/tcg.docx
643637
${extra_pandoc_options}
644638
--to=docx
645-
--output="'${DOCX_OUTPUT}'"
646-
"'${build_dir}/${input_file}.prefixed'")
639+
--output="'${SOURCE_DIR}/${DOCX_OUTPUT}'"
640+
"'${BUILD_DIR}/${input_file}.prefixed'")
647641
retry 5 "${cmd[@]}"
648642
if [ $? -ne 0 ]; then
649643
FAILED=true
@@ -656,7 +650,7 @@ fi
656650
# Generate the html output
657651
export MERMAID_FILTER_FORMAT="svg"
658652
if [ -n "${HTML_OUTPUT}" ]; then
659-
mkdir -p "$(dirname ${HTML_OUTPUT})"
653+
mkdir -p "${SOURCE_DIR}/$(dirname ${HTML_OUTPUT})"
660654
echo "Generating html Output"
661655
cmd=(pandoc
662656
--toc
@@ -689,8 +683,8 @@ if [ -n "${HTML_OUTPUT}" ]; then
689683
--from=${FROM}
690684
${extra_pandoc_options}
691685
--to=html
692-
--output="'${HTML_OUTPUT}'"
693-
"'${build_dir}/${input_file}'")
686+
--output="'${SOURCE_DIR}/${HTML_OUTPUT}'"
687+
"'${BUILD_DIR}/${input_file}'")
694688
retry 5 "${cmd[@]}"
695689
if [ $? -ne 0 ]; then
696690
FAILED=true
@@ -709,7 +703,7 @@ fi
709703
rm -f core
710704
rm -f mermaid-filter.err .mermaid-config.json
711705
rm -f .puppeteer.json
712-
rm -f "${build_dir}/${input_file}.bak"
706+
rm -f "${BUILD_DIR}/${input_file}.bak"
713707

714708
echo "Overall workflow succeeded"
715709
exit 0

0 commit comments

Comments
 (0)