Files
site/tools/shots.sh
T

48 lines
2.2 KiB
Bash

#!/bin/sh
# tools/shots.sh: shots/desktop.png, the first screen at 1440x900, and the app icons (icons/*.png from icons/icon.svg),
# rendered from this folder by headless Chrome.
# Needs python3 (for a local server) and Chrome or Chromium: set CHROME=/path/to/chrome if it isn't found, and
# CHROME_FLAGS=--no-sandbox when running as root.
set -eu
cd "$(dirname "$0")/.."
CHROME=${CHROME:-}
for c in "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" "$(command -v chromium || true)" \
"$(command -v chromium-browser || true)" "$(command -v google-chrome || true)" "$(command -v google-chrome-stable || true)"; do
if [ -z "$CHROME" ] && [ -n "$c" ] && [ -x "$c" ]; then CHROME=$c; fi
done
[ -n "$CHROME" ] || { echo "no Chrome or Chromium found; set CHROME" >&2; exit 1; }
PORT=${PORT:-8794}
python3 -m http.server "$PORT" --bind 127.0.0.1 >/dev/null 2>&1 </dev/null &
SRV=$!
trap 'kill $SRV 2>/dev/null || true' EXIT INT TERM
n=0
until curl -fs -o /dev/null --max-time 2 "http://127.0.0.1:$PORT/"; do
n=$((n + 1)); [ "$n" -lt 40 ] || { echo "the local server on port $PORT never answered" >&2; exit 1; }; sleep 0.5
done
# shoot <url> <WxH> <out>: one headless screenshot, waited for
shoot() {
prof=$(mktemp -d)
rm -f "$3"
# shellcheck disable=SC2086
"$CHROME" --headless=new --hide-scrollbars --no-first-run --no-default-browser-check --disable-background-networking \
--disable-component-update --user-data-dir="$prof" --virtual-time-budget=6000 --window-size="$2" \
--screenshot="$3" ${CHROME_FLAGS:-} "$1" >"$prof.log" 2>&1 &
pid=$!
n=0
while kill -0 "$pid" 2>/dev/null && [ ! -s "$3" ]; do n=$((n + 1)); [ "$n" -lt 120 ] || break; sleep 0.5; done
sleep 1
kill "$pid" 2>/dev/null || true; wait "$pid" 2>/dev/null || true
[ -s "$3" ] || { echo "Chrome made no screenshot for $1:" >&2; tail -5 "$prof.log" >&2; exit 1; }
rm -rf "$prof" "$prof.log"
echo "built: ${3#$PWD/}"
}
mkdir -p shots icons
shoot "http://127.0.0.1:$PORT/" 1440,900 "$PWD/shots/desktop.png"
shoot "http://127.0.0.1:$PORT/icons/icon.html" 512,512 "$PWD/icons/icon-512.png"
shoot "http://127.0.0.1:$PORT/icons/icon.html" 192,192 "$PWD/icons/icon-192.png"
shoot "http://127.0.0.1:$PORT/icons/icon.html" 180,180 "$PWD/icons/apple-touch-icon.png"