my-git-build/zola

119 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
[[ $# = 0 ]] && echo "ERROR: manyzola should be called with a project name" && exit 1
# $ 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
if [ ! -d $DEST ]; then
echo "Creating missing directory"
mkdir $DEST || echo "FAILED." && exit 3
fi
cp -R public/* $DEST/
[[ $? != 0 ]] && echo "Failed to copy project to $DEST" && exit 4
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
# Follow the symlink to make sure it's a directory
if [ -L $GITBUILDCONF/$1.basedir ]; then
dest="$(readlink -e $GITBUILDCONF/$1.basedir)"
SINGLE=1
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 ] && echo "Project has specific base_url but no basedir. Building to ~/public_html/$1" && 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)"
echo "[$1] Found target $t_name"
dest=$GITBUILDCONF/$t_name.target.basedir
# Follow symlink
[ -L $dest ] && dest="$(readlink -e $dest)"
if [ ! -d $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