ci: Use python to extract JS snippets from style guide

The job that lints code snippets in the style guide only runs
when either the documentation or the linting rules are changed.

Because of that it went unnoticed that the awk tool it uses to
extract the snippets is no longer available since the CI image
was rebased to F42.

Given that awk is awkward anyway, use the opportunity to rewrite the
snippet extraction in python instead of adding awk back to the image.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3694>
This commit is contained in:
Florian Müllner 2025-04-13 03:07:16 +02:00 committed by Bruce Leidl
parent 567eb6e0d0
commit a7410d82ac

View File

@ -6,36 +6,36 @@ trap "rm -rf $OUTDIR" EXIT
# Turn ```javascript``` code snippets in the # Turn ```javascript``` code snippets in the
# style guide into .js files in $OUTDIR # style guide into .js files in $OUTDIR
awk --assign dir=$OUTDIR -- ' cat <<'EOF' | python3 - docs/js-coding-style.md $OUTDIR
BEGIN { import sys
do_print = 0; import re
cur = 0;
}
# end of code snippet def extract_js_snippets(input_file, output_dir):
/```$/ { with open(input_file, 'r') as file:
do_print = 0; content = file.read()
cur++;
}
do_print { # Find all JavaScript code blocks using regex
# remove one level of indent js_blocks = re.findall(r'```javascript\n(.*?)\n?```', content, flags=re.DOTALL)
sub(" ","")
# the following are class snippets, turn them for i, (match) in enumerate(js_blocks):
js_code = match
# Remove one level of indent
js_code = re.sub(r'^ {4}', '', js_code, flags=re.MULTILINE)
# The following are class snippets, turn them
# into functions to not confuse eslint # into functions to not confuse eslint
sub("moveActor","function moveActor") js_code = re.sub(r'^moveActor', 'function moveActor', js_code)
sub("desaturateActor","function desaturateActor") js_code = re.sub(r'^desaturateActor', 'function desaturateActor', js_code)
# finally, append to the currently generated .js file # Finally, create a .js file in the output directory
print >> dir "/" cur ".js"; output_filename = f'{output_dir}/{i}.js'
} with open(output_filename, 'w') as out_file:
out_file.write(f'{js_code}\n')
# start of code snippet input_file, output_dir = sys.argv[1:]
/```javascript$/ { extract_js_snippets(input_file, output_dir)
do_print = 1; EOF
}
' docs/js-coding-style.md
eslint \ eslint \
--rule 'no-undef: off' \ --rule 'no-undef: off' \