Meta: Add a PNG size check to CI and pre-commit checks

This uses optipng to check how much size can be reduced on PNG files. If
that's more than 2 KiB for at least one file, the check fails. As with
other checks, it doesn't run if optipng is not installed.
This commit is contained in:
kleines Filmröllchen 2022-06-18 13:17:52 +02:00 committed by Ali Mohammad Pur
parent bae330d559
commit 2c6e3ea2e9
Notes: sideshowbarker 2024-07-17 10:05:30 +09:00
2 changed files with 43 additions and 0 deletions

42
Meta/check-png-sizes.sh Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -eo pipefail
# How many bytes optipng has to be able to strip out of the file for the optimization to be worth it. The default is 1 KiB.
: "${MINIMUM_OPTIMIZATION_BYTES:=1024}"
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "${script_path}/.."
if ! command -v optipng >/dev/null ; then
echo 'optipng is not installed, skipping png size check.'
echo 'Please install optipng for your system to run this check.'
exit 0
fi
files=()
for file in "$@"; do
if [[ "${file}" == *".png" ]]; then
files+=("${file}")
fi
done
if (( ${#files[@]} )); then
# We need to allow optipng to write output so we can check what it actually did. We use a dummy file that's discarded afterwards.
optimizations=$( printf '%s\0' "${files[@]}" |\
xargs -0 -n1 optipng -strip all -out dummy-optipng-output.png -clobber 2>&1 |\
grep -i -e 'Output IDAT size =' |\
sed -E 's/Output IDAT size = [0-9]+ byte(s?) \(([0-9]+) byte(s?) decrease\)/\2/g;s/Output IDAT size = [0-9]+ byte(s?) \(no change\)/0/g' |\
awk "{ if (\$1 >= $MINIMUM_OPTIMIZATION_BYTES) { S+=\$1 } } END { print S }")
rm -f dummy-optipng-output.png dummy-optipng-output.png.bak
optimizations="${optimizations:-0}"
if [[ "$optimizations" -ne 0 ]] ; then
echo "There are non-optimized PNG images in Base/. It is possible to reduce file sizes by at least $optimizations byte(s)."
# shellcheck disable=SC2016 # we're not trying to expand expressions here
echo 'Please run optipng with `-strip all` on modified PNG images and try again.'
exit 1
fi
else
echo 'No PNG images to check.'
fi

View file

@ -24,6 +24,7 @@ for cmd in \
Meta/check-debug-flags.sh \
Meta/check-markdown.sh \
Meta/check-newlines-at-eof.py \
Meta/check-png-sizes.sh \
Meta/check-style.py \
Meta/lint-executable-resources.sh \
Meta/lint-keymaps.py \