Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

4.8 KB
Raw Blame
#!/bin/bash
# Size limits (Shane, 10-27): a code file is at most FILE_MAX lines and a Rust crate at most CRATE_MAX lines of its own
# .rs (a nested crate's files count for that crate, not its parent), so any one file or crate stays small enough to hold
# in context and to rebuild alone. Files and crates over the limit today are listed in tools/size.allow with their size
# then; each may shrink but never grow, and one that isn't listed may not go over. The overhaul splits them until the
# allow file is empty.
#   tools/size.sh             check: each file or crate over its limit or allowance
#   tools/size.sh --report    every crate: own lines, files, its largest file, workspace crates it uses, crates using it
#   tools/size.sh --update    tools/size.allow rewritten to today's sizes (refused if any size rose)
set -u
ROOT=$(cd "$(dirname "$0")/.." && pwd)
cd "$ROOT" || exit 2
FILE_MAX=${FILE_MAX:-500}
CRATE_MAX=${CRATE_MAX:-2000}
ALLOW=tools/size.allow
# code only (raea-skills, skills/, is its own repo: an export has its files, the live tree's git doesn't list them);
# vendored crates are upstream's, lockfiles and type declarations are generated, and so is a file whose first
# line says it was ("// Generated by ...": its generator checks it for drift, and nobody edits it)
files() {
  git ls-files --cached --others --exclude-standard -- '*.rs' '*.ts' '*.tsx' '*.mjs' '*.js' '*.sh' ':!vendor/*' ':!*/vendor/*' ':!**/node_modules/**' ':!*.d.ts' ':!skills/*' \
    ':!*.min.js' | while read -r f; do [ -f "$f" ] && ! head -1 "$f" | grep -qi '^// generated by' && echo "$f"; done
}
# "lines path" per file over FILE_MAX
big_files() { files | tr '\n' '\0' | xargs -0 wc -l 2>/dev/null | grep -v ' total$' | awk -v m="$FILE_MAX" '$1>m {print $1, $2}'; }
# "name dir" per crate of ours, from the tracked Cargo.toml files (vendored ones left out). Not cargo metadata: L0
# runs this in a copy of what lands, where the workspace resolves differently and nested crates went missing
crates() {
  git ls-files --cached --others --exclude-standard -- '*Cargo.toml' ':!vendor/*' ':!*/vendor/*' | while read -r f; do
    [ -f "$f" ] || continue
    n=$(awk -F'"' '/^\[package\]/ {p=1} p && /^name *=/ {print $2; exit}' "$f")
    [ -n "$n" ] && echo "$n $(dirname "$f")"
  done
}
# "lines crate:name" per crate: its own .rs, without any nested crate's
crate_lines() {
  local list dirs
  list=$(crates)
  dirs=$(echo "$list" | awk '{print $2}')
  echo "$list" | while read -r name dir; do
    local skip=() d
    for d in $dirs; do case "$d" in "$dir"/*) skip+=(":!$d/*") ;; esac; done
    n=$(git ls-files --cached --others --exclude-standard -- "$dir/*.rs" ${skip[@]+"${skip[@]}"} | while read -r f; do [ -f "$f" ] && cat "$f"; done | wc -l)
    echo "$((n)) crate:$name"
  done
}
current() { { big_files; crate_lines | awk -v m="$CRATE_MAX" '$1>m'; } | sort -k2; }

if [ "${1:-}" = "--report" ]; then
  meta=$(cargo metadata --format-version 1 2>/dev/null)
  printf '%7s %5s %7s %4s %4s  %s\n' lines files largest uses used crate
  crates | while read -r name dir; do
    n=$(crate_lines | awk -v c="crate:$name" '$2==c {print $1}')
    nf=$(git ls-files --cached --others --exclude-standard -- "$dir/*.rs" | wc -l)
    lg=$(git ls-files --cached --others --exclude-standard -- "$dir/*.rs" | tr '\n' '\0' | xargs -0 wc -l 2>/dev/null | grep -v ' total$' | sort -rn | head -1 | awk '{print $1}')
    uses=$(echo "$meta" | jq --arg n "$name" '[.resolve.nodes[] | select(.id | test("[/#]" + $n + "[@#]")) | .deps[].name] as $d | [.packages[] | select(.source == null) | .name | gsub("-";"_")] as $w | [$d[] | select(. as $x | $w | index($x))] | length')
    used=$(echo "$meta" | jq --arg n "$name" '[.packages[] | select(.source == null) | select(any(.dependencies[]; .name == $n))] | length')
    printf '%7d %5d %7d %4d %4d  %s\n' "$n" "$nf" "${lg:-0}" "$uses" "$used" "$name"
  done | sort -rn
  exit 0
fi

now=$(current)
if [ "${1:-}" = "--update" ]; then
  rose=$(join -1 2 -2 2 <(echo "$now") <(grep -v '^#' "$ALLOW" 2>/dev/null | sort -k2) | awk '$2>$3 {print $1 ": " $3 " -> " $2}')
  [ -z "$rose" ] || { echo "size: refused, these grew:" >&2; echo "$rose" >&2; exit 1; }
  { echo "# allowance per file or crate over the limit (tools/size.sh): it only shrinks; split it and take the new size"
    echo "$now"; } > "$ALLOW"
  echo "size: $(echo "$now" | grep -c .) over the limit, $ALLOW updated"
  exit 0
fi
bad=0
while read -r n what; do
  [ -n "$what" ] || continue
  a=$(awk -v w="$what" '$2==w {print $1}' "$ALLOW" 2>/dev/null)
  case "$what" in crate:*) m=$CRATE_MAX ;; *) m=$FILE_MAX ;; esac
  if [ -z "$a" ]; then echo "size: $what is $n lines, over the $m limit: split it" >&2; bad=1
  elif [ "$n" -gt "$a" ]; then echo "size: $what grew to $n lines (allowed $a while it's split; limit $m)" >&2; bad=1; fi
done <<< "$now"
[ $bad -eq 0 ] && echo "size: ok ($(echo "$now" | grep -c .) still over the limit, none grew)"
exit $bad