From 5231b9515853c526609cee1f615ee9f83f25582a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20R=C3=BChle?= <j-r@online.de> Date: Mon, 14 Oct 2024 01:09:08 +0200 Subject: [PATCH] Make automatic translation generation script slightly more robust Setup a temporary directory to better control the complete configuration and hopefully prevent unwanted characters in directory names. Additionally support minetest and minetestserver executables installed to PATH. Also support being run from anywhere and not only from tool directory itself. Use --quiet parameter instead of directing all output to /dev/null to better support debugging. --- .../generate_translation_strings/generate.sh | 101 +++++++++++++----- tools/generate_translation_strings/world.conf | 1 + .../world/world.mt | 5 + .../init.lua | 0 .../mod.conf | 0 5 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 tools/generate_translation_strings/world.conf create mode 100644 tools/generate_translation_strings/world/world.mt rename tools/generate_translation_strings/{ => world/worldmods}/mcla_generate_translation_strings/init.lua (100%) rename tools/generate_translation_strings/{ => world/worldmods}/mcla_generate_translation_strings/mod.conf (100%) diff --git a/tools/generate_translation_strings/generate.sh b/tools/generate_translation_strings/generate.sh index 95e00c278..d98d346b7 100755 --- a/tools/generate_translation_strings/generate.sh +++ b/tools/generate_translation_strings/generate.sh @@ -1,45 +1,92 @@ #!/bin/sh -world=mcla_generate_translations -mtdir=../../../.. -wdir=$mtdir/worlds/$world -if [ ! -x $1 ]; then - echo mtt_updater not found at $1 pass the python script as first argument! + +# Automatically run the mineclonia version that contains this script in a +# temporary world with the translation dumper mod and copy the resulting +# translation string files into the corresponding source mods. +# +# This script uses ``realpath`` to support being run from anywhere and to +# slightly beautify output. When run from its directory it should also work +# with ``realpath`` replaced by ``echo``. It also uses ``mktemp`` and expects +# symlinks to be supported by the file system of the temporary directory. We +# also assume that the directory name chosen by ``mktemp`` contains no space +# characters. + +env >/dev/null 2>&1 which "$1" || (echo mod_translation_updater not found at $1, pass the python script as first argument!; false) || exit 1 + +# get directory of this script (with trailing slash) +mypath=$(realpath "$0") +mydir=${mypath%generate.sh} + +# toplevel mineclonia directory +mcladir=$(realpath "${mydir}../..") + +minetest= +parameters= + +# search ``minetest`` or ``minetestserver`` executable +# +# First check whether we are inside the ``games`` directory of a minetest +# source build directory, alternatively check for minetest installed in +# ``PATH``. If the executable is ``minetest``, add ``--server`` parameter. +for dir in "${mydir}../../../../bin/" ""; do + for program in minetest minetestserver; do + command=$(command -v "${dir}${program}") + if [ -x "${command}" ]; then + minetest=$(realpath "${command}") + if [ "${program}" = "minetest" ]; then + parameters="${parameters} --server" + fi + break 2 + fi + done +done + +if [ -z "${minetest}" ]; then + echo minetest not found, either move mineclonia to the games directory of a run_in_place build of minetest, or make minetest available in PATH exit 1 fi -if [ ! -x $mtdir/bin/minetest ]; then - echo This script needs to be run from the tools/generate_translation_strings directory within a run_in_place minetest +# prepare temporary directory +tmpdir=$(mktemp -d) +if [ ! -d ${tmpdir} ]; then + echo could not create temporary directory exit 1 fi -if [ -d $mtdir/worlds/$world ]; then - echo Temp world $world already exists. Remove or rename it to run this script. - exit 1 -fi +# create symlink for the special gameid used by the process to make sure that +# no other installation of mineclonia in the shared or user path is +# accidentally used instead of the one that contains this script +gameid=$(grep '^gameid *= *' "${mydir}world/world.mt" | cut -d = -f 2 | grep -o "[^ ][^ ]*") +ln -s "${mcladir}" "${tmpdir}/${gameid}" -mkdir -p $wdir/worldmods -cp -r mcla_generate_translation_strings $wdir/worldmods -echo "gameid = mineclonia" > $wdir/world.mt -echo -n Running minetest to extract complex translation strings... -$mtdir/bin/minetest --server --gameid mineclonia --worldname $world > /dev/null 2>&1 -if [ -f $wdir/mcla_translate/mod_dirs.txt ]; then - echo done -else +# initialize world in temporary directory +cp -r "${mydir}world.conf" "${mydir}/world" "${tmpdir}" + +echo Running ${minetest} to extract complex translation strings in ${tmpdir}... +MINETEST_GAME_PATH="${tmpdir}" "${minetest}" ${parameters} --config "${tmpdir}/world.conf" --world "${tmpdir}/world" --logfile "${tmpdir}/debug.txt" --quiet + +# the following only works when ``tmpdir`` contains no space characters +# (which would be written to mod_dirs.txt, too) +if [ ! -f ${tmpdir}/world/mcla_translate/mod_dirs.txt ]; then echo Running minetest did not produce any translation files. - echo The temporary world directory has been preserved so the situation can be investigated: - echo You can delete it by running rm -rf $wdir + echo The temporary directory has been preserved so the situation can be investigated: + echo You can delete it by running rm -rf \'${tmpdir}\' exit 1 fi -cat $wdir/mcla_translate/mod_dirs.txt |while read f; do + +cat ${tmpdir}/world/mcla_translate/mod_dirs.txt |while read f; do mod=$(echo $f|cut -d " " -f1) file=$(echo $f|cut -d " " -f2) - echo cp $wdir/mcla_translate/$file $mod + cp $tmpdir/world/mcla_translate/$file $mod done -#$1 -r ../../mods -cat $wdir/mcla_translate/mod_dirs.txt |while read f; do + +echo Running $1 on ${mcladir}/mods +"$1" -r "${mcladir}/mods" + +cat ${tmpdir}/world/mcla_translate/mod_dirs.txt |while read f; do mod=$(echo $f|cut -d " " -f1) file=$(echo $f|cut -d " " -f2) - echo rm $mod/$file + rm $mod/$file done -rm -rf $wdir + echo Mod translation strings have been extracted and updated. You will now want to create a new commit containing the new translation strings diff --git a/tools/generate_translation_strings/world.conf b/tools/generate_translation_strings/world.conf new file mode 100644 index 000000000..b6751396b --- /dev/null +++ b/tools/generate_translation_strings/world.conf @@ -0,0 +1 @@ +# use empty user config while generating translation strings diff --git a/tools/generate_translation_strings/world/world.mt b/tools/generate_translation_strings/world/world.mt new file mode 100644 index 000000000..b854d0c85 --- /dev/null +++ b/tools/generate_translation_strings/world/world.mt @@ -0,0 +1,5 @@ +mod_storage_backend = dummy +auth_backend = sqlite3 +player_backend = dummy +backend = dummy +gameid = mineclonia_generate_translation_strings diff --git a/tools/generate_translation_strings/mcla_generate_translation_strings/init.lua b/tools/generate_translation_strings/world/worldmods/mcla_generate_translation_strings/init.lua similarity index 100% rename from tools/generate_translation_strings/mcla_generate_translation_strings/init.lua rename to tools/generate_translation_strings/world/worldmods/mcla_generate_translation_strings/init.lua diff --git a/tools/generate_translation_strings/mcla_generate_translation_strings/mod.conf b/tools/generate_translation_strings/world/worldmods/mcla_generate_translation_strings/mod.conf similarity index 100% rename from tools/generate_translation_strings/mcla_generate_translation_strings/mod.conf rename to tools/generate_translation_strings/world/worldmods/mcla_generate_translation_strings/mod.conf