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

Modern-Apps.git / check

check290 lines · 12.0 KiBRaw
1#!/usr/bin/env bash
2# Static verification without building or installing:
3# ./gradlew :<mod>:detekt :<mod>:lint :<mod>:ktlintCheck [...] + cargo check/clippy/fmt/doc (rust modules only)
4#
5# Usage:
6# ./check <module> [module...] # detekt (detekt.yml) + lint + ktlintCheck + cargo check/clippy/fmt/doc
7# ./check games/voxels maps # multi-module, one gradlew invocation
8# ./check voxels # shorthand -> games:voxels (same as ./install)
9# ./check all # every app + library module
10# ./check structure # only the structure ratchets (below)
11#
12# Structure ratchets (docs/structure/README.md): tools/structure_check.py
13# checks Wear protocol-pair drift, the generated-source manifest, and the
14# exception-marker inventory. They run on every ./check invocation (cheap,
15# no build) and their failure fails this script.
16#
17# Module notation mirrors ./install: slash == colon, games:/personal: shorthand,
18# 'all' keyword, deduped. Tasks run with --continue so one broken module does not
19# block the rest; exit code is nonzero if anything failed.
20#
21# Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check
22# --locked`: workspace members together via `-p` from the repo root, standalone
23# crates (own [workspace]) individually in their own dir (host target: fast smoke
24# check; the real aarch64 cross-build runs inside the Gradle build via
25# cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy
26# --locked --all-targets` (blocking on deny/error lints; warnings never fail),
27# `cargo fmt --check` (blocking: this script fails on rustfmt drift; fix
28# with `./format <module>`), and `cargo doc --locked --no-deps` (blocking on
29# rustdoc errors). Kotlin formatting is enforced the same way: every module
30# runs `:ktlintCheck` next to detekt/lint; fix with `./format <module>`.
31# No rust modules -> cargo steps skipped.
32set -euo pipefail
33
34SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
35ROOT="$SCRIPT_DIR"
36
37print_help() {
38 sed -n '2,/^set -euo/p' "$SCRIPT_DIR/check" | sed 's/^# \{0,1\}//' | head -n -1
39}
40
41normalize_module() {
42 local m="$1"
43 m="$(echo "$m" | xargs)"
44 m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')"
45 m="$(echo "$m" | tr '/' ':')"
46 while echo "$m" | grep -q "::"; do m="$(echo "$m" | sed 's/::/:/g')"; done
47 echo "$m" | tr '[:upper:]' '[:lower:]'
48}
49
50# all = app + library convention modules (both apply dev.detekt and
51# org.jlleitschuh.gradle.ktlint; pure-JVM
52# modules like youpipe:extractor do not, so they are excluded here but still
53# accepted when named explicitly and left for Gradle to report on).
54all_module_dirs() {
55 grep -E 'include\(":[^"]+"\)' "$ROOT/settings.gradle.kts" 2>/dev/null \
56 | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \
57 | tr ':' '/' \
58 | while IFS= read -r p; do
59 case "$p" in "" ) continue;; esac
60 if [[ -f "$ROOT/$p/build.gradle.kts" ]] \
61 && grep -qE 'common-conventions-(app|library)' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then
62 echo "$p"
63 fi
64 done | sort -u
65}
66
67if [[ $# -eq 0 ]]; then print_help; echo ""; echo "Error: no modules specified."; exit 1; fi
68for arg in "$@"; do case "$arg" in --help|-h|help) print_help; exit 0;; esac; done
69for arg in "$@"; do
70 if [[ "$arg" == "structure" ]]; then
71 exec python3 "$ROOT/tools/structure_check.py" all
72 fi
73done
74
75MODS=(); ALL_EXP=0
76for raw in "$@"; do
77 norm="$(normalize_module "$raw")"
78 [[ -z "$norm" ]] && continue
79 if [[ "$norm" == "all" ]]; then
80 if [[ $ALL_EXP -eq 0 ]]; then
81 while IFS= read -r sm; do
82 case "$sm" in "" ) continue;; esac
83 MODS+=("$sm")
84 done < <(all_module_dirs)
85 ALL_EXP=1
86 fi
87 continue
88 fi
89 if [[ "$norm" != *":"* ]]; then
90 for pfx in games personal; do
91 [[ -f "$ROOT/$pfx/$norm/build.gradle.kts" ]] && { norm="$pfx:$norm"; break; }
92 done
93 fi
94 fs="$(echo "$norm" | tr ':' '/')"
95 if [[ ! -f "$ROOT/$fs/build.gradle.kts" ]]; then
96 echo "Error: unknown module '$raw' (-> $fs/, no build.gradle.kts)." >&2; exit 1
97 fi
98 dup=0; for m in ${MODS[@]:-}; do [[ "$m" == "$fs" ]] && dup=1; done
99 [[ $dup -eq 0 ]] && MODS+=("$fs")
100done
101[[ ${#MODS[@]} -gt 0 ]] || { echo "Error: no valid modules." >&2; exit 1; }
102
103TASKS=()
104for mod in "${MODS[@]}"; do
105 gmod="$(echo "$mod" | tr '/' ':')"
106 TASKS+=(":$gmod:detekt" ":$gmod:lint" ":$gmod:ktlintCheck")
107 # cargo include_bytes!s the SPIR-V this task writes to build/spirv.
108 [[ -d "$ROOT/$mod/src/main/rust/shaders" ]] && TASKS+=(":$gmod:compileShaders")
109done
110# Guards issue #884 (no library may depend on a permission-declaring library).
111# Root task, so add once however many modules are checked.
112TASKS+=(":verifyLibraryBoundaries")
113echo "Modules (${#MODS[@]}): ${MODS[*]}"
114echo "Gradle tasks: ${TASKS[*]}"
115echo ""
116
117STRUCTURE_RC=0
118if command -v python3 >/dev/null 2>&1; then
119 python3 "$ROOT/tools/structure_check.py" all || STRUCTURE_RC=$?
120else
121 echo "Error: python3 not found; structure checks (tools/structure_check.py) cannot run." >&2
122 STRUCTURE_RC=1
123fi
124[[ $STRUCTURE_RC -ne 0 ]] && echo "Warning: structure checks reported failures (exit $STRUCTURE_RC)."
125echo ""
126
127DETEKT_RC=0
128"$SCRIPT_DIR/gradlew" "${TASKS[@]}" --continue --console=plain || DETEKT_RC=$?
129echo ""
130[[ $DETEKT_RC -ne 0 ]] && echo "Warning: detekt/lint reported failures (exit $DETEKT_RC)."
131
132# --- cargo check (rust modules only) ---
133# Workspace members share one root `cargo check -p ...` invocation; standalone
134# crates (own [workspace] table + lockfile, e.g. games:voxels, code) are checked
135# individually with cwd set to the crate dir.
136ROOT_CRATES=()
137STANDALONE_DIRS=()
138for mod in "${MODS[@]}"; do
139 rdir="$ROOT/$mod/src/main/rust"
140 ct="$rdir/Cargo.toml"
141 if [[ -f "$ct" ]]; then
142 if grep -qE '^\[workspace\][[:space:]]*$' "$ct"; then
143 STANDALONE_DIRS+=("$rdir")
144 echo "Rust: $mod -> standalone crate ($rdir)"
145 else
146 name="$(sed -nE 's/^name *= *"([^"]+)".*/\1/p' "$ct" | head -1)"
147 [[ -n "$name" ]] && ROOT_CRATES+=("$name") && echo "Rust: $mod -> workspace crate $name"
148 fi
149 fi
150done
151CARGO_RC=0
152if command -v cargo >/dev/null 2>&1; then
153 CARGO_BIN="cargo"
154else
155 CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin/cargo"
156 [[ -x "$CARGO_BIN" ]] || { echo "Error: cargo not found ($CARGO_BIN)." >&2; exit 1; }
157fi
158if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then
159 echo ""
160 CARGO_ARGS=()
161 for c in "${ROOT_CRATES[@]}"; do CARGO_ARGS+=(-p "$c"); done
162 echo "Running: cargo check --locked ${CARGO_ARGS[*]}"
163 ( cd "$ROOT" && "$CARGO_BIN" check --locked "${CARGO_ARGS[@]}" ) || CARGO_RC=$?
164 [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check (workspace) reported failures (exit $CARGO_RC)."
165fi
166for rdir in ${STANDALONE_DIRS[@]:-}; do
167 echo ""
168 echo "Running: cargo check --locked (in $rdir)"
169 ( cd "$rdir" && "$CARGO_BIN" check --locked ) || CARGO_RC=$?
170 [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check ($rdir) reported failures (exit $CARGO_RC)."
171done
172# --- cargo clippy (rust modules only, lint-sweep Phase 4) ---
173# Same scope as cargo check. Each crate's own `[lints]` table governs severity,
174# so warnings never fail this step; only deny/error lints do. Runs with
175# --all-targets so tests/examples/benches are covered too.
176CLIPPY_RC=0
177if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then
178 echo ""
179 CLIPPY_ARGS=()
180 for c in "${ROOT_CRATES[@]}"; do CLIPPY_ARGS+=(-p "$c"); done
181 echo "Running: cargo clippy --locked ${CLIPPY_ARGS[*]} --all-targets"
182 ( cd "$ROOT" && "$CARGO_BIN" clippy --locked "${CLIPPY_ARGS[@]}" --all-targets ) || CLIPPY_RC=$?
183 [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy (workspace) reported failures (exit $CLIPPY_RC)."
184fi
185for rdir in ${STANDALONE_DIRS[@]:-}; do
186 echo ""
187 echo "Running: cargo clippy --locked --all-targets (in $rdir)"
188 ( cd "$rdir" && "$CARGO_BIN" clippy --locked --all-targets ) || CLIPPY_RC=$?
189 [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy ($rdir) reported failures (exit $CLIPPY_RC)."
190done
191# --- cargo fmt (blocking) ---
192# `cargo fmt --check` fails this script on drift. Fix with
193# `./format <module>` (or `cargo fmt -p <crate>`); style is rustfmt.toml.
194# With `all`, the workspace-wide `--all` run also covers workspace crates
195# with no Gradle module (scripts/, third_party/, library/tilecodec,
196# library/jni-http, library/vision, communicate-dev/src/main/rust-mls),
197# and any standalone crate that is not a workspace member (a Cargo.toml
198# outside target/build/personal and outside the workspace member set)
199# is checked in its own dir.
200standalone_crate_dirs() {
201 python3 - "$ROOT" "$CARGO_BIN" <<'PYEOF'
202import json, os, pathlib, subprocess, sys
203root = pathlib.Path(sys.argv[1]); cargo = sys.argv[2]
204try:
205 meta = json.loads(subprocess.check_output([cargo, "metadata", "--format-version", "1", "--no-deps"], cwd=root, text=True, stderr=subprocess.DEVNULL))
206 members = {str(pathlib.Path(p["manifest_path"]).parent) for p in meta["packages"]}
207except Exception:
208 members = set()
209skip = {"target", "build", ".git", ".gradle", "personal"}
210for dirpath, dirnames, filenames in os.walk(root):
211 dirnames[:] = sorted(d for d in dirnames if d not in skip)
212 if "Cargo.toml" in filenames and dirpath != str(root) and dirpath not in members:
213 print(dirpath)
214PYEOF
215}
216FMT_RC=0
217if [[ $ALL_EXP -eq 1 ]]; then
218 echo ""
219 echo "Running: cargo fmt --check --all"
220 FMT_OUT="$(cd "$ROOT" && "$CARGO_BIN" fmt --check --all 2>&1)" || FMT_RC=$?
221 if [[ $FMT_RC -ne 0 ]]; then
222 FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)"
223 echo "Error: rustfmt drift: $FMT_COUNT hunks in workspace (run ./format all to fix)."
224 else
225 echo "rustfmt: clean (workspace)."
226 fi
227elif [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then
228 echo ""
229 FMT_ARGS=()
230 for c in "${ROOT_CRATES[@]}"; do FMT_ARGS+=(-p "$c"); done
231 echo "Running: cargo fmt --check ${FMT_ARGS[*]}"
232 FMT_OUT="$(cd "$ROOT" && "$CARGO_BIN" fmt --check "${FMT_ARGS[@]}" 2>&1)" || FMT_RC=$?
233 if [[ $FMT_RC -ne 0 ]]; then
234 FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)"
235 echo "Error: rustfmt drift: $FMT_COUNT hunks in scope (run ./format <module> to fix)."
236 else
237 echo "rustfmt: clean."
238 fi
239fi
240for rdir in ${STANDALONE_DIRS[@]:-}; do
241 echo ""
242 echo "Running: cargo fmt --check (in $rdir)"
243 FMT_OUT="$(cd "$rdir" && "$CARGO_BIN" fmt --check 2>&1)" || FMT_RC=$?
244 if [[ $FMT_RC -ne 0 ]]; then
245 FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)"
246 echo "Error: rustfmt drift: $FMT_COUNT hunks in $rdir (run ./format <module> to fix)."
247 else
248 echo "rustfmt: clean ($rdir)."
249 fi
250done
251if [[ $ALL_EXP -eq 1 ]]; then
252 while IFS= read -r rdir; do
253 [[ -z "$rdir" ]] && continue
254 dup=0; for m in ${STANDALONE_DIRS[@]:-}; do [[ "$m" == "$rdir" ]] && dup=1; done
255 [[ $dup -eq 1 ]] && continue
256 echo ""
257 echo "Running: cargo fmt --check (standalone in $rdir)"
258 FMT_OUT="$(cd "$rdir" && "$CARGO_BIN" fmt --check 2>&1)" || FMT_RC=$?
259 if [[ $FMT_RC -ne 0 ]]; then
260 FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)"
261 echo "Error: rustfmt drift: $FMT_COUNT hunks in $rdir (run ./format all to fix)."
262 else
263 echo "rustfmt: clean ($rdir)."
264 fi
265 done < <(standalone_crate_dirs)
266fi
267# --- cargo doc (rust modules only, lint-sweep Phase 4) ---
268# Catches rustdoc::broken_intra_doc_links / invalid_html_tags. Both land as
269# warnings first (warn-then-error), so this fails only on rustdoc errors.
270DOC_RC=0
271if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then
272 echo ""
273 DOC_ARGS=()
274 for c in "${ROOT_CRATES[@]}"; do DOC_ARGS+=(-p "$c"); done
275 echo "Running: cargo doc --locked --no-deps ${DOC_ARGS[*]}"
276 ( cd "$ROOT" && "$CARGO_BIN" doc --locked --no-deps "${DOC_ARGS[@]}" ) || DOC_RC=$?
277 [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc (workspace) reported failures (exit $DOC_RC)."
278fi
279for rdir in ${STANDALONE_DIRS[@]:-}; do
280 echo ""
281 echo "Running: cargo doc --locked --no-deps (in $rdir)"
282 ( cd "$rdir" && "$CARGO_BIN" doc --locked --no-deps ) || DOC_RC=$?
283 [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc ($rdir) reported failures (exit $DOC_RC)."
284done
285if [[ ${#ROOT_CRATES[@]} -eq 0 && ${#STANDALONE_DIRS[@]} -eq 0 ]]; then
286 echo "No Rust crates in scope; cargo steps skipped."
287fi
288
289if [[ $STRUCTURE_RC -ne 0 || $DETEKT_RC -ne 0 || $CARGO_RC -ne 0 || $CLIPPY_RC -ne 0 || $FMT_RC -ne 0 || $DOC_RC -ne 0 ]]; then exit 1; fi
290exit 0