TRANSLATIONS!!!!! (requires jq)

This commit is contained in:
southerntofu 2020-04-29 21:23:29 +02:00
parent 152f28179c
commit 18f4a91e2c
3 changed files with 140 additions and 34 deletions

View File

@ -12,21 +12,39 @@ BASEDIR="$HOME/.git-build"
INFO=1
DEBUG=0
# Extract two letters from $LANG
locale="${LANG:0:2}"
# Initialize translations
[ -f i18n/$locale.json ] && locale_strings="$(cat i18n/$locale.json)" || locale_strings="$(cat i18n/en.json)"
# Takes one argument, looks up translation
trans() {
res="$(echo "$locale_strings" | jq ".$1")"
if [[ "$res" = "" ]]; then
echo "ERROR: Failed to translate $1"
exit 1
fi
res=${res#"\""}
res=${res%"\""}
echo -n "$res"
}
# We set custom logging functions so that later we can decorate stuff
warn() {
[[ $INFO = 1 ]] && echo -e "\e[33mWARNING:\e[0m $@" > /dev/stderr
[[ $INFO = 1 ]] && echo -e "\e[33m$(trans warning)\e[0m$(trans $1)" | envsubst > /dev/stderr
}
info() {
[[ $INFO = 1 ]] && echo "[git-build] $@"
[[ $INFO = 1 ]] && echo "[git-build] $(trans $1)" | envsubst
}
error () {
echo -e "\e[31mERROR:\e[0m $@" > /dev/stderr
echo -e "\e[31m$(trans error)\e[0m$(trans $1)" | envsubst > /dev/stderr
}
debug () {
[[ $DEBUG = 1 ]] && echo -e "\e[36mDEBUG:\e[0m $@"
[[ $DEBUG = 1 ]] && echo -e "\e[36m$(trans debug)\e[0m$(trans $1)" | envsubst
}
# Logging is done with the LOG="debug|info|error" environment variable
@ -42,14 +60,14 @@ if [ ! -z $LOG ] && [ "$LOG" != "" ]; then
# Default settings
;;
*)
warn "\$LOG is \"$LOG\" which is not understood to be a logging level."
warn loglevel_bad
;;
esac
fi
run() {
p_name="$1"
info "$p_name: RUN"
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> $BASEDIR/$p_name.err) &
}
@ -70,18 +88,21 @@ for folder in $BASEDIR/*/; do
done
# Pass either local or global config to the task
[[ LOCAL = 1 ]] && CONFDIR=$BASEDIR/config || CONFDIR="$BASEDIR/$HOSTNAME" && info "Using config: $CONFDIR"
[[ LOCAL = 1 ]] && CONFDIR=$BASEDIR/config || CONFDIR="$BASEDIR/$HOSTNAME"
export i18n_config="$CONFDIR"
info config
# Find targeted projects from args and extra arguments
PROJECTS=()
for arg in "$@"; do
if [[ "$arg" = "-f" ]] || [[ "$arg" = "--force" ]]; then
info "Running task(s) regardless of updates (--force)"
info force_flag
FORCE=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
debug "Found task $arg"
export i18n_task="$arg"
debug found_task
PROJECTS+=("$arg")
# Maybe it's a repo URL and we find a corresponding task?
elif matches="$(grep --files-with-matches --word-regexp "$arg" $BASEDIR/*.source)"; then
@ -90,24 +111,27 @@ for arg in "$@"; do
task_name="$(basename "$file" .source)"
# Make sure we don't have a .source file without name lying around, just in case
if [[ "$task_name" != "" ]]; then
debug "Found task $arg from URL $arg"
export i18n_task="$task_name" i18n_source="$arg"
debug found_url
PROJECT+=("$task_name")
fi
done
else
error "Unknown argument/task '$arg' (no such task or source)"
export i18n_arg="$arg"
error unknown_arg
exit 1
fi
done
# If no project argument passed, default to all projects
if [[ ${#PROJECTS[*]} = 0 ]]; then
info "No project was specified. Trying all projects"
info no_task
# TODO: sourceless tasks
for file in $BASEDIR/*.source; do
# Extract the project name from path
task="$(basename $file .source)"
debug "Found task $task"
export i18n_task="$task"
debug found_task
PROJECTS+=("$task")
done
fi
@ -115,60 +139,78 @@ fi
# TODO: sourceless tasks
for p_name in ${PROJECTS[*]}; do
debug "Start processing task $p_name"
# 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 ] && [[ "$(cat $BASEDIR/$p_name.host)" != "$HOSTNAME" ]]; then
# The task is specifically configured for a different host, ignore
debug "$p_name: SKIPPED for current host"
continue
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
[ -f $BASEDIR/$HOSTNAME/$p_name.ignore ] && info "$p_name: IGNORED for current host ($HOSTNAME)" && continue
if [ -f $BASEDIR/$HOSTNAME/$p_name.ignore ]; then
info host_ignored
continue
fi
fi
info "$p_name: PROCESS"
info process
# TODO: Should be able to switch branch after a repo was cloned
if [ -f $BASEDIR/$p_name.branch ]; then
p_branch="$(cat $BASEDIR/$p_name.branch)"
info "$p_name: Using branch $p_branch"
export i18n_branch="$p_branch"
info to_branch
else
p_branch="master"
debug "$p_name: Defaulting to branch master"
export i18n_branch="$p_branch"
debug default_branch
fi
p_dir="$BASEDIR/.$p_name"
if [ ! -d $p_dir ]; then
source="$(cat $BASEDIR/$p_name.source)"
info "$p_name: CLONE $source"
export i18n_source="$source"
info clone
# Don't forget the git submodules!
git clone --recursive "$source" "$p_dir"
[[ $? != 0 ]] && error "$p_name: CLONE failed" && exit 1
if ! git clone --recursive "$source" "$p_dir"; then
error clone_failed
exit 1
fi
cd $p_dir
if [[ "$p_branch" != "master" ]]; then
debug "$p_name: Checking out branch $p_branch"
git checkout "$p_branch"
[[ $? != 0 ]] && error "$p_name: CHECKOUT failed" && exit 1
debug checkout
if ! git checkout "$p_branch"; then
error checkout_failed
exit 1
fi
fi
run $p_name
# Skip to the next task!
continue
fi
debug "$p_name: Task source already downloaded, checking for updates"
debug already_cloned
cd "$p_dir"
# Refresh remote before comparing with local
git fetch --quiet origin
git diff --quiet remotes/origin/$p_branch
if [[ $? != 0 ]]; then
info "$p_name: PULL"
info 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
if ! git pull --quiet --recurse-submodules; then
error pull_failed
exit 1
fi
run $p_name
# If no update was found, we can still force rebuild with -f/--force
elif [[ $FORCE = 1 ]]; then
debug "$p_name: Forcing run despite no update found"
debug forcing
run $p_name
else
debug "$p_name: No updates found"
debug no_update
fi
done

32
i18n/en.json Normal file
View File

@ -0,0 +1,32 @@
{
"warning": "WARNING: ",
"error": "ERROR: ",
"debug": "DEBUG: ",
"unknown_arg": "Unknown argument/task '$i18n_arg' (no such task or source)",
"clone_failed": "$i18n_task: CLONE failed for $i18n_source",
"checkout_failed": "$i18n_task: CHECKOUT failed on branch $i18n_branch",
"pull_failed": "$i18n_task: PULL failed",
"loglevel_bad": "LOG level is $LOG which is not understood to be a loglevel (debug, info or error)",
"host_ignored": "$i18n_task: IGNORED for current host ($HOSTNAME)",
"process": "$i18n_task: PROCESS",
"to_branch": "$i18n_task: Using branch $i18n_branch",
"clone":"$i18n_task: CLONE $source",
"pull": "$i18n_task: PULL",
"run": "$i18n_task: RUN",
"force_flag": "Running task(s) regardless of updates (--force)",
"config": "Using config: $i18n_config",
"no_task": "No task was specified. Triggering all tasks",
"default_branch": "$i18n_task: Defaulting to branch master",
"checkout": "$i18n_task: Checking out branch $i18n_branch",
"already_cloned": "$i18n_task: Task source already downloaded, checking for updates",
"forcing": "$i18n_task: Forcing run despite no update found",
"no_update": "$i18n_task: No updates found",
"found_task": "Found task $i18n_task",
"found_url": "Found task $i18n_task from URL $i18n_source",
"skipped": "$i18n_task: SKIPPED for current host (task configured for $i18n_host",
"start_proc": "$i18n_task: Start processing"
}

32
i18n/fr.json Normal file
View File

@ -0,0 +1,32 @@
{
"warning": "ATTENTION: ",
"error": "ERREUR: ",
"debug": "DÉBOGAGE: ",
"unknown_arg": "Argument/tache inconnue '$i18n_arg' (aucune tache ou source correspondante)",
"clone_failed": "$i18n_task: COPIE de $i18n_source échouée",
"checkout_failed": "$i18n_task: CHECKOUT échoué pour la branche $i18n_branch",
"pull_failed": "$i18n_task: MISE À JOUR échouée",
"loglevel_bad": "LOG est $LOG qui n'est pas un niveau de log valide (debug, info ou error)",
"host_ignored": "$i18n_task: IGNORÉE pour cette machine ($HOSTNAME)",
"process": "$i18n_task: TRAITEMENT",
"to_branch": "$i18n_task: Sélection de la branche $i18n_branch",
"clone":"$i18n_task: COPIE de $source",
"pull": "$i18n_task: MISE À JOUR",
"run": "$i18n_task: ÉXÉCUTION",
"force_flag": "Les tâches seront exécutées qu'importent les mises à jour (--force)",
"config": "Configuration: $i18n_config",
"no_task": "Aucune tache sélectionnée. Déclenchement de toutes les taches",
"default_branch": "$18n_task: Sans branche spécifiée, sélection de la branche master",
"checkout": "$i18n_task: Activation de la branche $i18n_branch",
"already_cloned": "$i18n_task: La source est déjà téléchargée, vérification des mises à jour",
"forcing": "$i18n_task: Exécution forcée malgré l'absence de mises à jour",
"no_update": "$i18n_task: Aucune mise à jour trouvée",
"found_task": "Tache trouvée $i18n_task",
"found_url": "Tache trouvée $i18n_task depuis l'URL $i18n_source",
"skipped": "$i18n_task: PASSÉE pour cette machine (tache configurée pour $i18n_host)",
"start_proc": "$i18n_task: Début du traitement"
}