Big update to pass the first batch of tests

This commit is contained in:
southerntofu 2020-09-15 22:08:27 +02:00
parent 832ecb6d1f
commit cc0d8d4c92
1 changed files with 69 additions and 39 deletions

View File

@ -1,27 +1,35 @@
#/bin/bash
# This script was indeed self-updated!
#! /bin/bash
# Catch signals so we don't leave zombie tasks behind if we Ctrl^C
# No idea why but it seems to prevent the script from working? Disable!
#trap "exit" INT TERM ERR
#trap "kill 0" EXIT
BASEDIR="$HOME/.git-build"
[ ! -d $BASEDIR ] && mkdir $BASEDIR
# So scripts can know we're still running (for autoupdater)
echo "$BASHPID" > $BASEDIR/.LOCK
# Default logging levels (error is always enabled)
INFO=1
DEBUG=0
# TODO: Remove this quickhack which prevents from using real value in tasks
# (but enables unit testing of host-based config)
if [ ! -z $HOST ]; then
HOSTNAME="$HOST"
fi
# Extract two letters from $LANG
locale="${LANG:0:2}"
# Ensure translations have been setup
if [ -d /usr/share/forgebuild/i18n ]; then
I18N_DIR=/usr/share/forgebuild/i18n
elif [ -d $HOME/.local/share/forgebuild/i18n ]; then
I18N_DIR=$HOME/.local/share/forgebuild/i18n
else
echo "ERROR: could not find translations. Maybe you need to run the setup.sh script?"
exit 1
fi
# Initialize translations
[ -f i18n/$locale.json ] && locale_strings="$(cat i18n/$locale.json)" || locale_strings="$(cat i18n/en.json)"
[ -f $I18N_DIR/$locale.json ] && locale_strings="$(cat $I18N_DIR/$locale.json)" || locale_strings="$(cat $I18N_DIR/en.json)"
# Takes one argument, looks up translation
trans() {
@ -52,6 +60,14 @@ debug () {
[[ $DEBUG = 1 ]] && echo -e "\e[36m$(trans debug)\e[0m$(trans $1)" | envsubst
}
help () {
echo "forgebuild [tasks]"
echo " Trigger updates on `tasks` (all tasks by default)"
echo "ARGS:"
echo " -f: force run of tasks regardless of updates on the repository"
echo " -b BASEDIR: consider tasks from BASEDIR, not ~/.forgebuild"
}
# Logging is done with the LOG="debug|info|error" environment variable
if [ ! -z $LOG ] && [ "$LOG" != "" ]; then
case $LOG in
@ -74,31 +90,30 @@ run() {
p_name="$1"
i18n_task="$p_name" info run
# Run in background and redirect output to $p_name.log
(GITBUILDCONF="$CONFDIR" GITBUILDDIR="$BASEDIR" nohup $BASEDIR/$p_name $p_name > $BASEDIR/$p_name.log 2>&1) &
#(GITBUILDCONF="$CONFDIR" GITBUILDDIR="$BASEDIR" nohup $BASEDIR/$p_name $p_name > $BASEDIR/$p_name.log 2>&1) &
GITBUILDCONF="$CONFDIR" GITBUILDDIR="$BASEDIR" $BASEDIR/$p_name $p_name > $BASEDIR/$p_name.log 2>&1
}
# Overriden by -f/--force to force rebuild when no update is available
FORCE=0
# Check if we have host-specific config then LOCAL=0
# TODO: maybe we should just match for $BASEDIR/$HOSTNAME directly? Would allow to fallback to global config for multihost setup where one host is not configured.. but is that desired? Also enables to have more folders in $BASEDIR (maybe logs/)
LOCAL=1
for folder in $BASEDIR/*/; do
# Hidden folders (repos) are ignored by the pattern, so we can assume if a folder is not config/ then there are host-based configs here.
[[ "$(basename $folder)" != "config" ]] && LOCAL=0 && break
done
# Pass either local or global config to the task
[[ LOCAL = 1 ]] && CONFDIR=$BASEDIR/config || CONFDIR="$BASEDIR/$HOSTNAME"
export i18n_config="$CONFDIR"
info config
# Find targeted projects from args and extra arguments
PROJECTS=()
BASEDIR="$HOME/.forgebuild"
FOUND_BASEDIR=0
for arg in "$@"; do
if [[ "$arg" = "-f" ]] || [[ "$arg" = "--force" ]]; then
if [ $FOUND_BASEDIR -eq 1 ]; then
BASEDIR="$(readlink -m $arg)"
if [ ! -d $BASEDIR ]; then
echo "ERROR: Could not find task directory: $BASEDIR"
exit 1
fi
FOUND_BASEDIR=0
elif [[ "$arg" = "-f" ]] || [[ "$arg" = "--force" ]]; then
info force_flag
FORCE=1
elif [[ "$arg" = "-b" ]] || [[ "$arg" = "--basedir" ]]; then
FOUND_BASEDIR=1
# Maybe it's a task name and we find a corresponding source?
# TODO: Support source-less tasks https://tildegit.org/southerntofu/git-build.sh/issues/8
elif [ -f $BASEDIR/$arg.source ]; then
@ -124,6 +139,15 @@ for arg in "$@"; do
fi
done
# Still the default BASEDIR, if it doesn't exist we create it as a convenience
if [ ! -d $BASEDIR ]; then
echo "ERROR: No such basedir: $BASEDIR. Creating it and aborting"
exit 1
fi
# So scripts can know we're still running (for autoupdater)
echo "$BASHPID" > $BASEDIR/.LOCK
# If no project argument passed, default to all projects
if [[ ${#PROJECTS[*]} = 0 ]]; then
info no_task
@ -138,28 +162,34 @@ if [[ ${#PROJECTS[*]} = 0 ]]; then
fi
# Check if we have host-specific config or default to the config/ folder
[ -d $BASEDIR/$HOSTNAME ] && CONFDIR="$BASEDIR/$HOSTNAME" || CONFDIR=$BASEDIR/config
export i18n_config="$CONFDIR"
info config
# TODO: sourceless tasks
for p_name in ${PROJECTS[*]}; do
# For translations
export i18n_task="$p_name"
debug start_proc
# Check if task should run on host
if [[ $LOCAL = 0 ]]; then
if [ -f $BASEDIR/$p_name.host ]; then
for_host="$(cat $BASEDIR/$p_name.host)"
if [[ "$for_host" != "$HOSTNAME" ]]; then
# The task is specifically configured for a different host, ignore
export i18n_host="$for_host"
debug skipped
continue
fi
fi
# The task has a PROJECT.ignore file in host config, ignore
if [ -f $BASEDIR/$HOSTNAME/$p_name.ignore ]; then
info host_ignored
if [ -f $BASEDIR/$p_name.host ]; then
for_host="$(cat $BASEDIR/$p_name.host)"
if [[ "$for_host" != "$HOSTNAME" ]]; then
# The task is specifically configured for a different host, ignore
export i18n_host="$for_host"
debug skipped
continue
fi
fi
fi
# The task has a PROJECT.ignore file in host config, ignore
if [ -f $BASEDIR/$HOSTNAME/$p_name.ignore ]; then
info host_ignored
continue
fi
info process
# TODO: Should be able to switch branch after a repo was cloned
if [ -f $BASEDIR/$p_name.branch ]; then