site-shots.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env bash
  2. # Re-draws every picture of the website and the README from a LYNX build, at 1x and 2x (round 190bi).
  3. #
  4. # scripts/site-shots.sh <LYNX.exe | LYNX binary> [renders-folder]
  5. #
  6. # Every route used is off-screen (no window, no input): --showcase, --snapshot, --gridshot, --plugui, --flextrashot,
  7. # --pluginthumbs, --danceshot, --artshot, each run once per scale with --shot-scale (the program draws the same layout
  8. # at that pixel density: vector and text at 2x, not an upscale). Then scripts/site-shots.py converts the renders into
  9. # website/assets/img (+ the @2x files, catalog-img.json, img-sizes.json, as website/_src/build.py reads them) and
  10. # docs/img (README), and prints a table of what it wrote.
  11. #
  12. # - A Windows exe from WSL (path ending in .exe): run through WSL's interop with Windows paths. These routes do not
  13. # take the foreground on builds with round 177d's fix; still, run it when nobody needs the screen.
  14. # - A Linux binary: needs an X display; without $DISPLAY it runs under xvfb-run, or under $LYNX_XVFB (an Xvfb binary,
  15. # started on display :$LYNX_XVFB_DISPLAY, default 97). Needs libasound (LD_LIBRARY_PATH is passed through).
  16. #
  17. # Environment: SCALES (default "1 2"), SITE (default website), DOCS (default docs/img), ROUTES (default all:
  18. # "snapshot grid plugui extra thumbs dance art"), NO_IMPORT=1 (renders only), DRY_RUN=1 (import without writing).
  19. # A fresh profile (LYNX_PROFILE_DIR, XDG_CONFIG_HOME) under the renders folder; LYNX_TEST_MODE=1; the theme and the
  20. # language are the defaults (English, dark).
  21. set -euo pipefail
  22. usage() { sed -n '2,22p' "$0" | sed 's/^# \{0,1\}//'; exit 2; }
  23. [[ $# -ge 1 && $1 != -h && $1 != --help ]] || usage
  24. bin=$(readlink -f "$1")
  25. [[ -f $bin ]] || { echo "site-shots: no program at $1" >&2; exit 2; }
  26. repo=$(cd "$(dirname "$0")/.." && pwd)
  27. out=$(mkdir -p "${2:-$repo/build-site-shots}" && cd "${2:-$repo/build-site-shots}" && pwd)
  28. SCALES=${SCALES:-1 2}
  29. SITE=${SITE:-$repo/website}
  30. DOCS=${DOCS:-$repo/docs/img}
  31. ROUTES=${ROUTES:-snapshot grid plugui extra thumbs dance art}
  32. want() { [[ " $ROUTES " == *" $1 "* ]]; }
  33. windows=0
  34. [[ $bin == *.exe ]] && windows=1
  35. # a path as the program sees it
  36. p() { if [[ $windows == 1 ]]; then wslpath -w "$1"; else printf '%s' "$1"; fi; }
  37. profile="$out/profile"
  38. mkdir -p "$profile"
  39. unset LYNX_SHOT_SCALE LYNX_PARITYSHOT_SCALE LYNX_SHOT_SUFFIX # (the scale comes from --shot-scale below)
  40. export LYNX_TEST_MODE=1 LYNX_PROFILE_DIR LYNX_SNAPSHOT_SIZE
  41. LYNX_PROFILE_DIR=$(p "$profile")
  42. export XDG_CONFIG_HOME="$profile/xdg"
  43. if [[ $windows == 1 ]]; then
  44. # (WSL hands these variables to the Windows program; /p converts a path)
  45. export WSLENV="${WSLENV:+$WSLENV:}LYNX_TEST_MODE:LYNX_PROFILE_DIR:LYNX_SNAPSHOT_SIZE"
  46. fi
  47. xpid=
  48. cleanup() { [[ -n $xpid ]] && kill "$xpid" 2> /dev/null || true; }
  49. trap cleanup EXIT
  50. runner=()
  51. if [[ $windows == 0 && -z ${DISPLAY:-} ]]; then
  52. if [[ -n ${LYNX_XVFB:-} ]]; then
  53. d=${LYNX_XVFB_DISPLAY:-97}
  54. "$LYNX_XVFB" ":$d" -screen 0 1920x1080x24 -nolisten tcp > "$out/xvfb.log" 2>&1 &
  55. xpid=$!
  56. export DISPLAY=":$d"
  57. for _ in $(seq 30); do [[ -e /tmp/.X11-unix/X$d ]] && break; sleep 0.1; done
  58. sleep 1
  59. kill -0 "$xpid" 2> /dev/null || { echo "site-shots: $LYNX_XVFB did not start (see $out/xvfb.log)" >&2; exit 2; }
  60. elif command -v xvfb-run > /dev/null; then
  61. runner=(xvfb-run -a -s "-screen 0 1920x1080x24")
  62. else
  63. echo "site-shots: no display; install xvfb (xvfb-run) or set LYNX_XVFB=/path/to/Xvfb" >&2
  64. exit 2
  65. fi
  66. fi
  67. log="$out/site-shots.log"
  68. : > "$log"
  69. lynx() { # lynx <name> <args...>: one run, its output in the log, its time on stdout
  70. local name=$1 t0=$SECONDS rc=0
  71. shift
  72. printf '%s\n' "== $name: $(basename "$bin") $*" >> "$log"
  73. "${runner[@]}" "$bin" "$@" >> "$log" 2>&1 || rc=$?
  74. printf ' %-22s %4ss exit %s\n' "$name" $((SECONDS - t0)) "$rc"
  75. return 0
  76. }
  77. echo "site-shots: $bin -> $out (scales: $SCALES)"
  78. demo="$out/Demo.lynx"
  79. [[ -f $demo ]] || lynx showcase --showcase "$(p "$demo")"
  80. [[ -f $demo ]] || { echo "site-shots: --showcase wrote no demo project (see $log)" >&2; exit 1; }
  81. for s in $SCALES; do
  82. x="$out/x$s"
  83. want thumbs && rm -rf -- "${x:?}/thumbs" # (--pluginthumbs skips the pictures that exist: start clean)
  84. mkdir -p "$x"/{snap,snap1600,grid,grid1600,plugui,extra,thumbs,dance}
  85. echo " scale $s -> $x"
  86. if want snapshot; then
  87. LYNX_SNAPSHOT_SIZE=1920x1080 lynx "snapshot 1920 @$s" --snapshot "$(p "$x/snap/s")" "$(p "$demo")" --shot-scale "$s"
  88. LYNX_SNAPSHOT_SIZE=1600x900 lynx "snapshot 1600 @$s" --snapshot "$(p "$x/snap1600/s")" "$(p "$demo")" --shot-scale "$s"
  89. fi
  90. if want grid; then
  91. LYNX_SNAPSHOT_SIZE=1920x1080 lynx "gridshot 1920 @$s" --gridshot "$(p "$x/grid/g")" --shot-scale "$s"
  92. LYNX_SNAPSHOT_SIZE=1600x900 lynx "gridshot 1600 @$s" --gridshot "$(p "$x/grid1600/g")" --shot-scale "$s"
  93. fi
  94. want plugui && lynx "plugui @$s" --plugui "$(p "$x/plugui")" --shot-scale "$s"
  95. want extra && lynx "flextrashot @$s" --flextrashot "$(p "$x/extra")" --shot-scale "$s"
  96. # (with a scale given, --pluginthumbs draws each plugin's whole editor, not the picker's 258 x 128 thumbnail)
  97. # (it skips the pictures that exist, so a run that stops early is simply run again while it still adds pictures:
  98. # the Linux build of 0.1.26-dev can end with bad_function_call after Drumpad in one long run, round 190bi)
  99. if want thumbs; then
  100. n=-1
  101. for try in 1 2 3 4 5; do
  102. have=$(find "$x/thumbs" -name '*.png' | wc -l)
  103. [[ $have == "$n" ]] && break
  104. n=$have
  105. lynx "pluginthumbs @$s #$try" --pluginthumbs "$(p "$x/thumbs")" --shot-scale "$s"
  106. grep -q '^drawn ' "$log" && [[ $(tail -n 3 "$log" | grep -c '^drawn ') -gt 0 ]] && break
  107. done
  108. fi
  109. want dance && lynx "danceshot @$s" --danceshot "$(p "$x/dance/d")" --shot-scale "$s"
  110. done
  111. # --artshot draws 100 % and 200 % itself (art/<name>-after-100.png and -200.png)
  112. want art && { mkdir -p "$out/art"; lynx "artshot 100+200" --artshot "$(p "$out/art")" after; }
  113. [[ ${NO_IMPORT:-0} == 1 ]] && { echo "site-shots: renders in $out (NO_IMPORT=1)"; exit 0; }
  114. python3 "$repo/scripts/site-shots.py" "$out" --site "$SITE" --docs "$DOCS" ${DRY_RUN:+--dry-run}