Skip to content

Instantly share code, notes, and snippets.

@davidsneighbour
Created June 2, 2024 10:18
Show Gist options
  • Save davidsneighbour/5406a786c2f8425e302fd3a50f8ef327 to your computer and use it in GitHub Desktop.
Save davidsneighbour/5406a786c2f8425e302fd3a50f8ef327 to your computer and use it in GitHub Desktop.
My (old) bash script to build GoHugo websites. Using node now.
#!/bin/bash
function echo_warning() {
local message="$1"
# ANSI escape sequence for bold red text
echo -e "\033[1;31m#######################################################################\033[0m"
echo -e "\033[1;31m${message}\033[0m"
echo -e "\033[1;31m#######################################################################\033[0m"
}
echo_warning "THIS SCRIPT IS DEPRECATED, USE 'node ./bin/hugo/build.js' INSTEAD!"
# description: build GoHugo website
# usage: ./bin/hugo/build [baseURL]
set -eo pipefail
IFS=$'\n\t'
CURPATH=$(pwd -P)
check_required_tools() {
local missing_tools=()
local tools=(hugo git npm)
for tool in "${tools[@]}"; do
if ! command -v "$tool" &>/dev/null; then
missing_tools+=("$tool")
fi
done
if [ ${#missing_tools[@]} -ne 0 ]; then
echo "Missing required tools: ${missing_tools[*]}"
exit 1
fi
}
load_env_variables() {
local env_file=".env"
if [ -f "$env_file" ]; then
set -a
source "$env_file"
set +a
fi
}
check_modules() {
local modules=("hugo-modules")
local module_path
for module in "${modules[@]}"; do
module_path="../$module"
if [ -d "$module_path" ]; then
cd "$module_path"
if [[ $(git diff --stat) != '' ]]; then
echo "$module is dirty"
exit 128
fi
cd - >/dev/null
fi
done
}
parse_arguments() {
BASE_URL_PARAM=""
if [ -n "$1" ]; then
BASE_URL_PARAM="--baseURL=$1"
fi
}
process_replacements() {
local replacements_file="${CURPATH}/bin/etc/hugo/replacements"
local replacements=""
local not_first_line=false
if [ -f "$replacements_file" ]; then
while read -r -a line; do
if $not_first_line; then
replacements="$replacements,${line[0]} -> ${line[1]}"
else
replacements="${line[0]} -> ${line[1]}"
not_first_line=true
fi
done <"$replacements_file"
export HUGO_MODULE_REPLACEMENTS=$replacements
fi
}
run_hugo() {
npm run clean
hugo mod get -u ./...
env HUGO_MODULE_REPLACEMENTS="$HUGO_MODULE_REPLACEMENTS" \
hugo \
--logLevel "$LOGLEVEL" \
--enableGitInfo \
$BASE_URL_PARAM
}
main() {
check_required_tools
load_env_variables
# check_modules
parse_arguments "$@"
process_replacements
run_hugo
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment