#!/usr/bin/env bash # Verification script: check the Compose-preview store-screenshot setup is coherent. # # None of this needs a compiler, which is the point — it catches the mechanical mistakes # that are otherwise only found by a full Gradle run: # # - a @Preview that is missing @PreviewTest (renders in Studio, silently not collected, # surfaces as the unhelpful "did not discover any tests") # - previews declared as top-level functions instead of class members (same symptom) # - preview functions not named Preview… (listing order comes from sorting the # generated filenames, so a stray name silently reshuffles the store listing) # - a @Preview without group = "phone|tablet|large-tablet|tv|wear" (the # form-factor bucket the collector routes it to; Triple-T spellings verbatim) # - duplicate Preview numbers inside one bucket (per-bucket 1.png… order # comes from the function-name sort, so a clash reshuffles that bucket) # - a chrome-owning screen rendered bare (a screen that ships under a tab bar, # rail or drawer on device, shown without it — the listing misses the chrome # users actually see; see CHROME_SCREENS below) # - a leftover src/androidTest MetadataScreenshots.kt from the old on-device generator # - a module that opted into previews but has no @Preview at all # # On-device capture is gone. A module either renders its listing from previews, or its # images are hand-captured and committed under metadata_data/photos/ (camera and # games/voxels: a live viewfinder and a Vulkan surface, neither of which Layoutlib draws). # # Exit 0 = clean, non-zero = problems found. set -uo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$REPO_ROOT" problems=0 note() { printf ' %s\n' "$1"; } fail() { printf ' ✗ %s\n' "$1"; problems=$((problems + 1)); } # Screens that ship under tab/rail/drawer chrome on device: a preview calling one # outside chrome (a ChromePreview/Tabbed wrapper or drawer call in the same # function) renders chrome-less. Format: "module:Screen,Screen module:Screen ...". # Only screens with an existing chrome-wrapped precedent are listed — wrapping a # new module means adding its screens here. Drawer-closed shells (files) need no # entry: a closed drawer is invisible. Screens on pushed routes (no chrome on # device) are deliberately absent: app detail/trust, contact details, office # documents, hub Yuu, VPN now-playing/album/expanded, email conversation/compose. CHROME_SCREENS="calculator:CalculatorScreen,GraphScreen,UnitConverterScreen clock:AlarmScreen,ClockScreen,TimerScreen,StopwatchScreen appstore:HomeScreen,SearchScreen,UpdatesScreen,LibraryScreen contacts:ContactListScreen,GroupsScreen office:HomeScreen vpn:ConfigListContent,LoggingContent games/chess:ChessGameScreen,PuzzleBoardScreen games/hub:DashboardScreen,GamesListScreen,ProfileScreen health:TodayScreen,NutritionScreen,BarChartDetailsScreen youpipe:SearchScreen,SubscriptionVideosScreen securemail:InboxScreen" # True when every Preview in $1 that calls one of $2 (the module's chrome # screens) also hosts chrome in the same function body — a ChromePreview/Tabbed # wrapper or drawer call — i.e. the screen reaches the listing only through # chrome. A preview proving a pushed route (no chrome on device) opts out with # a `// CHROME EXEMPT: ` comment above or inside its function. Prints # the offending preview's line and returns non-zero otherwise. POSIX awk only # (no gawk extensions — this also runs on macOS). chrome_wrapped() { local file="$1" screens="$2" local screen_pat screen_pat=$(printf '%s' "$screens" | sed 's/,/|/g') awk -v pat="($screen_pat)\\(" ' function flush() { if (in_preview && has_screen && !has_chrome && !exempt) { print FILENAME":"start": preview renders chrome screen without chrome" failed = 1 } } /CHROME EXEMPT/ { exempt_next = 1; next } /^[[:space:]]*((private|internal)[[:space:]]+)?fun / { flush() in_preview = ($0 ~ /fun Preview[0-9]/) exempt = exempt_next; exempt_next = 0 start = FNR has_screen = 0; has_chrome = 0 next } /CHROME EXEMPT/ { exempt = 1 } /ChromePreview\(|TabbedPagerScaffold\(|ModalNavigationDrawer\(|MusicTabsBar\(/ { has_chrome = 1 } $0 ~ pat { has_screen = 1 } END { flush(); if (failed) exit 1 } ' "$file" } echo "=== Compose preview metadata audit ===" echo # Sapling keeps copies of build files under .git/sl/origbackups; they are not modules. app_modules() { grep -rl "common-conventions-app" --include="build.gradle.kts" . 2>/dev/null \ | grep -v '/build/' | grep -v '^\./\.git/' | sed 's|^\./||' | sort } for gradle_file in $(app_modules); do module="${gradle_file%/build.gradle.kts}" # Match the id(...) declaration only — a module can legitimately *mention* the other # convention in a comment explaining why it is not using it (games/voxels does). uses_preview=$(grep -cE '^[[:space:]]*id\("common-conventions-preview-metadata"\)' "$gradle_file" || true) preview_dir="$module/src/screenshotTest" old_gen=$(find "$module/src/androidTest" -name 'MetadataScreenshots.kt' 2>/dev/null | head -1) if [[ "$uses_preview" -eq 0 ]]; then # Manual or unpublished. Either way it must not carry preview sources that nothing # renders. if [[ -d "$preview_dir" ]]; then echo "$module" fail "has src/screenshotTest but does not apply common-conventions-preview-metadata" fi continue fi header_shown=0 show() { [[ $header_shown -eq 0 ]] && echo "$module" && header_shown=1; } if [[ ! -d "$preview_dir" ]]; then show; fail "applies preview-metadata but has no src/screenshotTest" continue fi if [[ -n "$old_gen" ]]; then show; fail "leftover on-device generator: $old_gen" fi files=$(find "$preview_dir" -name '*.kt' 2>/dev/null) if [[ -z "$files" ]]; then show; fail "src/screenshotTest contains no Kotlin sources" continue fi n_preview=0 n_previewtest=0 # "N:group" entries — N from the Preview function name, group from the # @Preview(group = "...") on the annotation block above it. Per-bucket # ordering means the numbers must be unique within each group. bucket_numbers="" for f in $files; do # Count annotations only where they annotate a declaration, not in prose/KDoc. p=$(grep -cE '^[[:space:]]*@Preview\(' "$f" || true) t=$(grep -cE '^[[:space:]]*@PreviewTest[[:space:]]*$' "$f" || true) n_preview=$((n_preview + p)) n_previewtest=$((n_previewtest + t)) # The bucket: the group = "..." on the @Preview line (annotations are # single-line repo-wide). Matches CollectPreviewScreenshots.readPreviewGroups. groups_in_file=$(grep -E '^[[:space:]]*@Preview\(' "$f" \ | grep -oE 'group[[:space:]]*=[[:space:]]*"[^"]*"' \ | sed -E 's/.*group[[:space:]]*=[[:space:]]*"([^"]*)".*/\1/' || true) n_grouped=$(printf '%s\n' "$groups_in_file" | grep -c . || true) if [[ "$n_grouped" -ne "$p" ]]; then show; fail "$((p - n_grouped)) of $p @Preview(s) in ${f#"$module"/} have no group = \"...\"; the collector routes everything ungrouped to phone/" fi bad_group=$(printf '%s\n' "$groups_in_file" \ | grep -vE '^(phone|tablet|large-tablet|tv|wear)$' || true) for g in $bad_group; do show; fail "unknown screenshot bucket \"$g\" in ${f#"$module"/}; expected phone, tablet, large-tablet, tv or wear" done # Preview functions must be class members; a top-level one lands in a synthetic # ...Kt facade the engine does not scan. if grep -qE '^fun Preview[0-9]' "$f"; then show; fail "top-level preview function in ${f#"$module"/}; must be a class member" fi # Listing order is filename order, which embeds the function name. # BSD sed (macOS) has no \s, so use POSIX classes throughout. badly_named=$(grep -oE '^[[:space:]]+fun [A-Za-z0-9_]+\(\)' "$f" \ | sed -E 's/^[[:space:]]+fun //; s/\(\)//' \ | grep -vE '^Preview[0-9]' || true) for name in $badly_named; do # Only complain about functions that are actually previews. if grep -B4 -E "fun $name\(\)" "$f" | grep -q '@Preview('; then show; fail "preview function '$name' is not named Preview…; listing order will be wrong" fi done # Pair each Preview function with its bucket: walk the file keeping the # last-seen group, like the collector does. Well-named preview functions # only — badly-named ones are already flagged above. (The group is NOT # cleared on @Composable: it always sits between @Preview and fun.) pending="" while IFS= read -r line; do case "$line" in *'@Preview('*) g=$(printf '%s' "$line" | grep -oE 'group[[:space:]]*=[[:space:]]*"[^"]*"' \ | sed -E 's/.*group[[:space:]]*=[[:space:]]*"([^"]*)".*/\1/' || true) [[ -n "$g" ]] && pending="$g" ;; esac if [[ "$line" =~ ^[[:space:]]*fun\ (Preview[0-9]+[A-Za-z0-9_]*)\(\) ]]; then if [[ -n "$pending" ]]; then n="${BASH_REMATCH[1]}" bucket_numbers="$bucket_numbers ${pending}:${n}" fi pending="" fi done < "$f" done # Numbers must be unique within each bucket — per-bucket 1.png… numbering # sorts by function name, so a clash reshuffles that bucket's listing. dupes=$(printf '%s\n' "$bucket_numbers" | grep . | sort | uniq -d || true) for d in $dupes; do show; fail "duplicate screenshot number in bucket \"${d%%:*}\" (${d##*:} twice); per-bucket order will be wrong" done # Chrome-owning screens must reach the listing through chrome, not bare. for entry in $CHROME_SCREENS; do entry_module="${entry%%:*}" if [[ "$module" == "$entry_module" ]]; then entry_screens="${entry#*:}" for f in $files; do if ! chrome_wrapped "$f" "$entry_screens"; then show; fail "chrome-owning screen rendered bare in ${f#"$module"/} (see line above); wrap it in ChromePreview" fi done fi done if [[ "$n_preview" -eq 0 ]]; then show; fail "no @Preview found under src/screenshotTest" elif [[ "$n_previewtest" -ne "$n_preview" ]]; then show; fail "$n_preview @Preview but $n_previewtest @PreviewTest — every preview needs both" fi [[ $header_shown -eq 1 ]] || printf '%-24s %s preview(s)\n' "$module" "$n_preview" done echo echo "--- shared infrastructure ---" for f in build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.kts \ gradle/libs.versions.toml gradle.properties .gitignore; do [[ -f "$f" ]] || fail "missing $f" done grep -q 'android.experimental.enableScreenshotTest=true' gradle.properties \ || fail "gradle.properties is missing android.experimental.enableScreenshotTest=true" grep -q 'screenshot-validation-api' gradle/libs.versions.toml \ || fail "libs.versions.toml is missing screenshot-validation-api (supplies @PreviewTest)" grep -q 'src/screenshotTest\*/reference/' .gitignore \ || fail ".gitignore is not ignoring the rendered reference images" echo preview_modules=0 while read -r gf; do if grep -qE '^[[:space:]]*id\("common-conventions-preview-metadata"\)' "$gf"; then preview_modules=$((preview_modules + 1)) fi done < <(app_modules) echo "=== $preview_modules module(s) render their listing from Compose previews ===" if [[ $problems -gt 0 ]]; then echo "=== $problems problem(s) found ===" exit 1 fi echo "=== clean ==="