Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / check
check217 lines · 8.9 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # Static verification without building or installing: |
| 3 | # ./gradlew :<mod>:detekt :<mod>:lint [...] + cargo check/clippy/fmt/doc (rust modules only) |
| 4 | # |
| 5 | # Usage: |
| 6 | # ./check <module> [module...] # detekt (detekt.yml) + lint + 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 | # |
| 11 | # Module notation mirrors ./install: slash == colon, games:/personal: shorthand, |
| 12 | # 'all' keyword, deduped. Tasks run with --continue so one broken module does not |
| 13 | # block the rest; exit code is nonzero if anything failed. |
| 14 | # |
| 15 | # Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check |
| 16 | # --locked`: workspace members together via `-p` from the repo root, standalone |
| 17 | # crates (own [workspace]) individually in their own dir (host target: fast smoke |
| 18 | # check; the real aarch64 cross-build runs inside the Gradle build via |
| 19 | # cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy |
| 20 | # --locked --all-targets` (blocking on deny/error lints; warnings never fail), |
| 21 | # `cargo fmt --check` (advisory report only until the tree is fmt-clean, then |
| 22 | # it promotes to blocking), and `cargo doc --locked --no-deps` (blocking on |
| 23 | # rustdoc errors). No rust modules -> cargo steps skipped. |
| 24 | set -euo pipefail |
| 25 | |
| 26 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 27 | ROOT="$SCRIPT_DIR" |
| 28 | |
| 29 | print_help() { |
| 30 | sed -n '2,/^set -euo/p' "$SCRIPT_DIR/check" | sed 's/^# \{0,1\}//' | head -n -1 |
| 31 | } |
| 32 | |
| 33 | normalize_module() { |
| 34 | local m="$1" |
| 35 | m="$(echo "$m" | xargs)" |
| 36 | m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" |
| 37 | m="$(echo "$m" | tr '/' ':')" |
| 38 | while echo "$m" | grep -q "::"; do m="$(echo "$m" | sed 's/::/:/g')"; done |
| 39 | echo "$m" | tr '[:upper:]' '[:lower:]' |
| 40 | } |
| 41 | |
| 42 | # all = app + library convention modules (both apply dev.detekt; pure-JVM |
| 43 | # modules like youpipe:extractor do not, so they are excluded here but still |
| 44 | # accepted when named explicitly and left for Gradle to report on). |
| 45 | all_module_dirs() { |
| 46 | grep -E 'include\(":[^"]+"\)' "$ROOT/settings.gradle.kts" 2>/dev/null \ |
| 47 | | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ |
| 48 | | tr ':' '/' \ |
| 49 | | while IFS= read -r p; do |
| 50 | case "$p" in "" ) continue;; esac |
| 51 | if [[ -f "$ROOT/$p/build.gradle.kts" ]] \ |
| 52 | && grep -qE 'common-conventions-(app|library)' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then |
| 53 | echo "$p" |
| 54 | fi |
| 55 | done | sort -u |
| 56 | } |
| 57 | |
| 58 | if [[ $# -eq 0 ]]; then print_help; echo ""; echo "Error: no modules specified."; exit 1; fi |
| 59 | for arg in "$@"; do case "$arg" in --help|-h|help) print_help; exit 0;; esac; done |
| 60 | |
| 61 | MODS=(); ALL_EXP=0 |
| 62 | for raw in "$@"; do |
| 63 | norm="$(normalize_module "$raw")" |
| 64 | [[ -z "$norm" ]] && continue |
| 65 | if [[ "$norm" == "all" ]]; then |
| 66 | if [[ $ALL_EXP -eq 0 ]]; then |
| 67 | while IFS= read -r sm; do |
| 68 | case "$sm" in "" ) continue;; esac |
| 69 | MODS+=("$sm") |
| 70 | done < <(all_module_dirs) |
| 71 | ALL_EXP=1 |
| 72 | fi |
| 73 | continue |
| 74 | fi |
| 75 | if [[ "$norm" != *":"* ]]; then |
| 76 | for pfx in games personal; do |
| 77 | [[ -f "$ROOT/$pfx/$norm/build.gradle.kts" ]] && { norm="$pfx:$norm"; break; } |
| 78 | done |
| 79 | fi |
| 80 | fs="$(echo "$norm" | tr ':' '/')" |
| 81 | if [[ ! -f "$ROOT/$fs/build.gradle.kts" ]]; then |
| 82 | echo "Error: unknown module '$raw' (-> $fs/, no build.gradle.kts)." >&2; exit 1 |
| 83 | fi |
| 84 | dup=0; for m in ${MODS[@]:-}; do [[ "$m" == "$fs" ]] && dup=1; done |
| 85 | [[ $dup -eq 0 ]] && MODS+=("$fs") |
| 86 | done |
| 87 | [[ ${#MODS[@]} -gt 0 ]] || { echo "Error: no valid modules." >&2; exit 1; } |
| 88 | |
| 89 | TASKS=() |
| 90 | for mod in "${MODS[@]}"; do |
| 91 | gmod="$(echo "$mod" | tr '/' ':')" |
| 92 | TASKS+=(":$gmod:detekt" ":$gmod:lint") |
| 93 | # cargo include_bytes!s the SPIR-V this task writes to build/spirv. |
| 94 | [[ -d "$ROOT/$mod/src/main/rust/shaders" ]] && TASKS+=(":$gmod:compileShaders") |
| 95 | done |
| 96 | # Guards issue #884 (no library may depend on a permission-declaring library). |
| 97 | # Root task, so add once however many modules are checked. |
| 98 | TASKS+=(":verifyLibraryBoundaries") |
| 99 | echo "Modules (${#MODS[@]}): ${MODS[*]}" |
| 100 | echo "Gradle tasks: ${TASKS[*]}" |
| 101 | echo "" |
| 102 | |
| 103 | DETEKT_RC=0 |
| 104 | "$SCRIPT_DIR/gradlew" "${TASKS[@]}" --continue --console=plain || DETEKT_RC=$? |
| 105 | echo "" |
| 106 | [[ $DETEKT_RC -ne 0 ]] && echo "Warning: detekt/lint reported failures (exit $DETEKT_RC)." |
| 107 | |
| 108 | # --- cargo check (rust modules only) --- |
| 109 | # Workspace members share one root `cargo check -p ...` invocation; standalone |
| 110 | # crates (own [workspace] table + lockfile, e.g. games:voxels, code) are checked |
| 111 | # individually with cwd set to the crate dir. |
| 112 | ROOT_CRATES=() |
| 113 | STANDALONE_DIRS=() |
| 114 | for mod in "${MODS[@]}"; do |
| 115 | rdir="$ROOT/$mod/src/main/rust" |
| 116 | ct="$rdir/Cargo.toml" |
| 117 | if [[ -f "$ct" ]]; then |
| 118 | if grep -qE '^\[workspace\][[:space:]]*$' "$ct"; then |
| 119 | STANDALONE_DIRS+=("$rdir") |
| 120 | echo "Rust: $mod -> standalone crate ($rdir)" |
| 121 | else |
| 122 | name="$(sed -nE 's/^name *= *"([^"]+)".*/\1/p' "$ct" | head -1)" |
| 123 | [[ -n "$name" ]] && ROOT_CRATES+=("$name") && echo "Rust: $mod -> workspace crate $name" |
| 124 | fi |
| 125 | fi |
| 126 | done |
| 127 | CARGO_RC=0 |
| 128 | if command -v cargo >/dev/null 2>&1; then |
| 129 | CARGO_BIN="cargo" |
| 130 | else |
| 131 | CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin/cargo" |
| 132 | [[ -x "$CARGO_BIN" ]] || { echo "Error: cargo not found ($CARGO_BIN)." >&2; exit 1; } |
| 133 | fi |
| 134 | if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then |
| 135 | echo "" |
| 136 | CARGO_ARGS=() |
| 137 | for c in "${ROOT_CRATES[@]}"; do CARGO_ARGS+=(-p "$c"); done |
| 138 | echo "Running: cargo check --locked ${CARGO_ARGS[*]}" |
| 139 | ( cd "$ROOT" && "$CARGO_BIN" check --locked "${CARGO_ARGS[@]}" ) || CARGO_RC=$? |
| 140 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check (workspace) reported failures (exit $CARGO_RC)." |
| 141 | fi |
| 142 | for rdir in ${STANDALONE_DIRS[@]:-}; do |
| 143 | echo "" |
| 144 | echo "Running: cargo check --locked (in $rdir)" |
| 145 | ( cd "$rdir" && "$CARGO_BIN" check --locked ) || CARGO_RC=$? |
| 146 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: cargo check ($rdir) reported failures (exit $CARGO_RC)." |
| 147 | done |
| 148 | # --- cargo clippy (rust modules only, lint-sweep Phase 4) --- |
| 149 | # Same scope as cargo check. Each crate's own `[lints]` table governs severity, |
| 150 | # so warnings never fail this step; only deny/error lints do. Runs with |
| 151 | # --all-targets so tests/examples/benches are covered too. |
| 152 | CLIPPY_RC=0 |
| 153 | if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then |
| 154 | echo "" |
| 155 | CLIPPY_ARGS=() |
| 156 | for c in "${ROOT_CRATES[@]}"; do CLIPPY_ARGS+=(-p "$c"); done |
| 157 | echo "Running: cargo clippy --locked ${CLIPPY_ARGS[*]} --all-targets" |
| 158 | ( cd "$ROOT" && "$CARGO_BIN" clippy --locked "${CLIPPY_ARGS[@]}" --all-targets ) || CLIPPY_RC=$? |
| 159 | [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy (workspace) reported failures (exit $CLIPPY_RC)." |
| 160 | fi |
| 161 | for rdir in ${STANDALONE_DIRS[@]:-}; do |
| 162 | echo "" |
| 163 | echo "Running: cargo clippy --locked --all-targets (in $rdir)" |
| 164 | ( cd "$rdir" && "$CARGO_BIN" clippy --locked --all-targets ) || CLIPPY_RC=$? |
| 165 | [[ $CLIPPY_RC -ne 0 ]] && echo "Warning: cargo clippy ($rdir) reported failures (exit $CLIPPY_RC)." |
| 166 | done |
| 167 | # --- cargo fmt (advisory only, lint-sweep Phase 4) --- |
| 168 | # The tree carries fmt drift today, so this step reports but never fails. |
| 169 | # Promotes to blocking once `cargo fmt --check` is clean on the owning slice. |
| 170 | if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then |
| 171 | echo "" |
| 172 | FMT_ARGS=() |
| 173 | for c in "${ROOT_CRATES[@]}"; do FMT_ARGS+=(-p "$c"); done |
| 174 | echo "Running: cargo fmt --check ${FMT_ARGS[*]} (advisory)" |
| 175 | FMT_OUT="$(cd "$ROOT" && "$CARGO_BIN" fmt --check "${FMT_ARGS[@]}" 2>&1)" || true |
| 176 | FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" |
| 177 | if [[ "$FMT_COUNT" -gt 0 ]]; then |
| 178 | echo "rustfmt drift: $FMT_COUNT hunks in scope (advisory; run cargo fmt -p <crate> to clean)." |
| 179 | else |
| 180 | echo "rustfmt: clean." |
| 181 | fi |
| 182 | fi |
| 183 | for rdir in ${STANDALONE_DIRS[@]:-}; do |
| 184 | echo "" |
| 185 | echo "Running: cargo fmt --check (in $rdir, advisory)" |
| 186 | FMT_OUT="$(cd "$rdir" && "$CARGO_BIN" fmt --check 2>&1)" || true |
| 187 | FMT_COUNT="$(echo "$FMT_OUT" | grep -c '^Diff in' || true)" |
| 188 | if [[ "$FMT_COUNT" -gt 0 ]]; then |
| 189 | echo "rustfmt drift: $FMT_COUNT hunks in $rdir (advisory)." |
| 190 | else |
| 191 | echo "rustfmt: clean ($rdir)." |
| 192 | fi |
| 193 | done |
| 194 | # --- cargo doc (rust modules only, lint-sweep Phase 4) --- |
| 195 | # Catches rustdoc::broken_intra_doc_links / invalid_html_tags. Both land as |
| 196 | # warnings first (warn-then-error), so this fails only on rustdoc errors. |
| 197 | DOC_RC=0 |
| 198 | if [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then |
| 199 | echo "" |
| 200 | DOC_ARGS=() |
| 201 | for c in "${ROOT_CRATES[@]}"; do DOC_ARGS+=(-p "$c"); done |
| 202 | echo "Running: cargo doc --locked --no-deps ${DOC_ARGS[*]}" |
| 203 | ( cd "$ROOT" && "$CARGO_BIN" doc --locked --no-deps "${DOC_ARGS[@]}" ) || DOC_RC=$? |
| 204 | [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc (workspace) reported failures (exit $DOC_RC)." |
| 205 | fi |
| 206 | for rdir in ${STANDALONE_DIRS[@]:-}; do |
| 207 | echo "" |
| 208 | echo "Running: cargo doc --locked --no-deps (in $rdir)" |
| 209 | ( cd "$rdir" && "$CARGO_BIN" doc --locked --no-deps ) || DOC_RC=$? |
| 210 | [[ $DOC_RC -ne 0 ]] && echo "Warning: cargo doc ($rdir) reported failures (exit $DOC_RC)." |
| 211 | done |
| 212 | if [[ ${#ROOT_CRATES[@]} -eq 0 && ${#STANDALONE_DIRS[@]} -eq 0 ]]; then |
| 213 | echo "No Rust crates in scope; cargo steps skipped." |
| 214 | fi |
| 215 | |
| 216 | if [[ $DETEKT_RC -ne 0 || $CARGO_RC -ne 0 || $CLIPPY_RC -ne 0 || $DOC_RC -ne 0 ]]; then exit 1; fi |
| 217 | exit 0 |