tikz-trackschematic/build.sh

1002 lines
26 KiB
Bash
Raw Normal View History

#!/usr/bin/env sh
# Copyright (c) 2018 - 2022, Martin Scheidt (ISC license)
# Permission to use, copy, modify, and/or distribute this file for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# Variables in upper case, e.g.: $VARIABLE
# Functions in lower case as action with underscore, e.g.: do_something
#
# Halt on error
set -e
## -- print usage
print_usage() {
cat << EOF
Usage: ./build.sh [OPTIONS]
install, test or release a package for tikz-trackschematic
2022-03-10 15:26:50 +01:00
-h, --help Display this help message.
-s, --silent Run script in silent mode.
The -s option overrides any previous -v or -d options.
-v, --verbose Run script in verbose mode.
2022-03-10 15:26:50 +01:00
The -v option overrides any previous -s or -d options.
-d, --debug Run script in debug mode.
The -d option overrides any previous -s or -v options.
-m, --messy Do not clean up afterwards.
2022-03-10 15:41:17 +01:00
-n, --non-interactive Run script with no interaction.
2022-03-10 15:26:50 +01:00
-i, --install-dev Install as dev-package in local TeX Live environment.
The -i option overrides any previous -u option.
2022-03-09 18:01:10 +01:00
2022-03-10 15:26:50 +01:00
-u, --uninstall-dev Deinstall dev-package from local TeX Live environment.
The -u option overrides any previous -i option.
2022-03-10 15:26:50 +01:00
-t, --test Tests the current src/ against the test/.
-c, --compile-doc Compile documentation sources.
-y, --compile-symbology Compile symbology sources.
2022-03-10 15:26:50 +01:00
-r, --release VERSION Creates a .zip with the release for given VERSION in
Semantic Versioning with leading 'v', e.g: v1.0.0
EOF
}
## -- processes getopts
2022-03-10 15:26:50 +01:00
VERBOSITY=2 # set by cli argument
NOINTERACT=0 # set by cli argument
INSTALL=0 # set by cli argument
TESTING=0 # set by cli argument
COMPILE=0 # set by cli argument
SYMBOLOGY=0 # set by cli argument
RELEASE=0 # set by cli argument
CLEANUP=1 # set by cli argument
process_arguments() {
while true; do
2022-03-07 11:13:49 +01:00
# loop condition - test for empty string:
2022-03-10 15:41:17 +01:00
if [ -z "$1" ]; then break; fi
2022-03-07 11:13:49 +01:00
# loop test
case $1 in
-h|--help)
print_usage
exit 0
;;
2022-03-10 15:26:50 +01:00
-s|--silent)
VERBOSITY=0
;;
-v|--verbose)
2022-03-10 15:26:50 +01:00
VERBOSITY=3
;;
-d|--debug)
VERBOSITY=4
;;
-m|--messy)
CLEANUP=0
;;
2022-03-10 15:41:17 +01:00
-n|--non-interactive)
NOINTERACT=1
;;
-i|--install-dev)
INSTALL=1
;;
2022-03-09 18:01:10 +01:00
-u|--uninstall-dev)
INSTALL=2
;;
-t|--test)
TESTING=1
;;
-c|--compile-doc)
COMPILE=1
;;
-y|--compile-symbology)
SYMBOLOGY=1
;;
-r|--release)
RELEASE=1
shift
2022-03-10 17:17:17 +01:00
if [ -z "$1" ] || [ "${1:0:1}" = "-" ]; then
print_usage
exit 1
fi
VERSION_STR=$1
;;
*)
print_usage
exit 1
;;
esac
shift
done
}
## -- run variables
#
PDFTOPPM_CONVERT=0 # set by check_pdftoppm
POLICY_MOD=0 # set by check_imagemagick_policy
#
ERROR_OCCURRED=0
2022-03-10 15:26:50 +01:00
## -- logging functions
## colors
RED="\033[0;31m"
GREEN="\033[0;32m"
2022-03-10 15:26:50 +01:00
YELLOW="\033[0;33m"
COLOR_RESET="\033[0;m"
2022-03-10 15:26:50 +01:00
## cross platform echo option
if [ "`echo -n`" = "-n" ]; then
n=""; c="\c"
else
n="-n"; c=""
fi
2022-03-10 15:26:50 +01:00
log() {
NO_LINE_BREAK=0
2022-03-11 11:04:36 +01:00
case $2 in
-n) NO_LINE_BREAK=1;;
esac
2022-03-11 10:55:21 +01:00
2022-03-11 11:04:36 +01:00
COLOR=${COLOR_RESET}
2022-03-11 10:55:21 +01:00
case $1 in
1) COLOR=${RED};;
2) COLOR=${GREEN};;
4) COLOR=${YELLOW};;
esac
2022-03-10 15:26:50 +01:00
if [ $VERBOSITY -ge $1 ]; then
shift
if [ $NO_LINE_BREAK = 0 ]; then
echo "${COLOR}$@${COLOR_RESET}" | fold -s
else
shift
echo $n "${COLOR}$@ $c${COLOR_RESET}" | fold -s
fi
fi
}
2022-03-10 15:26:50 +01:00
log_show() { log 0 $@; } # will always show
log_error() { log 1 $@; } # print message in RED ; but not when --silent
log_info() { log 2 $@; } # print message in GREEN ; but not when --silent
log_note() { log 3 $@; } # print message ; with --verbose and --debug
log_debug() { log 4 $@; } # print message in YELLOW; only with --debug
2022-03-11 10:51:26 +01:00
## -- user interaction
user_confirmation () {
if [ ! $NOINTERACT = 1 ]; then
log_show $@
log_show -n "(y/n)"
while true; do
read -p "" answer
case $answer in
[Yy]* ) break;;
[Nn]* ) exit 1;;
* ) log_show "Please answer yes or no.";;
esac
done
fi
}
## -- commands
check_path() {
2022-03-09 18:32:29 +01:00
# test for project specific files
STATUS=0
set -- doc/tikz-trackschematic-documentation.sty src/tikz-trackschematic.sty test/turnout.tex
for FILE in "$@"; do
2022-03-09 18:32:29 +01:00
if [ ! -e $FILE ]; then
STATUS=1
fi
done
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "Build script is within the project folder."
2022-03-09 18:32:29 +01:00
return 0
fi
2022-03-09 18:32:29 +01:00
2022-03-10 15:26:50 +01:00
log_error "Please run this script from inside the project folder!"
2022-03-09 18:32:29 +01:00
exit 1
}
2022-03-09 13:43:36 +01:00
## checks for installed software
2022-03-10 15:26:50 +01:00
sedi () {
## cross platform sed -i option without a backup file name
# https://stackoverflow.com/questions/2320564/sed-i-command-for-in-place-editing-to-work-with-both-gnu-sed-and-bsd-osx
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
}
check_zip() {
# check for zip
STATUS=0
command -v zip >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "zip found"
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Program 'zip' not found. Be sure to have zip installed!"
exit 1
}
2022-03-09 13:43:36 +01:00
check_sudo() {
# checks if sudo is available
2022-03-10 15:26:50 +01:00
STATUS=0
2022-03-09 13:43:36 +01:00
rootrun=""
# If we are root, we do note require sudo
if [ "$EUID" = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "you are root"
2022-03-09 13:43:36 +01:00
return 0
fi
2022-03-10 15:26:50 +01:00
command -v sudo >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
log_note "sudo found"
2022-03-09 13:43:36 +01:00
rootrun="sudo"
2022-03-10 15:26:50 +01:00
return 0
2022-03-09 13:43:36 +01:00
else
2022-03-10 15:26:50 +01:00
log_debug "sudo failed."
2022-03-09 13:43:36 +01:00
fi
2022-03-10 15:26:50 +01:00
log_error "This script must be run as root!"
exit 1
2022-03-09 13:43:36 +01:00
}
check_texlive() {
# check for kpsewhich (and mktexlsr)
STATUS=0
command -v kpsewhich >/dev/null 2>&1 || STATUS=1
command -v mktexlsr >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "kpsewhich and mktexlsr found"
2022-03-09 13:43:36 +01:00
TEXMFLOCAL=$(kpsewhich --var-value TEXMFLOCAL)
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Program 'kpsewhich' or 'mktexlsr' not found. Be sure to use texlive or mactex!"
2022-03-09 13:43:36 +01:00
exit 1
}
check_latexmk() {
# check for latexmk
STATUS=0
command -v latexmk >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
log_note "latexmk found"
return 0
fi
log_error "Program 'latexmk' not found. Be sure to have texlive or mactex installed!"
exit 1
}
2022-03-09 13:43:36 +01:00
check_pdflatex() {
# check for pdflatex
STATUS=0
command -v pdflatex >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "pdflatex found"
2022-03-09 13:43:36 +01:00
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Program 'pdflatex' not found. Be sure to have texlive or mactex installed!"
2022-03-09 13:43:36 +01:00
exit 1
}
check_imagemagick() {
# check for ImageMagick/compare
STATUS=0
command -v compare >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "compare found"
2022-03-09 13:43:36 +01:00
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Program 'compare' not found. Be sure to have ImageMagick installed!"
2022-03-09 13:43:36 +01:00
exit 1
}
check_pdftoppm() {
# check for poppler/pdftoppm
STATUS=0
command -v pdftoppm >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "pdftoppm found"
2022-03-09 13:43:36 +01:00
PDFTOPPM_CONVERT=1
return 0
fi
2022-03-11 10:51:26 +01:00
log_note "Program 'pdftoppm' not found."
2022-03-10 15:26:50 +01:00
# no # exit 1 ## can still modify ImageMagick policy!
2022-03-09 13:43:36 +01:00
}
check_pdf2svg() {
# check for poppler/pdf2svg
STATUS=0
command -v pdf2svg >/dev/null 2>&1 || STATUS=1
if [ $STATUS = 0 ]; then
log_note "pdf2svg found"
return 0
fi
log_note "Program 'pdf2svg' not found."
exit 1
}
2022-03-09 13:43:36 +01:00
check_imagemagick_policy() {
STATUS=1
convert -list policy | grep -q "pattern: PDF" || STATUS=0
2022-03-09 13:43:36 +01:00
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "ImageMagick allows to convert PDFs. Great!"
2022-03-09 13:43:36 +01:00
return 0
else
2022-03-10 15:26:50 +01:00
log_note "ImageMagick does not allow to convert PDFs."
2022-03-09 13:43:36 +01:00
## check for alternative
check_pdftoppm # if pdftoppm is available, then PDFTOPPM_CONVERT=1
if [ $PDFTOPPM_CONVERT = 0 ]; then
## modify ImageMagick-6/policy.xml
2022-03-11 10:51:26 +01:00
user_confirmation "Be sure to have either poppler(-utils) installed or an ImageMagick \
policy which allows for PDF conversion! Do you wish to temporaly remove \
the policy preventing ImageMagick from converting PDFs?"
2022-03-09 13:43:36 +01:00
check_sudo
POLICY_PATH=$(convert -list policy | grep "Path" | awk "NR==1" | cut -d " " -f2) # default /etc/ImageMagick-*/policy.xml
2022-03-09 22:48:55 +01:00
if [ ! -e $POLICY_PATH ]; then
VERSION=$(convert --version | grep "Version" | cut -d " " -f3 | cut -d "." -f1 )
POLICY_PATH="/etc/ImageMagick-${VERSION}/policy.xml"
2022-03-09 22:48:55 +01:00
if [ ! -e $POLICY_PATH ]; then
2022-03-10 15:26:50 +01:00
log_error "ImageMagick policy is preventing converting PDFs to PNGs and program \
'pdftoppm' was not found. Modifying the policy temporaly failed! Be sure \
to have either poppler(-utils) installed or an ImageMagick policy which \
allows for PDF conversion!"
exit 1
fi
fi
2022-03-09 14:53:39 +01:00
2022-03-09 13:43:36 +01:00
POLICY_MOD=1
$rootrun sed -i".backup" 's/^.*policy.*coder.*none.*PDF.*//' $POLICY_PATH
2022-03-10 15:26:50 +01:00
log_note "Modified ${POLICY_PATH}!"
2022-03-09 13:43:36 +01:00
fi
fi
}
check_trackschematic() {
# check for tikz-trackschematic
STATUS=0
TEXMFLOCAL=$(kpsewhich --var-value TEXMFLOCAL)
2022-03-09 18:01:10 +01:00
DEVDIR=$(find $TEXMFLOCAL -name 'tikz-trackschematic-dev')
2022-03-09 13:43:36 +01:00
2022-03-09 18:01:10 +01:00
ls $DEVDIR/tikz-trackschematic-dev.sty >> /dev/null 2>&1 || STATUS=1
2022-03-09 13:43:36 +01:00
if [ $STATUS = 0 ]; then
log_note "Package tikz-trackschematic-dev found."
2022-03-09 13:43:36 +01:00
return 0
fi
log_note "Package 'tikz-trackschematic-dev' not found - using project src/."
export TEXINPUTS=.:$(pwd)/src/:$TEXINPUTS
2022-03-09 13:43:36 +01:00
}
## checks for updated repository
check_version_number() {
2022-03-07 11:13:49 +01:00
while true; do
# loop condition - test format of $VERSION_STR:
echo "$VERSION_STR" | egrep -q "v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)?" && break;
# loop test
if [ $NOINTERACT = 0 ]; then
2022-03-10 15:26:50 +01:00
log_error "Your version '$VERSION_STR' has not the correct format!"
log_show -n "Please specify as Semantic Versioning ( e.g. v1.0.0 ): "
read VERSION_STR
else
2022-03-10 15:26:50 +01:00
log_error "Your version '$VERSION_STR' has not the correct format! \
Please use Semantic Versioning with leading 'v'"
exit 1
fi
done
# remove leading 'v'
VERSION_NUM=$(echo $VERSION_STR | cut -c 2-)
}
check_versionhistory() {
# check if $VERSION is present in doc/versionhistory.tex
STATUS=0
grep -qs "vhEntry{$VERSION_NUM" doc/versionhistory.tex || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "Version $VERSION_NUM is present in versionhistory.tex."
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Version $VERSION_NUM not found in versionhistory.tex. \
Be sure to edit versionhistory.tex and specify current version!"
exit 1
}
check_changelog() {
# check if $VERSION is present in CHANGELOG.md
STATUS=0
grep -qs "Version \[$VERSION_NUM\]" CHANGELOG.md || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "Version $VERSION_NUM is present in CHANGELOG.md."
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Version $VERSION_NUM not found in CHANGELOG.md. \
Be sure to edit CHANGELOG.md and specify current version!"
exit 1
}
check_date() {
## extract DATE from versionhistory.tex and CHANGELOG.md
LINE_1=$(grep "vhEntry{$VERSION_NUM" doc/versionhistory.tex)
LINE_2=$(grep "Version \[$VERSION_NUM\]" CHANGELOG.md)
DATEISO_1=$(echo $LINE_1 | egrep -o '[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
DATEISO_2=$(echo $LINE_2 | egrep -o '[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])')
if [ $DATEISO_1 = $DATEISO_2 ]; then
# DATE=$(date "+%Y-%m-%d")
DATE="$DATEISO_1"
2022-03-10 15:26:50 +01:00
log_note "The date $DATE was extracted from versionhistory.tex and CHANGELOG.md."
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "The date in versionhistory.tex and CHANGELOG.md did not match.\
Be sure to edit versionhistory.tex or CHANGELOG.md and modifiy the date!"
exit 1
}
check_url1() {
## extract urls from CHANGELOG.md
STATUS=0
LINE=$(grep "\[$VERSION_NUM\]: https://" CHANGELOG.md)
echo $LINE | grep -qs "...$VERSION_STR" || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "Version $VERSION_NUM URL is present in CHANGELOG.md."
return 0
fi
2022-03-10 15:26:50 +01:00
log_error "Version $VERSION_NUM URL was not found in CHANGELOG.md. \
Be sure to edit CHANGELOG.md and specify a URL for the current version!"
exit 1
}
check_url2() {
## extract urls from CHANGELOG.md
STATUS=0
LINE=$(grep "\[Unreleased\]: https://" CHANGELOG.md)
echo $LINE | grep -qs "/$VERSION_STR..." || STATUS=1
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_note "The URL for [Unreleased] was also updated in CHANGELOG.md! Thx!"
return 0
fi
2022-03-10 15:26:50 +01:00
log_show "WARNING: URL for [Unreleased] in CHANGELOG.md does not reflect the current version $VERSION_NUM."
log_show "WARNING: Be sure to edit CHANGELOG.md and specify current version!"
2022-03-11 10:51:26 +01:00
user_confirmation "Do you wish to continue without updated URL for [Unreleased]?"
2022-03-11 10:51:26 +01:00
if [ $NOINTERACT = 1 ]; then
2022-03-10 15:26:50 +01:00
log_error "Aborting in batch mode!"
exit 1
fi
}
2022-03-09 13:43:36 +01:00
## functionality
create_release() {
####
# This function produces a .zip-file in accordance to the requirements for CTAN.
# For more information see https://ctan.org/help/upload-pkg.
####
2022-03-11 10:51:26 +01:00
user_confirmation "Do you wish to create a release for the version $VERSION_NUM?"
## create backup-file und update VERSIONDATE in tikz-trackschematic.sty
sed -i".backup" -e"s/%\[VERSIONDATE/\[$DATE $VERSION_STR/g" src/tikz-trackschematic.sty
sedi "/%%\[SCRIPT\]/d" src/tikz-trackschematic.sty
2022-03-10 15:26:50 +01:00
log_note "Updated version in src/tikz-trackschematic.sty"
## -- create zip-archive
# create temporary folder
2022-03-08 14:42:27 +01:00
TMP="tikz-trackschematic-$VERSION_STR"
mkdir -p $TMP
# copy README, CHANGELOG, LICENSE and CITATION
cp README.md $TMP/README.md
cp CHANGELOG.md $TMP/CHANGELOG.md
cp LICENSE $TMP/LICENSE
cp CITATION.cff $TMP/CITATION.cff
# copy and rename documentation
cp doc/tikz-trackschematic-documentation.sty $TMP/
cp doc/manual.pdf $TMP/tikz-trackschematic.pdf
cp doc/manual.tex $TMP/tikz-trackschematic.tex
cp doc/snippets.pdf $TMP/tikz-trackschematic-snippets.pdf
cp doc/snippets.tex $TMP/tikz-trackschematic-snippets.tex
cp doc/symbology-table.pdf $TMP/tikz-trackschematic-symbology-table.pdf
cp doc/symbology-table.tex $TMP/tikz-trackschematic-symbology-table.tex
mkdir $TMP/tikz-trackschematic-examples
mkdir $TMP/tikz-trackschematic-snippets
cp -R doc/examples/* $TMP/tikz-trackschematic-examples/
cp -R doc/snippets/* $TMP/tikz-trackschematic-snippets/
2022-03-10 15:26:50 +01:00
log_note "copied documentation"
# copy src-files
for SRC in src/*; do
EXT=${SRC##*.}
# do not copy backup created by sed
if [ $EXT != "backup" ]; then
cp $SRC $TMP/
fi
done
2022-03-10 15:26:50 +01:00
log_note "copied src-files"
# zip package
2022-03-10 15:26:50 +01:00
zip -r $TMP.zip $TMP/* >/dev/null 2>&1
log_note "compressed the release in $TMP.zip"
}
create_release_notes() {
2022-03-11 10:51:26 +01:00
user_confirmation "Do you wish to create a release notes for the version $VERSION_NUM?"
## -- create release note as excerpt from CHANGELOG.md
# determine beginning and end in CHANGELOG.md
TOP=$(grep -n "Version \[$VERSION_NUM\]" CHANGELOG.md | cut -d: -f1)
awk "NR>$TOP" CHANGELOG.md > release-note-tmp.md
BOTTOM=$(grep -n -m 1 "## Version" release-note-tmp.md | cut -d: -f1)
BOTTOM=$(( $TOP + $BOTTOM ))
BOTTOM=$(( $BOTTOM - 2 ))
TOP=$(( $TOP + 1 ))
# extract the excerpt
awk "NR>$TOP&&NR<$BOTTOM" CHANGELOG.md > release-note-$VERSION_STR.md
sedi "s/###/##/g" release-note-$VERSION_STR.md
}
run_compile() {
## compile order
# 1. manual, symbology-table, snippets
# 2. examples
# 3. symbology
cd doc/
mkdir -p .tex
log_debug "entered documentation dir"
## 1. main documentation
set -- manual symbology-table snippets
for NAME in "$@"; do
log_info -n "compiling $NAME:"
#
## TeX build
EXIT_CODE=0
/usr/bin/time -p -o .tex/${NAME}.time \
latexmk -pdf -f -g -emulate-aux-dir -auxdir=.tex -outdir=.tex $NAME.tex >> /dev/null 2>&1 || EXIT_CODE=1
#
TIME=$(awk "NR==2" .tex/${NAME}.time | cut -d " " -f2)
# understanding TeX statistics:
# -> https://tex.stackexchange.com/questions/26208/components-of-latexs-memory-usage
MEMORY_USAGE=$(grep "words of memory out of" .tex/${NAME}.log | cut -d " " -f2)
MEMORY_USAGE=$(($MEMORY_USAGE/1000))
#
## compiling snipptes.tex may run out of memory!
## to increase available memory find local texmf.cnf:
# kpsewhich -a texmf.cnf
# returns /usr/local/texlive/2021/texmf.cnf
## append in /usr/local/texlive/2021/texmf.cnf
# % increase available memory
# main_memory = 12000000
# extra_mem_bot = 12000000
# font_mem_size = 12000000
# pool_size = 12000000
# buf_size = 12000000
## run
# sudo mktexlsr
#
if [ $EXIT_CODE = 0 ]; then
log_info " - build successful in ${TIME}s and with ${MEMORY_USAGE}k memory."
#
mv .tex/$NAME.pdf $NAME.pdf
log_debug "copied $NAME to doc/"
else
ERROR_OCCURRED=1
log_error " - build failed."
fi
done
## 2. examples
cd examples/
mkdir -p .tex
EXAMPLEDIR="../examples"
for EXAMPLE in `ls $EXAMPLEDIR/*.tex`; do
FILE=$(basename "$EXAMPLE") # remove path
NAME=${FILE%.*} # remove extension
#
log_info -n "compiling $FILE:"
#
## TeX build
EXIT_CODE=0
/usr/bin/time -p -o .tex/${NAME}.time \
latexmk -pdf -f -g -emulate-aux-dir -auxdir=.tex -outdir=.tex $NAME.tex >> /dev/null 2>&1 || EXIT_CODE=1
#
TIME=$(awk "NR==2" .tex/${NAME}.time | cut -d " " -f2)
# understanding TeX statistics:
# -> https://tex.stackexchange.com/questions/26208/components-of-latexs-memory-usage
MEMORY_USAGE=$(grep "words of memory out of" .tex/${NAME}.log | cut -d " " -f2)
MEMORY_USAGE=$(($MEMORY_USAGE/1000))
#
if [ $EXIT_CODE = 0 ]; then
log_info " - build successful in ${TIME}s and with ${MEMORY_USAGE}k memory."
#
mv .tex/$NAME.pdf $NAME.pdf
log_debug "copied $NAME to doc/examples/"
#
if [ $PDFTOPPM_CONVERT = 0 ]; then
# 'compare' will convert the pdf to png
# -> this reasonably fast!
convert -density 300 ${NAME}.pdf ${NAME}.png >> /dev/null 2>&1
else
# use 'pdftoppm' convert the pdf to png
# -> this is slower!
pdftoppm -png -r 300 -singlefile ${NAME}.pdf ${NAME}.png
fi
log_debug "converted $NAME.pdf to PNG"
else
ERROR_OCCURRED=1
log_error " - build failed."
fi
done
cd ..
## 3. symbology
cd ..
}
run_symbology() {
cd doc/symbology/
mkdir -p .tex
for FILE in symbols_tikz/*.tikz; do
SYMBOL=$(basename $FILE .tikz)
log_note "converting: $SYMBOL"
## -- header tex file
echo '\\documentclass[tikz,border=0]{standalone}' > tmp.tex
echo '\\usepackage[dev]{tikz-trackschematic}' >> tmp.tex
echo '\\begin{document}' >> tmp.tex
echo '\\begin{tikzpicture}[font=\\sffamily]' >> tmp.tex
## -- input symbol
echo '\\input{'$FILE'}' >> tmp.tex
## -- footer tex file
echo '\\end{tikzpicture}' >> tmp.tex
echo '\\end{document}' >> tmp.tex
# echo "---------------"
# cat tmp.tex
# echo "---------------"
## -- compile tmp.tex
# pdflatex -output-directory=.tex tmp.tex
pdflatex -output-directory=.tex -interaction=batchmode tmp.tex 2>&1 > /dev/null
## -- copy and convert symbols
pdf2svg .tex/tmp.pdf symbols_svg/$SYMBOL.svg
if [ $PDFTOPPM_CONVERT = 0 ]; then
# 'compare' will convert the pdf to png
# -> this reasonably fast!
convert -density 600 .tex/tmp.pdf symbols_png/$SYMBOL.png >> /dev/null 2>&1
else
# use 'pdftoppm' convert the pdf to png
# -> this is slower!
pdftoppm -png -r 600 -singlefile .tex/tmp.pdf symbols_png/$SYMBOL.png
fi
#
mv .tex/tmp.pdf symbols_pdf/$SYMBOL.pdf
done
cd ../..
}
run_test_cases() {
cd test/
TESTDIR="../test"
mkdir -p .tex
STATUS=0
# Start with an empty List:
FAILED=""
for TEST in `ls $TESTDIR/*.tex`; do
# setup
FILE=$(basename "$TEST") # remove path
NAME=${FILE%.*} # remove extension
ADD_TO_LIST=0
#
2022-03-10 15:26:50 +01:00
log_info "$NAME test:"
#
#
## TeX build
#
2022-03-07 13:44:34 +01:00
EXIT_CODE=0
/usr/bin/time -p -o .tex/${NAME}.time \
pdflatex -output-directory=.tex -interaction=NOINTERACT -halt-on-error ${NAME}.tex >> /dev/null 2>&1 || EXIT_CODE=1
2022-03-07 13:44:34 +01:00
#
TIME=$(awk "NR==2" .tex/${NAME}.time | cut -d " " -f2)
# understanding TeX statistics:
# -> https://tex.stackexchange.com/questions/26208/components-of-latexs-memory-usage
2022-03-07 13:44:34 +01:00
MEMORY_USAGE=$(grep "words of memory out of" .tex/${NAME}.log | cut -d " " -f2)
MEMORY_USAGE=$(($MEMORY_USAGE/1000))
#
if [ $EXIT_CODE = 0 ]; then
2022-03-10 15:26:50 +01:00
log_info " - build successful in ${TIME}s and with ${MEMORY_USAGE}k memory."
else
STATUS=1
ADD_TO_LIST=1
2022-03-10 15:26:50 +01:00
log_error " - build failed."
fi
#
#
## compare images with following compare metrics
# AE: absolute error count, number of different pixels (-fuzz affected)
# DSSIM: structural dissimilarity index
# FUZZ: mean color distance
# MAE: mean absolute error (normalized), average channel error distance
# MEPP: mean error per pixel (normalized mean error, normalized peak error)
# MSE: mean error squared, average of the channel error squared
# NCC: normalized cross correlation
# PAE: peak absolute (normalized peak absolute)
# PHASH: perceptual hash for the sRGB and HCLp colorspaces.
# PSNR: peak signal to noise ratio
# RMSE: root mean squared (normalized root mean squared)
# SSIM: structural similarity index
#
EXIT_CODE=0
if [ $PDFTOPPM_CONVERT = 0 ]; then
# 'compare' will convert the pdf to png
# unless the policy of ImageMagick prevents it
# -> this reasonably fast!
compare -metric RMSE -colorspace RGB .tex/${NAME}.pdf ${NAME}_expected.pdf NULL: >> /dev/null 2>&1 || EXIT_CODE=1
else
# use 'pdftoppm' convert the pdf to png
# then use 'compare' for comparison without converting
# -> this is slower!
2022-03-09 23:02:53 +01:00
pdftoppm -png -r 144 -singlefile .tex/${NAME}.pdf .tex/${NAME}
pdftoppm -png -r 144 -singlefile ${NAME}_expected.pdf .tex/${NAME}_expected
compare -metric RMSE -colorspace RGB .tex/${NAME}.png .tex/${NAME}_expected.png NULL: >> /dev/null 2>&1 || EXIT_CODE=1
fi
if [ $EXIT_CODE = 0 ]; then
2022-03-10 15:26:50 +01:00
log_info " - comparison successful."
else
STATUS=1
ADD_TO_LIST=1
2022-03-10 15:26:50 +01:00
log_error " - comparison failed."
fi
## if a test failed add to list
if [ $ADD_TO_LIST = 1 ]; then
if [ -z "$FAILED" ]; then
# first item
FAILED="$NAME"
else
FAILED="$FAILED, $NAME"
fi
fi
done
if [ $STATUS = 0 ]; then
2022-03-10 15:26:50 +01:00
log_info "\n=> All tests passed!\n"
else
2022-03-10 15:26:50 +01:00
log_error "\n=> Some or all tests failed!"
log_debug "The following tests failed: ${FAILED}!"
2022-03-08 14:42:27 +01:00
ERROR_OCCURRED=1
fi
2022-03-08 14:42:27 +01:00
cd ..
}
link_dev_files() {
# destination folder inside the TeX Live installation
2022-03-09 18:01:10 +01:00
TEXMFLOCAL=$(kpsewhich --var-value TEXMFLOCAL)
DEVDIR="$TEXMFLOCAL/tex/latex/tikz-trackschematic-dev"
PROJECTDIR=$(pwd -P)
2022-03-11 10:51:26 +01:00
user_confirmation "Do you wish to install this package for development to $DEVDIR?"
# make sure that destination folder exists
2022-03-09 18:01:10 +01:00
if [ ! -d "$DEVDIR" ]; then
$rootrun mkdir -p $DEVDIR
fi
# link every file in src/ and rename it
for SRC in src/*; do
FILE=$(basename "$SRC") # remove path
NAME=${FILE%.*} # remove extension
PREFIX=${NAME%%.*}
POSTFIX=${NAME#*.}
EXT=${SRC##*.}
if [ "$PREFIX" = "$POSTFIX" ]; then
DST="$PREFIX-dev.$EXT"
else
DST="$PREFIX-dev.$POSTFIX.$EXT"
fi
2022-03-09 18:01:10 +01:00
$rootrun ln -sfn $PROJECTDIR/$SRC $DEVDIR/$DST
2022-03-10 15:26:50 +01:00
if [ $VERBOSITY = 1 ]; then
echo "linked '$DST'"
fi
done
# update TeX Live installation
TEXlsr=`which mktexlsr`
2022-03-10 15:26:50 +01:00
if [ $VERBOSITY -ge 3 ]; then
$rootrun $TEXlsr
else
$rootrun $TEXlsr --quiet
fi
}
2022-03-09 18:01:10 +01:00
remove_dev_files() {
# destination folder inside the TeX Live installation
cd $DEVDIR # from check_trackschematic
2022-03-11 10:51:26 +01:00
user_confirmation "Do you wish to remove the package '$DEVDIR'?"
2022-03-10 15:26:50 +01:00
log_note "removing $DEVDIR!"
2022-03-09 18:01:10 +01:00
$rootrun rm -f *
cd ..
$rootrun rmdir tikz-trackschematic-dev
# update TeX Live installation
TEXlsr=`which mktexlsr`
2022-03-10 15:26:50 +01:00
if [ $VERBOSITY -ge 3 ]; then
2022-03-09 18:01:10 +01:00
$rootrun $TEXlsr
else
$rootrun $TEXlsr --quiet
fi
}
2022-03-08 14:42:27 +01:00
cleanup() {
## -- cleanup
## from create_release
if [ $CLEANUP = 1 ]; then
if [ $RELEASE = 1 ]; then
# remove TMP-folder
rm -rf $TMP
# undo changes to tikz-trackschematic.sty by sed
mv src/tikz-trackschematic.sty.backup src/tikz-trackschematic.sty
# remove TMP-release-note
rm release-note-tmp.md
fi
2022-03-08 14:42:27 +01:00
## from check_imagemagick_policy
if [ $POLICY_MOD = 1 ]; then
# undo changes to /etc/ImageMagick-6/policy.xml by sed
$rootrun mv ${POLICY_PATH}.backup $POLICY_PATH
fi
2022-03-08 14:42:27 +01:00
## from run_compile
if [ $COMPILE = 1 ]; then
# remove TMP-folder
rm -rf doc/examples/.tex
rm -rf doc/.tex
fi
## from run_symbology
if [ $SYMBOLOGY = 1 ]; then
# remove TMP-folder
rm -rf doc/symbology/.tex/
rm doc/symbology/tmp.tex
fi
## from run_test_cases
if [ $TESTING = 1 ]; then
# remove TMP-folder
rm -rf test/.tex
fi
##
2022-03-10 15:26:50 +01:00
log_note "Clean up done!"
2022-03-08 14:42:27 +01:00
fi
}
## -- execution
## Process user arguments
process_arguments $@
check_path
## do what is requested
if [ $INSTALL = 1 ]; then
2022-03-09 18:01:10 +01:00
## install package
check_texlive
check_sudo
##
link_dev_files
fi
2022-03-09 18:01:10 +01:00
if [ $INSTALL = 2 ]; then
## deinstall package
check_texlive
check_trackschematic
check_sudo
##
remove_dev_files
fi
if [ $TESTING = 1 ]; then
##
check_pdflatex
check_trackschematic
check_imagemagick
check_imagemagick_policy
##
run_test_cases
fi
if [ $COMPILE = 1 ]; then
##
check_latexmk
check_trackschematic
check_imagemagick
check_imagemagick_policy
##
run_compile
fi
if [ $SYMBOLOGY = 1 ]; then
##
check_pdflatex
check_trackschematic
check_imagemagick
check_imagemagick_policy
check_pdf2svg
##
run_symbology
fi
if [ $RELEASE = 1 ]; then
## check if version ist in the correct format
check_version_number
## check if $VERSION is present in CHANGELOG.md and versionhistory.tex
check_versionhistory
check_changelog
check_url1
check_url2
check_date
## check for installed software
check_zip
##
create_release
create_release_notes
fi
2022-03-08 14:42:27 +01:00
##
cleanup
##
if [ $ERROR_OCCURRED = 0 ]; then
exit 0
else
exit 1
fi
#EOF