csb-build
Raw
#!/bin/bash
set -euo pipefail
shopt -s nullglob
HELP="Builds csb sites
Usage:
$(basename ${0}) [options]
$(basename ${0}) (-h|--help)
Options:
-h, --help Show this message and exit
-s DIR, --source DIR Directory to read files relative to [default: .]
-d DIR, --destination DIR Directory to write files relative to [default: ./public]"
TOOLS=(csb-config csb-index csb-tree csb-git csb-json jq realpath)
for tool in ${TOOLS[@]} ; do
if ! which $tool >/dev/null 2>&1 ; then
echo "ERROR: \`$tool\` not in path" >&2
exit 2
fi
done
ARGS=$(echo "$HELP" | csb-optparse "$@")
trap "rm -rf $ARGS" EXIT
[[ $(< "${ARGS}/--help") == "true" ]] && echo "$HELP" && exit 0
SOURCE="$(<$ARGS/--source)"
TARGET="$(<$ARGS/--destination)"
build_single() {
local sourcedir="$1"
local destdir="$2"
local templatedir="$3"
local name=$(basename "$sourcedir")
local stype=$(csb-config get type "${sourcedir}")
local sloc=$(csb-config get source "${sourcedir}")
csb-"${stype}" "${name}" "${sloc}" -o "${destdir}" -t "${templatedir}"
csb-index "${sourcedir}/index.md" "${destdir}/${name}/index.html" "${templatedir}/project.tmpl" --name="$name"
}
build_multi() {
local sourcedir=$1
local destdir=$2
local templatedir=$3
local index
local contentdir
local staticdir
: "${contentdir:=$(csb-config get content 2>/dev/null || true)}" "${contentdir:=content}"
: "${index:=$(csb-config get index 2>/dev/null || true)}" "${index:=$contentdir/index.md}"
: "${staticdir:=$(csb-config get static 2>/dev/null || true)}" "${staticdir:=static}"
index="${sourcedir}/${index}"
contentdir="${sourcedir}/${contentdir}"
staticdir="${sourcedir}/${staticdir}"
cp -r "${staticdir}/." "${destdir}" 2>/dev/null || true
local tempdir="${PWD}/tmp"
mkdir -p "$tempdir"
local projects=()
local n
for p in "${contentdir}"/*/ ; do
n=$(basename "$p")
build "$p" "$destdir" "$templatedir"
projects+=("${n}")
done
printf '%s\n' ${projects[@]+"${projects[@]}"} \
| jq --raw-input --slurp 'split("\n") | map(select(length>0))' \
| csb-json --template "${templatedir}/projlist.tmpl" \
> "${tempdir}/projlist.html"
csb-index "${index}" "${destdir}/index.html" "${templatedir}/frontindex.tmpl" --include "${tempdir}"
rm -rf "${tempdir}"
}
build() {
local sourcedir="${1:-${SOURCE}}"
local destdir="${2:-${TARGET}}"
local templatedir="${3:-${TEMPLATEDIR}}"
mkdir -p "${destdir}"
local sitetype
: "${sitetype:=$(csb-config get type ${sourcedir} 2>/dev/null || true)}" "${sitetype:=multi}"
if [ "$sitetype" == "multi" ]; then
destdir=$(realpath -m ${TARGET}/$(realpath -m --relative-base=$SOURCE $sourcedir))
mkdir -p "$destdir"
build_multi "$sourcedir" "$destdir" "$templatedir"
else
build_single "$sourcedir" "$destdir" "$templatedir"
fi
}
: "${SITETYPE:=$(csb-config get type 2>/dev/null || true)}" "${SITETYPE:=multi}"
: "${TEMPLATEDIR:=$(csb-config get templatedir 2>/dev/null || true)}" "${TEMPLATEDIR:=templates}"
TEMPLATEDIR="${SOURCE}/${TEMPLATEDIR}"
build