Support multiple hosts and build targets

This commit is contained in:
southerntofu 2020-04-19 12:55:49 -04:00
parent 26204bc164
commit 17bfae349c
2 changed files with 107 additions and 15 deletions

View File

@ -1 +1 @@
../../public_html2/
../../public_html/

120
zola
View File

@ -1,18 +1,110 @@
#!/bin/bash
[[ $# = 0 ]] && echo "ERROR" && exit 1
[[ $# = 0 ]] && echo "ERROR: manyzola should be called with a project name" && exit 1
ARGS=""
if [ -f $GITBUILDDIR/config/hostname ]; then
base_url="https://$(cat $GITBUILDDIR/config/hostname)/~$USER/$1"
ARGS="$ARGS -u $base_url"
fi
zola build $ARGS
if [[ $? != 0 ]]; then
echo "Building site $1 failed."
exit 2
# $ manyzola project_name
# Looks up build targets in $GITBUILDCONF and builds the zola project for all targets
# A build target "t" has two files:
# - t.target.baseurl building the base_url for the site from $1 (the project_name)
# - t.target.basedir symlink to where the project should be built (can use $1)
# Additionally a project "p" can override these settings with p.baseurl and p.basedir.
# Contrary to target basedir, project basedir does not have $1 appended.
# When no target and no project settings are found:
# - if ~$GITBUILDCONF/hostname exists, build site for hostname/~$USER/$1 to ~/public_html/$1
# - otherwise, we can't do anything so we exist with error
# WARNING: this new version was only tested on multi-host setup
# Function to format base_url
# url_template "foobar" /foo/bar/urltemplate
# foobar is project name ($1)
# urltemplate is the baseurl file to expand
url_template () {
sed "s/\$1/$1/g" "$2" | envsubst
}
build_to () {
# $1 is destination folder, the rest of args is passed to zola
DEST=$1
shift # <-- Removes $1 and $2 is now $1, etc..
echo "Building to $DEST"
zola build $@
if [[ $? != 0 ]]; then
echo "Building site failed."
exit 2
fi
[ ! -d $DEST ] && mkdir $DEST
cp -R public/* $DEST/
rm -R public
}
# By default, run all targets. However if we find project-specific settings we only run those
SINGLE=0
if [ -f $GITBUILDCONF/$1.baseurl ]; then
SINGLE=1
base_url="$(url_template $1 $GITBUILDCONF/$1.baseurl)"
fi
rm -R ~/public_html/$1
cp -R public ~/public_html/$1
rm -R public
if [ -f $GITBUILDCONF/$1.basedir ]; then
SINGLE=1
dest="$(cat $GITBUILDCONF/$1.basedir)"
fi
if [[ $SINGLE = 1 ]]; then
echo "[$1] Project has specific settings"
# We are in a project-specific build!
# We have dest and/or base_url, fill missing info with defaults
# However, if we can't find a hostname to build defaut base_url, it fails!
if [ -z $base_url ]; then
if [ ! -f $GITBUILDCONF/hostname ]; then
echo "ERROR: Could not find a base_url to build $1"
exit 1
fi
base_url="https://$(cat $GITBUILDCONF/hostname)/~$USER/$1"
fi
[ -z $dest ] && dest=$HOME/public_html/$1
# BUILD
build_to $dest -u $base_url
exit $?
fi
# Now we want to fail if we don't have a hostname to build URLs from
if [ ! -f $GITBUILDCONF/hostname ]; then
echo "ERROR: Could not find a base_url to build $1"
echo "Missing file: $GITBUILDCONF/hostname"
exit 1
fi
# If no targets are found, we try to build with default settings
TARGETS=($GITBUILDCONF/*.target.baseurl)
if [[ 0 = "${#TARGETS[@]}" ]]; then
echo "[$1] Building with default settings"
base_url="https://$(cat $GITBUILDCONF/hostname)/~$USER/$1"
dest=$HOME/public_html/$1
# BUILD
build_to $dest -u $base_url
exit $?
fi
# Follow each target settings to build the site
# A target t needs both t.target.baseurl and t.target.basedir
# Contrary to single projects, targets auto-append $1 to basedir because they are intended to build many sites not 1
FAILED=0
for t in $GITBUILDCONF/*.target.baseurl; do
t_name="$(basename $t .target.baseurl)"
dest=$GITBUILDCONF/$t_name.target.basedir
echo "[$1] Found target $t_name"
# Check if basedir is symbolic link (hopefully to a real dir)
if [ ! -h $dest ]; then
echo "ERROR: no $dest. Skipping $1:$t_name"
continue
fi
base_url="$(url_template $1 $t)"
[[ "$base_url" = "" ]] && echo "FAILED TO BUILD URL" && FAILED=1 && continue
# BUILD in subdirectory
build_to "$dest/$1" -u "$base_url" || FAILED=1
done