Replace sponge with standard file redirect and mv

This commit is contained in:
Alexis Hildebrandt 2022-07-27 11:29:05 +02:00
parent 9e32ea5e48
commit ef54800c98

View File

@ -1,9 +1,7 @@
#!/bin/bash #!/bin/bash
# Format and update translations from a source file (en_US.json by default) # Format and update translations from a source file (en_US.json by default).
# # Requires jq (https://stedolan.github.io/jq/)
# Requires jq (https://stedolan.github.io/jq/) and sponge
# (https://www.putorius.net/linux-sponge-soak-up-standard-input-and-write-to-a-file.html)
set -euo pipefail set -euo pipefail
@ -13,28 +11,29 @@ PREFIX=_TSTR_
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
LOCALES_DIR="${SCRIPT_DIR}/../src/musikcube/data/locales" LOCALES_DIR="${SCRIPT_DIR}/../src/musikcube/data/locales"
TEMPFILE=$(mktemp -p ${TMPDIR:-/tmp} $(basename "${0}").XXXXX) TEMPFILE=$(mktemp -p ${TMPDIR:-/tmp} $(basename "${0}").XXXXX)
PREFIXDFILE=$(mktemp -p ${TMPDIR:-/tmp} $(basename "${0}").XXXXX)
trap cleanup 0 1 2 3 5 trap cleanup 0 1 2 3 5
cleanup() { cleanup() {
rm -f "${TEMPFILE}" rm -f "${PREFIXDFILE}" "${TEMPFILE}"
} }
# Sort $SOURCE translations file alphabetically by keys. # Sort $SOURCE translations file alphabetically by keys.
jq -S . "${LOCALES_DIR}/${SOURCE}" | sponge "${LOCALES_DIR}/${SOURCE}" jq -S . "${LOCALES_DIR}/${SOURCE}" > "${TEMPFILE}"
mv "${TEMPFILE}" "${LOCALES_DIR}/${SOURCE}"
# Generate a temporary translations file prefixing all values with ${PREFIX} # Generate a temporary translations file prefixing all values with ${PREFIX}
# to indicate missing translations. # to indicate missing translations.
awk -F'": ' -v prefix="${PREFIX}" \ awk -F'": ' -v prefix="${PREFIX}" \
'/: "/{sub("\"", "\"" prefix, $2);print $1 "\": " $2;next} {print}' \ '/: "/{sub("\"", "\"" prefix, $2);print $1 "\": " $2;next} {print}' \
"${LOCALES_DIR}/${SOURCE}" \ "${LOCALES_DIR}/${SOURCE}" \
> "${TEMPFILE}" \ > "${PREFIXDFILE}" \
# awk > $TEMPFILE # awk > $PREFIXDFILE
# Merge language specific translations fle with temporary translations file, # Merge language specific translations fle with temporary translations file,
# adding any missing translations from ${SOURCE}, prefixed with ${PREFIX}. # adding any missing translations from ${SOURCE}, prefixed with ${PREFIX}.
for JSON in $(git ls-files "${LOCALES_DIR}" | grep -v "${SOURCE}"); do for JSON in $(git ls-files "${LOCALES_DIR}" | grep -v "${SOURCE}"); do
jq -Ss '.[0] * .[1]' "${TEMPFILE}" "${JSON}" \ jq -Ss '.[0] * .[1]' "${PREFIXDFILE}" "${JSON}" > "${TEMPFILE}"
| sponge "${JSON}" \ mv "${TEMPFILE}" "${JSON}"
# jq | sponge
done done