Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-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
7set -euo pipefail
8
9REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
10cd "$REPO_ROOT"
11
12echo "=== Hardcoded string audit (excluding contentDescription, single-char symbols) ==="
13
14# 1) Text("...") with alphabetic content
15echo ""
16echo "--- Text literals ---"
17TEXT_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
25if [[ -n "$TEXT_HITS" ]]; then
26 echo "$TEXT_HITS"
27 TEXT_COUNT=$(echo "$TEXT_HITS" | wc -l)
28 echo "Found $TEXT_COUNT Text() violations"
29else
30 echo "No Text() violations"
31 TEXT_COUNT=0
32fi
33
34# 2) Toast.makeText with literal
35echo ""
36echo "--- Toast.makeText literals ---"
37TOAST_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
45if [[ -n "$TOAST_HITS" ]]; then
46 echo "$TOAST_HITS"
47 TOAST_COUNT=$(echo "$TOAST_HITS" | wc -l)
48 echo "Found $TOAST_COUNT Toast violations"
49else
50 echo "No Toast violations"
51 TOAST_COUNT=0
52fi
53
54# 3) label = { Text("...") } / placeholder with literals
55echo ""
56echo "--- label / placeholder / title literals ---"
57LABEL_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
64if [[ -n "$LABEL_HITS" ]]; then
65 echo "$LABEL_HITS"
66 LABEL_COUNT=$(echo "$LABEL_HITS" | wc -l)
67 echo "Found $LABEL_COUNT label/placeholder violations"
68else
69 echo "No label/placeholder violations"
70 LABEL_COUNT=0
71fi
72
73# 4) contentDescription = "literal" (accessibility text, screen-reader visible)
74echo ""
75echo "--- contentDescription literals ---"
76CD_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
84if [[ -n "$CD_HITS" ]]; then
85 echo "$CD_HITS"
86 CD_COUNT=$(echo "$CD_HITS" | wc -l)
87 echo "Found $CD_COUNT contentDescription violations"
88else
89 echo "No contentDescription violations"
90 CD_COUNT=0
91fi
92
93# 5) Plural string-gluing (e.g. if (n == 1) "" else "s") — must use <plurals>
94echo ""
95echo "--- plural string-gluing ---"
96PLURAL_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
102if [[ -n "$PLURAL_HITS" ]]; then
103 echo "$PLURAL_HITS"
104 PLURAL_COUNT=$(echo "$PLURAL_HITS" | wc -l)
105 echo "Found $PLURAL_COUNT plural-gluing violations"
106else
107 echo "No plural-gluing violations"
108 PLURAL_COUNT=0
109fi
110
111echo ""
112echo "=== Summary ==="
113TOTAL=$((TEXT_COUNT + TOAST_COUNT + LABEL_COUNT + CD_COUNT + PLURAL_COUNT))
114echo "Total violations: $TOTAL"
115
116if [[ $TOTAL -gt 0 ]]; then
117 echo "FAIL: hardcoded strings remain"
118 exit 1
119else
120 echo "PASS: no hardcoded UI strings found"
121 exit 0
122fi