tools: remove inline scripting from make-luacheck-files

- the awk script was moved to its own file (deps-to-luacheck.awk) with
  minor changes, including added commentary, slightly more rigorous error
  checking, and using a different separator model to avoid a second
  stage to remove trailing commas
- SED_MODCONF2DEPS was moved to a containing function, and commented
- the awk command itself is now easier on the eyes
This commit is contained in:
E 2021-05-03 13:33:19 -04:00
parent 02a8496b5f
commit b1fbbfdd35
2 changed files with 65 additions and 46 deletions

View File

@ -0,0 +1,41 @@
BEGIN {
if (!DIR || !MOD) {
printf("error: DIR and MOD must be set.\n");
_panic=1;
exit 1;
}
printf("files[\"" DIR "\"] = { globals = { \"" MOD "\" }");
if (!EMPTY) {
printf(", read_globals = { ");
}
};
# Without a pattern, this will match on empty lines
# and mess up the entry. We clean up the extra commas
# later in the script.
/.+/{
# Get rid of the optional (?) mark from
# depends.txt entries.
sub("?$", "");
# Some files have CRLF, so get rid of those
# too.
sub("\xd", "");
SEP=", ";
if (NR == 1) {
SEP="";
}
printf(SEP "\"" $1 "\"");
};
END {
if (!_panic) {
if (!EMPTY) {
printf(" }");
}
printf(" }\n");
}
};

View File

@ -2,6 +2,7 @@
# #
# make-luacheck-files.sh # make-luacheck-files.sh
# v1 - E - Initial version # v1 - E - Initial version
# v2 - E - Remove (most) inline scripts
set -ue set -ue
@ -10,53 +11,27 @@ if [ ! -d "mods" ] || [ ! -r ".luacheck.head" ]; then
exit 1 exit 1
fi fi
# We need these to be explicitly NOT expanded. HEADER='
# shellcheck disable=2016
{
# This creates a luacheckrc-compatible line from a mod (supplied on
# the command line with -v MOD="mod_name"), a directory (-v DIR="..."),
# and a list of dependencies (via STDIN). The `read_globals` section is
# skipped if there are no dependencies (-v EMPTY=1).
AWK_DEPS2LCHECK='
BEGIN {
printf("files[\"" DIR "\"] = { globals = { \"" MOD "\" }");
if (!EMPTY) {
printf(", read_globals = { ");
}
};
# Without a pattern, this will match on empty lines
# and mess up the entry. We clean up the extra commas
# later in the script.
/.+/{
sub("?$", "");
sub("\xd", "");
printf("\"" $1 "\", ");
};
END {
if (!EMPTY) {
printf("}");
}
printf(" }\n");
};'
# This takes any line that contains 'depends' and an equal sign
# and spits out each (comma-separated) entry on its own line.
SED_MODCONF2DEPS='
/depends *=/{
s/.*depends *=//;
s/, ?/\n/g;
p;
}'
HEADER='
------------------------------------------------------------------- -------------------------------------------------------------------
-- THIS FILE WAS AUTOGENERATED BY tools/make-luacheck-files.sh ! -- -- THIS FILE WAS AUTOGENERATED BY tools/make-luacheck-files.sh ! --
-- DO NOT EDIT BY HAND. YOUR CHANGES WILL BE LOST NEXT RUN! -- -- DO NOT EDIT BY HAND. YOUR CHANGES WILL BE LOST NEXT RUN! --
------------------------------------------------------------------- -------------------------------------------------------------------
' '
# This takes any line that contains 'depends' and an equal sign
# and spits out each (comma-separated) entry on its own line.
modconf2deps() {
sed -n -r -e '
# Match any lines like "depends = asdfada"
/depends *=/{
# Get rid of the "depends =" part
s/.*depends *=//;
# Convert commas to newlines
s/, */\n/g;
# Record the output
p;
}'
} }
find mods -type d -print | while read -r DIR; do find mods -type d -print | while read -r DIR; do
@ -69,7 +44,7 @@ find mods -type d -print | while read -r DIR; do
printf "found: %s (%s)\n" "$DIR" "mod.conf" 1>&2 printf "found: %s (%s)\n" "$DIR" "mod.conf" 1>&2
# mod.conf needs some more help to get usable # mod.conf needs some more help to get usable
# data out of it. # data out of it.
DEPS="$(sed -n -r -e "$SED_MODCONF2DEPS" "$DIR/mod.conf")" DEPS="$(modconf2deps < "$DIR/mod.conf")"
else else
# Empty dir, or not a mod dir. # Empty dir, or not a mod dir.
continue continue
@ -84,10 +59,13 @@ find mods -type d -print | while read -r DIR; do
# for the mod name. # for the mod name.
MOD="${DIR##*/}" MOD="${DIR##*/}"
# Now we run through it through the formatter, # Now we run through it through the formatter
# and remove any rogue trailing commas. awk \
{ awk -v DIR="$DIR" -v EMPTY="$EMPTY" -v MOD="$MOD" -- "$AWK_DEPS2LCHECK" | sed -e "s/, }/ }/"; } <<-EOF -v DIR="$DIR" \
$DEPS -v EMPTY="$EMPTY" \
-v MOD="$MOD" \
-f tools/deps-to-luacheck.awk <<-EOF
$DEPS
EOF EOF
done > .luacheck.files done > .luacheck.files