From b3c816b428a6152d5fc6ecd87a5a4bed9eb142d8 Mon Sep 17 00:00:00 2001 From: southerntofu Date: Fri, 27 Nov 2020 18:40:39 +0100 Subject: [PATCH] Don't load translations for tests, don't attempt to load folders as tasks --- forgebuild.sh | 64 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/forgebuild.sh b/forgebuild.sh index 31d65fe..cdaf9f7 100755 --- a/forgebuild.sh +++ b/forgebuild.sh @@ -15,24 +15,38 @@ if [ ! -z $HOST ]; then HOSTNAME="$HOST" fi -# Extract two letters from $LANG -locale="${LANG:0:2}" +# Check whether we use translations, or use debug output +# If LANG is not special-value "NONE", then: +# - we load the requested language if it exists, fail otherwise +# - when no language is requested, default to something (TODO) +if [[ "$LANG" != "NONE" ]]; then + # Enable colors (escape codes) as echo option + COLORS=1 -# 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 + # Extract two letters from $LANG + locale="${LANG:0:2}" -# Initialize translations -[ -f $I18N_DIR/$locale.json ] && locale_strings="$(cat $I18N_DIR/$locale.json)" || locale_strings="$(cat $I18N_DIR/en.json)" + # 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_DIR/$locale.json ] && locale_strings="$(cat $I18N_DIR/$locale.json)" || locale_strings="$(cat $I18N_DIR/en.json)" + +fi # End translations initialization # Takes one argument, looks up translation trans() { + # If translations are disabled (for output testing), + # return the name of the requested key witout translating + [[ "$LANG" == "NONE" ]] && echo -n "$1" && exit 0 + # We want translation res="$(echo "$locale_strings" | jq ".$1")" if [[ "$res" = "" ]]; then echo "ERROR: Failed to translate $1" @@ -45,19 +59,22 @@ trans() { # We set custom logging functions so that later we can decorate stuff warn() { - [[ $INFO = 1 ]] && echo -e "\e[33m$(trans warning)\e[0m$(trans $1)" | envsubst > /dev/stderr + [[ $INFO != 1 ]] && return + [[ $COLORS = 1 ]] && echo -e "\e[33m$(trans warning)\e[0m$(trans $1)" | envsubst > /dev/stderr || echo "$(trans warning)$(trans $1)" | envsubst > /dev/stderr } info() { - [[ $INFO = 1 ]] && echo "[git-build] $(trans $1)" | envsubst + [[ $INFO != 1 ]] && return + echo "[git-build] $(trans $1)" | envsubst > /dev/stderr } error () { - echo -e "\e[31m$(trans error)\e[0m$(trans $1)" | envsubst > /dev/stderr + [[ $COLORS = 1 ]] && echo -e "\e[31m$(trans error)\e[0m$(trans $1)" | envsubst > /dev/stderr || echo "$(trans error)$(trans $1)" | envsubst > /dev/stderr } debug () { - [[ $DEBUG = 1 ]] && echo -e "\e[36m$(trans debug)\e[0m$(trans $1)" | envsubst + [[ $DEBUG != 1 ]] && return + [[ $COLORS = 1 ]] && echo -e "\e[36m$(trans debug)\e[0m$(trans $1)" | envsubst > /dev/stderr || echo "$(trans debug)$(trans $1)" | envsubst > /dev/stderr } help () { @@ -241,7 +258,8 @@ for arg in "$@"; do if [ $FOUND_BASEDIR -eq 1 ]; then BASEDIR="$(readlink -m $arg)" if [ ! -d $BASEDIR ]; then - echo "ERROR: Could not find task directory: $BASEDIR" + i18n_basedir="$BASEDIR" + error "missing_basedir" exit 1 fi FOUND_BASEDIR=0 @@ -259,7 +277,9 @@ for arg in "$@"; do PROJECTS+=("$arg") # Maybe it's a repo URL and we find a corresponding task? # Used for interop (for example forgehook-notify) - elif matches="$(grep --files-with-matches --word-regexp "$arg" $BASEDIR/*.source)"; then + # --no-messages does not fail when the glob pattern doesn't match + # --regexp (compared to raw pattern) avoids leaking a wrong forgebuild argument to grep + elif matches="$(grep --no-messages --files-with-matches --word-regexp --regexp "$arg" $BASEDIR/*.source)"; then # Iterate over the files found to extract the task name while IFS= read -r file; do task_name="$(basename "$file" .source)" @@ -291,8 +311,9 @@ echo "$BASHPID" > $BASEDIR/.LOCK if [[ ${#PROJECTS[*]} = 0 ]]; then info no_task for file in $BASEDIR/*; do - # Skip non-executable files + # Skip non-executable files, and directories (which can be +x) [ ! -x $file ] && continue + [ ! -f $file ] && continue # Extract the project name from path task="$(basename $file .source)" export i18n_task="$task" @@ -344,7 +365,8 @@ for p_name in ${PROJECTS[*]}; do source="$(cat $BASEDIR/$p_name.source)" export i18n_source="$source" info clone - if ! clone "$dvcs" "$source" "$p_dir"; then + # Suppress clone stderr, TODO: maybe save in some log file? + if ! clone "$dvcs" "$source" "$p_dir" 2> /dev/null; then error clone_failed exit 1 fi