build.sh/git-build.sh

54 lines
1.6 KiB
Bash
Executable File

#/bin/bash
# This script was indeed self-updated!
# 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"
run() {
p_name="$1"
echo "[$p_name] RUN"
# Run in background and redirect output to $p_name.log
(GITBUILDDIR="$BASEDIR" nohup $BASEDIR/$p_name $p_name > $BASEDIR/$p_name.log 2> $BASEDIR/$p_name.err) &
}
# So scripts can know we're still running (for autoupdater)
touch $BASEDIR/.LOCK
for project in $BASEDIR/*.source; do
p_name="$(basename $project .source)"
echo "[$p_name] START"
[ -f $BASEDIR/$p_name.branch ] && p_branch="$(cat $BASEDIR/$p_name.branch)" || p_branch="master"
p_dir="$BASEDIR/.$p_name"
if [ ! -d $p_dir ]; then
source="$(cat $BASEDIR/$p_name.source)"
echo "[$p_name] CLONE $source"
# Don't forget the git submodules!
git clone --recursive "$source" "$p_dir"
[[ $? != 0 ]] && echo "[$p_name] CLONE FAILED" && exit 1
cd $p_dir
if [[ "$p_branch" != "master" ]]; then
echo "[$p_name] CHECKOUT BRANCH $p_branch"
git checkout "$p_branch"
[[ $? != 0 ]] && echo "[$p_name] CHECKOUT FAILED" && exit 1
fi
run $p_name
fi
cd "$p_dir"
# Refresh remote before comparing with local
git fetch --quiet origin
git diff --quiet remotes/origin/$p_branch
if [[ $? != 0 ]]; then
echo "[$p_name] PULL"
# Update all submodules, for now only when the main repo changed (TODO)
git pull --quiet --recurse-submodules
[[ $? != 0 ]] && echo "[$p_name] PULL FAILED" && exit 1
run $p_name
fi
done
rm $BASEDIR/.LOCK