Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / format
format212 lines · 7.9 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # Auto-formatting for Kotlin (ktlint) and Rust (rustfmt): |
| 3 | # ./format <module> [...] # ktlintFormat + cargo fmt (writes changes) |
| 4 | # ./format --check <module> [...] # ktlintCheck + cargo fmt --check (fails on drift) |
| 5 | # |
| 6 | # Usage: |
| 7 | # ./format notes backup # multi-module, one gradlew invocation |
| 8 | # ./format voxels # shorthand -> games:voxels (same as ./install) |
| 9 | # ./format games/voxels maps # slash == colon |
| 10 | # ./format all # every app + library module |
| 11 | # ./format --check games/pipes # verify only, no writes |
| 12 | # |
| 13 | # Module notation mirrors ./check and ./install: slash == colon, |
| 14 | # games:/personal: shorthand, 'all' keyword, deduped. Gradle tasks run |
| 15 | # with --continue so one broken module does not block the rest; exit |
| 16 | # code is nonzero if anything failed. Configuration cache stays on |
| 17 | # (repo gradle.properties), so repeat runs are cheap. |
| 18 | # |
| 19 | # Kotlin style lives in the root .editorconfig (indent 4, max line 120, |
| 20 | # LF, final newline); ktlint runs its standard rule set against it. |
| 21 | # Rust style lives in the root rustfmt.toml (max_width 100). A module |
| 22 | # with no Rust sources simply skips the cargo step. |
| 23 | set -euo pipefail |
| 24 | |
| 25 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 26 | ROOT="$SCRIPT_DIR" |
| 27 | |
| 28 | print_help() { |
| 29 | sed -n '2,/^set -euo/p' "$SCRIPT_DIR/format" | sed 's/^# \{0,1\}//' | head -n -1 |
| 30 | } |
| 31 | |
| 32 | normalize_module() { |
| 33 | local m="$1" |
| 34 | m="$(echo "$m" | xargs)" |
| 35 | m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" |
| 36 | m="$(echo "$m" | tr '/' ':')" |
| 37 | while echo "$m" | grep -q "::"; do m="$(echo "$m" | sed 's/::/:/g')"; done |
| 38 | echo "$m" | tr '[:upper:]' '[:lower:]' |
| 39 | } |
| 40 | |
| 41 | # Same set as ./check's 'all': app + library convention modules (the ones |
| 42 | # that apply the ktlint convention). Pure-JVM modules are excluded here |
| 43 | # but still accepted when named explicitly. |
| 44 | all_module_dirs() { |
| 45 | grep -E 'include\(":[^"]+"\)' "$ROOT/settings.gradle.kts" 2>/dev/null \ |
| 46 | | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ |
| 47 | | tr ':' '/' \ |
| 48 | | while IFS= read -r p; do |
| 49 | case "$p" in "" ) continue;; esac |
| 50 | if [[ -f "$ROOT/$p/build.gradle.kts" ]] \ |
| 51 | && grep -qE 'common-conventions-(app|library)' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then |
| 52 | echo "$p" |
| 53 | fi |
| 54 | done | sort -u |
| 55 | } |
| 56 | |
| 57 | CHECK_ONLY=0 |
| 58 | ARGS=() |
| 59 | for arg in "$@"; do |
| 60 | case "$arg" in |
| 61 | --check) CHECK_ONLY=1;; |
| 62 | --help|-h|help) print_help; exit 0;; |
| 63 | *) ARGS+=("$arg");; |
| 64 | esac |
| 65 | done |
| 66 | if [[ ${#ARGS[@]} -eq 0 ]]; then print_help; echo ""; echo "Error: no modules specified."; exit 1; fi |
| 67 | |
| 68 | MODS=(); ALL_EXP=0 |
| 69 | for raw in "${ARGS[@]}"; do |
| 70 | norm="$(normalize_module "$raw")" |
| 71 | [[ -z "$norm" ]] && continue |
| 72 | if [[ "$norm" == "all" ]]; then |
| 73 | if [[ $ALL_EXP -eq 0 ]]; then |
| 74 | while IFS= read -r sm; do |
| 75 | case "$sm" in "" ) continue;; esac |
| 76 | MODS+=("$sm") |
| 77 | done < <(all_module_dirs) |
| 78 | ALL_EXP=1 |
| 79 | fi |
| 80 | continue |
| 81 | fi |
| 82 | if [[ "$norm" != *":"* ]]; then |
| 83 | for pfx in games personal; do |
| 84 | [[ -f "$ROOT/$pfx/$norm/build.gradle.kts" ]] && { norm="$pfx:$norm"; break; } |
| 85 | done |
| 86 | fi |
| 87 | fs="$(echo "$norm" | tr ':' '/')" |
| 88 | if [[ ! -f "$ROOT/$fs/build.gradle.kts" ]]; then |
| 89 | echo "Error: unknown module '$raw' (-> $fs/, no build.gradle.kts)." >&2; exit 1 |
| 90 | fi |
| 91 | dup=0; for m in ${MODS[@]:-}; do [[ "$m" == "$fs" ]] && dup=1; done |
| 92 | [[ $dup -eq 0 ]] && MODS+=("$fs") |
| 93 | done |
| 94 | [[ ${#MODS[@]} -gt 0 ]] || { echo "Error: no valid modules." >&2; exit 1; } |
| 95 | |
| 96 | KT_TASK="ktlintFormat"; FMT_LABEL="cargo fmt" |
| 97 | if [[ $CHECK_ONLY -eq 1 ]]; then KT_TASK="ktlintCheck"; FMT_LABEL="cargo fmt --check"; fi |
| 98 | |
| 99 | TASKS=() |
| 100 | for mod in "${MODS[@]}"; do |
| 101 | gmod="$(echo "$mod" | tr '/' ':')" |
| 102 | TASKS+=(":$gmod:$KT_TASK") |
| 103 | done |
| 104 | echo "Modules (${#MODS[@]}): ${MODS[*]}" |
| 105 | echo "Mode: $([[ $CHECK_ONLY -eq 1 ]] && echo check || echo format)" |
| 106 | echo "Gradle tasks: ${TASKS[*]}" |
| 107 | echo "" |
| 108 | |
| 109 | GRADLE_RC=0 |
| 110 | "$SCRIPT_DIR/gradlew" "${TASKS[@]}" --continue --console=plain || GRADLE_RC=$? |
| 111 | echo "" |
| 112 | [[ $GRADLE_RC -ne 0 ]] && echo "Warning: $KT_TASK reported failures (exit $GRADLE_RC)." |
| 113 | |
| 114 | # --- cargo fmt (rust modules only; same crate scoping as ./check) --- |
| 115 | # Workspace members share one root invocation via -p; standalone crates |
| 116 | # (own [workspace] table) are handled individually in their own dir. |
| 117 | # With `all`, the workspace-wide `--all` run also covers workspace crates |
| 118 | # with no Gradle module (scripts/, third_party/, library/tilecodec, |
| 119 | # library/jni-http, library/vision, communicate-dev/src/main/rust-mls), |
| 120 | # and any standalone crate that is not a workspace member (a Cargo.toml |
| 121 | # outside target/build/personal and outside the workspace member set) |
| 122 | # is formatted in its own dir. |
| 123 | standalone_crate_dirs() { |
| 124 | python3 - "$ROOT" "$CARGO_BIN" <<'PYEOF' |
| 125 | import json, os, pathlib, subprocess, sys |
| 126 | root = pathlib.Path(sys.argv[1]); cargo = sys.argv[2] |
| 127 | try: |
| 128 | meta = json.loads(subprocess.check_output([cargo, "metadata", "--format-version", "1", "--no-deps"], cwd=root, text=True, stderr=subprocess.DEVNULL)) |
| 129 | members = {str(pathlib.Path(p["manifest_path"]).parent) for p in meta["packages"]} |
| 130 | except Exception: |
| 131 | members = set() |
| 132 | skip = {"target", "build", ".git", ".gradle", "personal"} |
| 133 | for dirpath, dirnames, filenames in os.walk(root): |
| 134 | dirnames[:] = sorted(d for d in dirnames if d not in skip) |
| 135 | if "Cargo.toml" in filenames and dirpath != str(root) and dirpath not in members: |
| 136 | print(dirpath) |
| 137 | PYEOF |
| 138 | } |
| 139 | ROOT_CRATES=() |
| 140 | STANDALONE_DIRS=() |
| 141 | for mod in "${MODS[@]}"; do |
| 142 | rdir="$ROOT/$mod/src/main/rust" |
| 143 | ct="$rdir/Cargo.toml" |
| 144 | if [[ -f "$ct" ]]; then |
| 145 | if grep -qE '^\[workspace\][[:space:]]*$' "$ct"; then |
| 146 | STANDALONE_DIRS+=("$rdir") |
| 147 | echo "Rust: $mod -> standalone crate ($rdir)" |
| 148 | else |
| 149 | name="$(sed -nE 's/^name *= *"([^"]+)".*/\1/p' "$ct" | head -1)" |
| 150 | [[ -n "$name" ]] && ROOT_CRATES+=("$name") && echo "Rust: $mod -> workspace crate $name" |
| 151 | fi |
| 152 | fi |
| 153 | done |
| 154 | CARGO_RC=0 |
| 155 | if [[ ${#ROOT_CRATES[@]} -gt 0 || ${#STANDALONE_DIRS[@]} -gt 0 || $ALL_EXP -eq 1 ]]; then |
| 156 | if command -v cargo >/dev/null 2>&1; then |
| 157 | CARGO_BIN="cargo" |
| 158 | else |
| 159 | CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin/cargo" |
| 160 | [[ -x "$CARGO_BIN" ]] || { echo "Error: cargo not found ($CARGO_BIN)." >&2; exit 1; } |
| 161 | fi |
| 162 | fi |
| 163 | if [[ $ALL_EXP -eq 1 ]]; then |
| 164 | echo "" |
| 165 | if [[ $CHECK_ONLY -eq 1 ]]; then |
| 166 | echo "Running: $FMT_LABEL --all" |
| 167 | ( cd "$ROOT" && "$CARGO_BIN" fmt --check --all ) || CARGO_RC=$? |
| 168 | else |
| 169 | echo "Running: $FMT_LABEL --all" |
| 170 | ( cd "$ROOT" && "$CARGO_BIN" fmt --all ) || CARGO_RC=$? |
| 171 | fi |
| 172 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: $FMT_LABEL (workspace) reported failures (exit $CARGO_RC)." |
| 173 | elif [[ ${#ROOT_CRATES[@]} -gt 0 ]]; then |
| 174 | echo "" |
| 175 | FMT_ARGS=() |
| 176 | for c in "${ROOT_CRATES[@]}"; do FMT_ARGS+=(-p "$c"); done |
| 177 | if [[ $CHECK_ONLY -eq 1 ]]; then FMT_ARGS=(--check "${FMT_ARGS[@]}"); fi |
| 178 | echo "Running: $FMT_LABEL ${FMT_ARGS[*]}" |
| 179 | ( cd "$ROOT" && "$CARGO_BIN" fmt "${FMT_ARGS[@]}" ) || CARGO_RC=$? |
| 180 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: $FMT_LABEL (workspace) reported failures (exit $CARGO_RC)." |
| 181 | fi |
| 182 | for rdir in ${STANDALONE_DIRS[@]:-}; do |
| 183 | echo "" |
| 184 | echo "Running: $FMT_LABEL (in $rdir)" |
| 185 | if [[ $CHECK_ONLY -eq 1 ]]; then |
| 186 | ( cd "$rdir" && "$CARGO_BIN" fmt --check ) || CARGO_RC=$? |
| 187 | else |
| 188 | ( cd "$rdir" && "$CARGO_BIN" fmt ) || CARGO_RC=$? |
| 189 | fi |
| 190 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: $FMT_LABEL ($rdir) reported failures (exit $CARGO_RC)." |
| 191 | done |
| 192 | if [[ $ALL_EXP -eq 1 ]]; then |
| 193 | while IFS= read -r rdir; do |
| 194 | [[ -z "$rdir" ]] && continue |
| 195 | dup=0; for m in ${STANDALONE_DIRS[@]:-}; do [[ "$m" == "$rdir" ]] && dup=1; done |
| 196 | [[ $dup -eq 1 ]] && continue |
| 197 | echo "" |
| 198 | echo "Running: $FMT_LABEL (standalone in $rdir)" |
| 199 | if [[ $CHECK_ONLY -eq 1 ]]; then |
| 200 | ( cd "$rdir" && "$CARGO_BIN" fmt --check ) || CARGO_RC=$? |
| 201 | else |
| 202 | ( cd "$rdir" && "$CARGO_BIN" fmt ) || CARGO_RC=$? |
| 203 | fi |
| 204 | [[ $CARGO_RC -ne 0 ]] && echo "Warning: $FMT_LABEL ($rdir) reported failures (exit $CARGO_RC)." |
| 205 | done < <(standalone_crate_dirs) |
| 206 | fi |
| 207 | if [[ ${#ROOT_CRATES[@]} -eq 0 && ${#STANDALONE_DIRS[@]} -eq 0 && $ALL_EXP -eq 0 ]]; then |
| 208 | echo "No Rust crates in scope; cargo steps skipped." |
| 209 | fi |
| 210 | |
| 211 | if [[ $GRADLE_RC -ne 0 || $CARGO_RC -ne 0 ]]; then exit 1; fi |
| 212 | exit 0 |