Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / lint_hardcoded_strings.sh
lint_hardcoded_strings.sh122 lines · 3.8 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # Verification script: detect hardcoded UI strings that should be in strings.xml |
| 3 | # Excludes contentDescription (out-of-scope per plan), symbolic single-char, and |
| 4 | # already-migrated stringResource / R.string usages. |
| 5 | # Exit 0 = clean, non-zero = violations found. |
| 6 | |
| 7 | set -euo pipefail |
| 8 | |
| 9 | REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 10 | cd "$REPO_ROOT" |
| 11 | |
| 12 | echo "=== Hardcoded string audit (excluding contentDescription, single-char symbols) ===" |
| 13 | |
| 14 | # 1) Text("...") with alphabetic content |
| 15 | echo "" |
| 16 | echo "--- Text literals ---" |
| 17 | TEXT_HITS=$(grep -R 'Text\s*(\s*"' --include="*.kt" -n \ |
| 18 | --exclude-dir=build --exclude-dir=.gradle --exclude-dir=.git \ |
| 19 | . 2>/dev/null \ |
| 20 | | grep -v -E 'stringResource|R\.string|Log\.|//.*Text\(|contentDescription|newPlainText|ClipData|ClipEntry' \ |
| 21 | | grep -v -E 'androidTest|/test/|build/|origbackups|/.git/|/extractor/' \ |
| 22 | | python3 scripts/filter_text.py \ |
| 23 | || true) |
| 24 | |
| 25 | if [[ -n "$TEXT_HITS" ]]; then |
| 26 | echo "$TEXT_HITS" |
| 27 | TEXT_COUNT=$(echo "$TEXT_HITS" | wc -l) |
| 28 | echo "Found $TEXT_COUNT Text() violations" |
| 29 | else |
| 30 | echo "No Text() violations" |
| 31 | TEXT_COUNT=0 |
| 32 | fi |
| 33 | |
| 34 | # 2) Toast.makeText with literal |
| 35 | echo "" |
| 36 | echo "--- Toast.makeText literals ---" |
| 37 | TOAST_HITS=$(grep -R 'Toast\.makeText' --include="*.kt" -n \ |
| 38 | --exclude-dir=build --exclude-dir=.gradle --exclude-dir=.git \ |
| 39 | . 2>/dev/null \ |
| 40 | | grep -v -E 'androidTest|/test/|origbackups|/.git/|/extractor/' \ |
| 41 | | grep '"' \ |
| 42 | | grep -v -E 'R\.string|getString|stringResource' \ |
| 43 | || true) |
| 44 | |
| 45 | if [[ -n "$TOAST_HITS" ]]; then |
| 46 | echo "$TOAST_HITS" |
| 47 | TOAST_COUNT=$(echo "$TOAST_HITS" | wc -l) |
| 48 | echo "Found $TOAST_COUNT Toast violations" |
| 49 | else |
| 50 | echo "No Toast violations" |
| 51 | TOAST_COUNT=0 |
| 52 | fi |
| 53 | |
| 54 | # 3) label = { Text("...") } / placeholder with literals |
| 55 | echo "" |
| 56 | echo "--- label / placeholder / title literals ---" |
| 57 | LABEL_HITS=$(grep -R -E '(label\s*=\s*\{\s*Text\s*\(\s*"|placeholder\s*=\s*\{\s*Text\s*\(\s*")' \ |
| 58 | --include="*.kt" -n --exclude-dir=build --exclude-dir=.gradle --exclude-dir=.git \ |
| 59 | . 2>/dev/null \ |
| 60 | | grep -v -E 'stringResource|R\.string' \ |
| 61 | | grep -v -E 'androidTest|origbackups|/.git/|/extractor/' \ |
| 62 | || true) |
| 63 | |
| 64 | if [[ -n "$LABEL_HITS" ]]; then |
| 65 | echo "$LABEL_HITS" |
| 66 | LABEL_COUNT=$(echo "$LABEL_HITS" | wc -l) |
| 67 | echo "Found $LABEL_COUNT label/placeholder violations" |
| 68 | else |
| 69 | echo "No label/placeholder violations" |
| 70 | LABEL_COUNT=0 |
| 71 | fi |
| 72 | |
| 73 | # 4) contentDescription = "literal" (accessibility text, screen-reader visible) |
| 74 | echo "" |
| 75 | echo "--- contentDescription literals ---" |
| 76 | CD_HITS=$(grep -R -E 'contentDescription\s*=\s*"' --include="*.kt" -n \ |
| 77 | --exclude-dir=build --exclude-dir=.gradle --exclude-dir=.git \ |
| 78 | . 2>/dev/null \ |
| 79 | | grep -v -E 'stringResource|R\.string' \ |
| 80 | | grep -v -E 'androidTest|origbackups|/.git/|/extractor/' \ |
| 81 | | grep -E 'contentDescription\s*=\s*"[^"]*[A-Za-z]{3,}' \ |
| 82 | || true) |
| 83 | |
| 84 | if [[ -n "$CD_HITS" ]]; then |
| 85 | echo "$CD_HITS" |
| 86 | CD_COUNT=$(echo "$CD_HITS" | wc -l) |
| 87 | echo "Found $CD_COUNT contentDescription violations" |
| 88 | else |
| 89 | echo "No contentDescription violations" |
| 90 | CD_COUNT=0 |
| 91 | fi |
| 92 | |
| 93 | # 5) Plural string-gluing (e.g. if (n == 1) "" else "s") — must use <plurals> |
| 94 | echo "" |
| 95 | echo "--- plural string-gluing ---" |
| 96 | PLURAL_HITS=$(grep -R -E 'if \([^)]*==\s*1[lL]?\)\s*"[a-z]*"\s*else\s*"[a-z]+"' \ |
| 97 | --include="*.kt" -n --exclude-dir=build --exclude-dir=.gradle --exclude-dir=.git \ |
| 98 | . 2>/dev/null \ |
| 99 | | grep -v -E 'androidTest|origbackups|/.git/|/extractor/' \ |
| 100 | || true) |
| 101 | |
| 102 | if [[ -n "$PLURAL_HITS" ]]; then |
| 103 | echo "$PLURAL_HITS" |
| 104 | PLURAL_COUNT=$(echo "$PLURAL_HITS" | wc -l) |
| 105 | echo "Found $PLURAL_COUNT plural-gluing violations" |
| 106 | else |
| 107 | echo "No plural-gluing violations" |
| 108 | PLURAL_COUNT=0 |
| 109 | fi |
| 110 | |
| 111 | echo "" |
| 112 | echo "=== Summary ===" |
| 113 | TOTAL=$((TEXT_COUNT + TOAST_COUNT + LABEL_COUNT + CD_COUNT + PLURAL_COUNT)) |
| 114 | echo "Total violations: $TOTAL" |
| 115 | |
| 116 | if [[ $TOTAL -gt 0 ]]; then |
| 117 | echo "FAIL: hardcoded strings remain" |
| 118 | exit 1 |
| 119 | else |
| 120 | echo "PASS: no hardcoded UI strings found" |
| 121 | exit 0 |
| 122 | fi |