claude/skills/web-project-scaffold-containerized-python/scripts/scaffold.sh
Neil Bantoc 5201aa9bd5 Add web-frontend, web-project-audit and web-project-scaffold-containerized-python skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 16:26:33 -08:00

111 lines
6.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Write a new containerized Python web project from templates/project.
# scaffold.sh --dir <path> --name <slug> --repo-url <url> [--title "My App"] [--python 3.14]
# [--port 8000] [--branch main] [--install-url <url>] [--no-resolve]
# --repo-url is required: the remote must exist, since servers install and update from it.
# --install-url is the raw URL of install.sh on the default branch; derived for GitHub and
# GitLab, required for other hosts.
# Resolves current fastapi/uvicorn/pytest/httpx2 versions inside python:<ver>-slim and pins
# them (==X.Y.*), unless --no-resolve (then the fallback pins below are used).
# Writes .env from .env.example, runs `git init` and adds the remote as origin.
# Does not commit, build or start anything; run verify.sh for that.
set -euo pipefail
SKILL_DIR=$(cd "$(dirname "$0")/.." && pwd)
TEMPLATE="$SKILL_DIR/templates/project"
TOKENS="$SKILL_DIR/../web-frontend/assets/tokens.css"
dir= name= title= python=3.14 port=8000 branch=main repo_url= install_url= resolve=1
while [ $# -gt 0 ]; do
case $1 in
--dir) dir=$2; shift ;;
--name) name=$2; shift ;;
--title) title=$2; shift ;;
--python) python=$2; shift ;;
--port) port=$2; shift ;;
--branch) branch=$2; shift ;;
--repo-url) repo_url=$2; shift ;;
--install-url) install_url=$2; shift ;;
--no-resolve) resolve=0 ;;
-h|--help) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
shift
done
[ -n "$dir" ] && [ -n "$name" ] && [ -n "$repo_url" ] || { echo "usage: scaffold.sh --dir <path> --name <slug> --repo-url <url> [...]" >&2; exit 2; }
[[ $repo_url =~ ^(https://|ssh://|git@[^:]+:) ]] || { echo "--repo-url must be https://..., ssh://... or git@host:owner/repo.git" >&2; exit 2; }
[[ $repo_url =~ ://[^/]*@ ]] && { echo "--repo-url must not contain credentials; they go in the server's .env" >&2; exit 2; }
# Raw URL of install.sh, and the header a private repo's token goes in.
token_header="Authorization: Bearer "
if [[ $repo_url =~ github\.com[:/]([^/]+)/([^/]+)$ ]]; then
derived="https://raw.githubusercontent.com/${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}/$branch/install.sh"
token_header="Authorization: token "
elif [[ $repo_url =~ gitlab\.com[:/](.+)/([^/]+)$ ]]; then
gl_path="${BASH_REMATCH[1]}/${BASH_REMATCH[2]%.git}"
derived="https://gitlab.com/api/v4/projects/${gl_path//\//%2F}/repository/files/install.sh/raw?ref=$branch"
token_header="PRIVATE-TOKEN: "
else derived=; fi
install_url=${install_url:-$derived}
[ -n "$install_url" ] || { echo "can't derive the raw install.sh URL for $repo_url; pass --install-url" >&2; exit 2; }
[[ $name =~ ^[a-z][a-z0-9-]*$ ]] || { echo "--name must be a lowercase slug (letters, digits, -)" >&2; exit 2; }
[ -n "$title" ] || title=$(echo "$name" | tr '-' ' ' | sed -E 's/(^| )([a-z])/\1\u\2/g')
if [ -e "$dir" ] && [ -n "$(ls -A "$dir" | grep -v '^\.git$' || true)" ]; then
echo "$dir exists and is not empty; refusing to overwrite" >&2; exit 1
fi
# Fallback pins, verified 2026-11 on Python 3.14.
fastapi=0.162 uvicorn=0.54 pytest=9.2 httpx=2.16
if [ "$resolve" = 1 ]; then
echo "resolving current versions in python:$python-slim ..."
if out=$(docker run --rm "python:$python-slim" sh -c \
'pip install -q --disable-pip-version-check --root-user-action=ignore fastapi "uvicorn[standard]" pytest httpx2 >/dev/null 2>&1 && pip freeze'); then
v() { echo "$out" | sed -n "s/^$1==\([0-9]*\.[0-9]*\).*/\1/p"; }
fastapi=$(v fastapi) uvicorn=$(v uvicorn) pytest=$(v pytest) httpx=$(v httpx2)
else
echo "could not resolve versions (offline, or no python:$python-slim image); using fallback pins" >&2
fi
fi
mkdir -p "$dir"
cp -r "$TEMPLATE/." "$dir/"
if [ -f "$TOKENS" ]; then
cp "$TOKENS" "$dir/app/static/css/tokens.css"
else
echo "web-frontend skill not found; writing minimal tokens.css" >&2
cat > "$dir/app/static/css/tokens.css" <<'CSS'
:root { color-scheme: light dark; --hue: 250; --bg: oklch(0.985 0.01 var(--hue)); --surface: oklch(1 0 0);
--surface-2: oklch(0.965 0.01 var(--hue)); --border: oklch(0.9 0.015 var(--hue)); --fg: oklch(0.22 0.02 var(--hue));
--fg-muted: oklch(0.45 0.02 var(--hue)); --ok: oklch(0.55 0.13 150); --danger: oklch(0.55 0.19 25);
--font-body: ui-sans-serif, system-ui, sans-serif; --font-mono: ui-monospace, Menlo, monospace;
--text-xs: .75rem; --text-lg: 1.125rem; --weight-medium: 500; --weight-semibold: 600; --measure: 65ch;
--space-2: .5rem; --space-4: 1rem; --space-5: 1.25rem; --space-6: 1.5rem; --space-12: 3rem;
--gutter: max(16px, 4vw); --content-max: 72rem; --radius-md: 8px; --radius-full: 999px; }
@media (prefers-color-scheme: dark) { :root { --bg: oklch(0.17 0.015 var(--hue)); --surface: oklch(0.21 0.015 var(--hue));
--surface-2: oklch(0.245 0.015 var(--hue)); --border: oklch(0.3 0.02 var(--hue)); --fg: oklch(0.95 0.01 var(--hue));
--fg-muted: oklch(0.74 0.015 var(--hue)); --ok: oklch(0.72 0.14 150); --danger: oklch(0.7 0.17 25); } }
body { margin: 0; background: var(--bg); color: var(--fg); font-family: var(--font-body); line-height: 1.6; }
.label { font-size: var(--text-xs); font-weight: var(--weight-semibold); text-transform: uppercase; letter-spacing: .06em; color: var(--fg-muted); }
CSS
fi
# Fill placeholders in every text file.
find "$dir" -type f ! -path '*/.git/*' -print0 | xargs -0 sed -i \
-e "s|__NAME__|$name|g" -e "s|__TITLE__|$title|g" -e "s|__PYTHON__|$python|g" \
-e "s|__PORT__|$port|g" -e "s|__BRANCH__|$branch|g" \
-e "s|__REPO_URL__|$repo_url|g" -e "s|__INSTALL_URL__|$install_url|g" -e "s|__TOKEN_HEADER__|$token_header|g" \
-e "s|__FASTAPI__|$fastapi.*|g" -e "s|__UVICORN__|$uvicorn.*|g" \
-e "s|__PYTEST__|$pytest.*|g" -e "s|__HTTPX__|$httpx.*|g"
chmod +x "$dir/container/entrypoint.sh" "$dir/container/update.sh" "$dir/install.sh"
# The machine's own .env: gitignored, read by the container.
cp "$dir/.env.example" "$dir/.env"
chmod 600 "$dir/.env"
[ -d "$dir/.git" ] || git -C "$dir" init -q -b "$branch"
git -C "$dir" remote get-url origin >/dev/null 2>&1 || git -C "$dir" remote add origin "$repo_url"
left=$(grep -rl '__[A-Z]*__' "$dir" --exclude-dir=.git || true)
[ -z "$left" ] || { echo "unfilled placeholders in: $left" >&2; exit 1; }
echo "scaffolded $name in $dir (Python $python, fastapi $fastapi, uvicorn $uvicorn)"
echo "install command (after the first push): curl -fsSL $install_url | bash"
echo "next: bash $SKILL_DIR/scripts/verify.sh $dir"