From d0958fce806131ca24be5782b61ff0f3ab148418 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 6 Jul 2026 23:03:17 +0200 Subject: [PATCH] Make script/ entrypoints POSIX sh; add bootstrap and setup --- Makefile | 8 ++++- README.md | 8 +++-- prompts/NEW_REPO_CHECKLIST.md | 5 +++ prompts/REPO_POLICIES.md | 49 +++++++++++++++----------- script/bootstrap | 65 +++++++++++++++++++++++++++++++++++ script/check | 6 ++-- script/cibuild | 6 ++-- script/docker | 6 ++-- script/fmt | 6 ++-- script/fmt-check | 6 ++-- script/install-precommit | 12 +++---- script/lint | 6 ++-- script/precommit | 6 ++-- script/projectname | 4 +-- script/setup | 13 +++++++ script/test | 6 ++-- 16 files changed, 156 insertions(+), 56 deletions(-) create mode 100755 script/bootstrap create mode 100755 script/setup diff --git a/Makefile b/Makefile index 063870b..dde6752 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,15 @@ -.PHONY: test lint fmt fmt-check check docker hooks +.PHONY: bootstrap setup test lint fmt fmt-check check docker hooks # Makefile targets are thin shims; the implementations live in script/ # per the scripts-to-rule-them-all pattern (see the Entrypoints section # of README.md). +bootstrap: + @script/bootstrap + +setup: + @script/setup + test: @script/test diff --git a/README.md b/README.md index 45a9535..3991bfc 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,13 @@ needed in your projects. This repository adheres to the [Scripts to Rule Them All](https://github.com/github/scripts-to-rule-them-all) standard: normalized scripts in `script/` are the entrypoints for the -development workflow, and the Makefile targets are thin shims that call them. We -provide: +development workflow, and the Makefile targets are thin shims that call them. +The scripts are POSIX sh (not bash) so they run in minimal containers such as +alpine. We provide: +- `script/bootstrap` — install all dependencies (yarn install) +- `script/setup` — set up the repo for development after a fresh clone: runs + `script/bootstrap`, then `script/install-precommit` - `script/projectname` — output the project name (our own extension); used by `script/docker` for the image tag - `script/test` — run the test suite (no tests defined here) diff --git a/prompts/NEW_REPO_CHECKLIST.md b/prompts/NEW_REPO_CHECKLIST.md index 34e4577..be900bf 100644 --- a/prompts/NEW_REPO_CHECKLIST.md +++ b/prompts/NEW_REPO_CHECKLIST.md @@ -71,6 +71,11 @@ Implementations live in `script/` (scripts-to-rule-them-all); Makefile targets are thin shims calling them. Model scripts: `https://git.eeqj.de/sneak/prompts/raw/branch/main/script/` +- [ ] scripts are POSIX sh (`#!/bin/sh`, `set -eu`, no bashisms) so they run on + alpine images without bash +- [ ] `script/bootstrap` / `make bootstrap` — installs all dependencies +- [ ] `script/setup` / `make setup` — readies a fresh clone: runs `bootstrap`, + then `install-precommit`, plus repo-specific init - [ ] `script/test` / `make test` — runs real tests, not a no-op (30-second timeout) - [ ] `script/lint` / `make lint` — runs linter diff --git a/prompts/REPO_POLICIES.md b/prompts/REPO_POLICIES.md index 58c3753..e164478 100644 --- a/prompts/REPO_POLICIES.md +++ b/prompts/REPO_POLICIES.md @@ -34,29 +34,35 @@ style conventions are in separate documents: every file before committing. There are zero exceptions to this rule. - Every repo with software must have a root `Makefile` with these targets: - `make test`, `make lint`, `make fmt` (writes), `make fmt-check` (read-only), - `make check` (runs `test`, `lint`, `fmt-check`), `make docker`, and - `make hooks` (installs pre-commit hook). A model Makefile is at - `https://git.eeqj.de/sneak/prompts/raw/branch/main/Makefile`. + `make bootstrap`, `make setup`, `make test`, `make lint`, `make fmt` (writes), + `make fmt-check` (read-only), `make check` (runs `test`, `lint`, `fmt-check`), + `make docker`, and `make hooks` (installs pre-commit hook). A model Makefile + is at `https://git.eeqj.de/sneak/prompts/raw/branch/main/Makefile`. - Repos follow the [Scripts to Rule Them All](https://github.com/github/scripts-to-rule-them-all) pattern: the implementation of each Makefile target lives in an executable - script in `script/` (`script/test`, `script/lint`, `script/fmt`, - `script/fmt-check`, `script/check`, `script/docker`), and the Makefile targets - are thin shims that call them. `script/cibuild` (a standard - scripts-to-rule-them-all name) runs the CI build: it changes to the repo root - and runs `docker build .`; the Gitea workflow calls it. Four further scripts - are our own extensions to the standard: `script/check` runs `script/test`, - `script/lint`, and `script/fmt-check`; `script/precommit` is what the git - pre-commit hook runs, and it calls `script/check`; `script/install-precommit` - installs the git pre-commit hook (the `make hooks` target shims to it); and - `script/projectname` (literally that filename) simply outputs the project's - name. Scripts that need the name call `script/projectname` — e.g. - `script/docker` assembles its image tag from it — so those scripts stay - byte-identical across all repos. Repo-type-specific pre-commit extras (e.g. - `go mod tidy` verification in Go repos) belong in `script/precommit`, not in - the hook itself. Model scripts are at + script in `script/` (`script/bootstrap`, `script/setup`, `script/test`, + `script/lint`, `script/fmt`, `script/fmt-check`, `script/check`, + `script/docker`), and the Makefile targets are thin shims that call them. The + scripts must be POSIX sh (`#!/bin/sh`, `set -eu`, no bashisms) so they run in + minimal containers (e.g. alpine images have no bash); locate the repo root + with `$(cd "$(dirname "$0")/.." && pwd -P)` and `cd` there before acting. From + the standard's canonical set we use `bootstrap` (install all dependencies), + `setup` (make the repo ready for development after a fresh clone: runs + `bootstrap`, then `install-precommit`, plus any repo-specific initialization), + `test`, and `cibuild`. `script/cibuild` runs the CI build: it changes to the + repo root and runs `docker build .`; the Gitea workflow calls it. Four further + scripts are our own extensions to the standard: `script/check` runs + `script/test`, `script/lint`, and `script/fmt-check`; `script/precommit` is + what the git pre-commit hook runs, and it calls `script/check`; + `script/install-precommit` installs the git pre-commit hook (the `make hooks` + target shims to it); and `script/projectname` (literally that filename) simply + outputs the project's name. Scripts that need the name call + `script/projectname` — e.g. `script/docker` assembles its image tag from it — + so those scripts stay byte-identical across all repos. Repo-type-specific + pre-commit extras (e.g. `go mod tidy` verification in Go repos) belong in + `script/precommit`, not in the hook itself. Model scripts are at `https://git.eeqj.de/sneak/prompts/raw/branch/main/script/`. The README must document the provided scripts in an **Entrypoints** section (see the README requirements below). @@ -380,8 +386,9 @@ style conventions are in separate documents: - `README.md`, `.git`, `.gitignore`, `.editorconfig` - `LICENSE`, `REPO_POLICIES.md` (copy from the `prompts` repo) - `Makefile` - - `script/` entrypoints (`projectname`, `test`, `lint`, `fmt`, `fmt-check`, - `check`, `docker`, `cibuild`, `precommit`, `install-precommit`) + - `script/` entrypoints (`bootstrap`, `setup`, `projectname`, `test`, + `lint`, `fmt`, `fmt-check`, `check`, `docker`, `cibuild`, `precommit`, + `install-precommit`) - `Dockerfile`, `.dockerignore` - `.gitea/workflows/check.yml` - Go: `go.mod`, `go.sum`, `.golangci.yml` diff --git a/script/bootstrap b/script/bootstrap new file mode 100755 index 0000000..3afdb94 --- /dev/null +++ b/script/bootstrap @@ -0,0 +1,65 @@ +#!/bin/sh +# script/bootstrap: install all dependencies needed to build and develop +# this repo. Idempotent: every install is guarded by a check so already +# installed tools are skipped. Installs base tooling with nix, apt, or +# brew (detected in that order); assumes nothing is present. +set -eu + +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" + +PKGMGR="" +SUDO="" + +detect_pkgmgr() { + if command -v nix-env >/dev/null 2>&1; then + PKGMGR="nix" + elif command -v apt-get >/dev/null 2>&1; then + PKGMGR="apt" + elif command -v brew >/dev/null 2>&1; then + PKGMGR="brew" + else + echo "bootstrap: no supported package manager (nix, apt, brew)" >&2 + exit 1 + fi + if [ "$PKGMGR" = "apt" ]; then + export DEBIAN_FRONTEND=noninteractive + if [ "$(id -u)" != "0" ]; then + SUDO="sudo" + fi + fi +} + +# pkg_install +pkg_install() { + case "$PKGMGR" in + nix) nix-env -iA "nixpkgs.$1" ;; + apt) $SUDO env DEBIAN_FRONTEND=noninteractive apt-get install -y "$2" ;; + brew) brew install "$3" ;; + esac +} + +missing() { + ! command -v "$1" >/dev/null 2>&1 +} + +main() { + cd "$ROOT" + detect_pkgmgr + + if missing git; then pkg_install git git git; fi + if missing make; then pkg_install gnumake make make; fi + if missing node; then pkg_install nodejs nodejs node; fi + if missing yarn; then + # corepack ships with node >= 16 and provides yarn + if command -v corepack >/dev/null 2>&1; then + corepack enable + else + pkg_install yarn yarnpkg yarn + fi + fi + + yarn install --frozen-lockfile + echo "bootstrap complete" +} + +main "$@" diff --git a/script/check b/script/check index 26ca4bc..3e1778c 100755 --- a/script/check +++ b/script/check @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh # script/check: run all checks (test, lint, fmt-check). Our own # extension to scripts-to-rule-them-all. Must not modify any files. -set -euo pipefail +set -eu -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" main() { "$SCRIPT_DIR/test" diff --git a/script/cibuild b/script/cibuild index 56c96d1..75cc3e6 100755 --- a/script/cibuild +++ b/script/cibuild @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh # script/cibuild: run the CI build. The Dockerfile runs script/check, so # a successful build implies all checks pass. -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT" diff --git a/script/docker b/script/docker index 59f26aa..9b9ea86 100755 --- a/script/docker +++ b/script/docker @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh # script/docker: build the Docker image tagged with the project name. # Identical in all repos; the tag comes from script/projectname. -set -euo pipefail +set -eu -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)" main() { diff --git a/script/fmt b/script/fmt index 999e47b..62d88e9 100755 --- a/script/fmt +++ b/script/fmt @@ -1,8 +1,8 @@ -#!/usr/bin/env bash +#!/bin/sh # script/fmt: format all files (writes). -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT" diff --git a/script/fmt-check b/script/fmt-check index b9bce49..d089ccf 100755 --- a/script/fmt-check +++ b/script/fmt-check @@ -1,8 +1,8 @@ -#!/usr/bin/env bash +#!/bin/sh # script/fmt-check: check formatting (read-only). -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT" diff --git a/script/install-precommit b/script/install-precommit index 9eb0c0d..bef6406 100755 --- a/script/install-precommit +++ b/script/install-precommit @@ -1,15 +1,15 @@ -#!/usr/bin/env bash +#!/bin/sh # script/install-precommit: install the git pre-commit hook that runs # script/precommit. Our own extension to scripts-to-rule-them-all. -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT" - local hook=".git/hooks/pre-commit" - printf '#!/bin/sh\nset -e\nscript/precommit\n' > "$hook" - chmod +x "$hook" + hook=".git/hooks/pre-commit" + printf '#!/bin/sh\nset -e\nscript/precommit\n' > .git/hooks/pre-commit + chmod +x .git/hooks/pre-commit echo "pre-commit hook installed: runs script/precommit" } diff --git a/script/lint b/script/lint index 4f4f080..c489634 100755 --- a/script/lint +++ b/script/lint @@ -1,8 +1,8 @@ -#!/usr/bin/env bash +#!/bin/sh # script/lint: run the linter. -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT" diff --git a/script/precommit b/script/precommit index 0096fc5..c0a7867 100755 --- a/script/precommit +++ b/script/precommit @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh # script/precommit: run by the git pre-commit hook; fails the commit if # checks fail. Our own extension to scripts-to-rule-them-all. -set -euo pipefail +set -eu -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" main() { "$SCRIPT_DIR/check" diff --git a/script/projectname b/script/projectname index 90516e0..8f277cc 100755 --- a/script/projectname +++ b/script/projectname @@ -1,9 +1,9 @@ -#!/usr/bin/env bash +#!/bin/sh # script/projectname: output the name of this project. Our own # extension to scripts-to-rule-them-all. Other scripts that need the # name (e.g. script/docker) call this, so they can stay identical # across all repos. -set -euo pipefail +set -eu main() { echo "prompts" diff --git a/script/setup b/script/setup new file mode 100755 index 0000000..4cc5b6b --- /dev/null +++ b/script/setup @@ -0,0 +1,13 @@ +#!/bin/sh +# script/setup: set up the repo for development after a fresh clone: +# installs dependencies and the git pre-commit hook. +set -eu + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" + +main() { + "$SCRIPT_DIR/bootstrap" + "$SCRIPT_DIR/install-precommit" +} + +main "$@" diff --git a/script/test b/script/test index 9a2a41c..cbfda0e 100755 --- a/script/test +++ b/script/test @@ -1,8 +1,8 @@ -#!/usr/bin/env bash +#!/bin/sh # script/test: run the test suite. -set -euo pipefail +set -eu -ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" main() { cd "$ROOT"