diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e9ba518..f28846c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -9,3 +9,5 @@ jobs: permissions: contents: write # for the nested badges job; the nested check job lowers it to read uses: rjstone/plugin-ci/.github/workflows/check.yml@main + with: + template-repo: rjstone/harness-plugin-template # drift report; see AGENTS.md diff --git a/AGENTS.md b/AGENTS.md index cd096c5..acfbbdf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -90,7 +90,7 @@ server as `docshelf`: `python3 {root}/skills/docshelf/scripts/docshelf.py --mcp` This repo was made from `rjstone/harness-plugin-template`; everything below the marker is that template's reference. - + ## Making a plugin from the template 1. Create a repo from the template (rjstone/harness-plugin-template) and name it @@ -106,10 +106,7 @@ template's reference. 6. Update `README.md` (install table, owner/repo, and the badge row's owner/repo). 7. Put the repo's own checks in `scripts/ci.sh` (see "Checks"). It starts with the template's three; add yours below them. -8. In a repo just created from the template, delete the template's own files under - `docs/superpowers/` (its shared-CI spec and plans). A plugin's own specs and plans may live - there. -9. In the repo's Actions settings, set the variable `MARKETPLACE_APP_CLIENT_ID` and the secret +8. In the repo's Actions settings, set the variable `MARKETPLACE_APP_CLIENT_ID` and the secret `MARKETPLACE_APP_PRIVATE_KEY`, or delete `.github/workflows/notify-marketplace.yml` if the plugin isn't listed in `rjstone/plugin-marketplace` (the drift list marks that file optional, so a deleted copy stays deleted). @@ -188,10 +185,9 @@ the plugin uses it; **doc** = described here only, not in the template. ├── cline/ doc Cline plugin (package.json "cline" key) ├── amp/ doc Amp plugin module (/index.ts) ├── scripts/ core the generator (build.ts, generate.ts, types.ts, frontmatter.ts), -│ ci.sh (the repo's own checks), template-drift.sh and -│ template-files.txt (see "Staying in step with the template") -├── tests/ core build.test.ts (generator tests) and template-drift.test.ts -│ (node:test, throwaway folders, no network) +│ ci.sh (the repo's own checks) +├── tests/ core build.test.ts (generator tests; node:test, throwaway +│ folders, no network) ├── .github/workflows/ three thin callers of rjstone/plugin-ci's reusable workflows │ ├── check.yml core scripts/ci.sh, claude plugin validate, test counts; badges job │ ├── release.yml core GitHub Release from git-cliff notes on a vX.Y.Z tag @@ -429,17 +425,21 @@ workflows. Pass event values to scripts through `env:`, never by `${{ }}` inside ## Staying in step with the template A plugin made from the template keeps some files identical to the template's: the generator -modules, its tests, `tsconfig.json`, the three workflow callers, `CLAUDE.md`, the drift script -and its list, and this file from the `template-reference` marker to the end. -`scripts/template-files.txt` names them (one path per line; `path :: MARKER` means from the -first line containing MARKER to the end of the file; `? path` marks a whole file optional: it -is skipped while the plugin has no copy, and compared and applied once it exists). - -- `bash scripts/template-drift.sh` fetches the template's `main` and compares each listed file. - In CI it runs after validation and prints one warning per differing file; it never - fails the run. It does nothing in the template itself. -- `bash scripts/template-drift.sh --apply` copies the template's versions over the local ones. - Review the diff and commit it. +modules, their test, `tsconfig.json`, the three workflow callers, `CLAUDE.md`, and this file +from the `template-reference` marker to the end. The list and the script that compares them are +not in the plugin: they live on the template's `maintenance` branch (`scripts/template-files.txt` +and `scripts/template-drift.sh`). + +- In CI, `check.yml` passes `template-repo` to the shared workflow, which fetches that branch + and runs the script after validation. It prints one warning per differing file and never fails + a plugin's run. A plugin that should stop following the template removes that input. +- To pull the template's versions in, run this from the plugin repo's root, then review the diff + and commit it (leave out `--apply` for a report only): + + ```sh + d=$(mktemp -d) && git clone -q --depth 1 -b maintenance https://github.com/rjstone/harness-plugin-template "$d" && bash "$d/scripts/template-drift.sh" --apply + ``` + - Fix a shared file in the template first, then apply it in each plugin repo. A fix made only in a plugin repo is overwritten by the next `--apply`. diff --git a/scripts/template-drift.sh b/scripts/template-drift.sh deleted file mode 100644 index 7cf3bff..0000000 --- a/scripts/template-drift.sh +++ /dev/null @@ -1,213 +0,0 @@ -#!/usr/bin/env bash -# shellcheck disable=SC2317 # the last line is a safety net: main exits by itself -# Report (or, with --apply, fix) drift between this plugin repo and the template it was made from. -# Usage: bash scripts/template-drift.sh [--apply] [TEMPLATE_DIR] (run from the plugin repo's root) -# The list of files is the template's scripts/template-files.txt. A line is "PATH" (the whole -# file) or "PATH :: MARKER" (from the first line containing MARKER to the end of the file). -# A "?" before a whole-file PATH marks it optional: skipped while the plugin has no such file. -# Report mode always exits 0, so CI can run it as a non-blocking step. -set -u - -# Everything is inside main, and the last line runs it, so bash has parsed the whole script -# before --apply can replace this file. -main() { - - TEMPLATE_REPO="rjstone/harness-plugin-template" - TEMPLATE_URL="https://github.com/$TEMPLATE_REPO" - CLONE_URL="${TEMPLATE_DRIFT_URL:-$TEMPLATE_URL}" - - apply=0 - template_dir="" - for arg in "$@"; do - case "$arg" in - --apply) apply=1 ;; - -*) echo "usage: template-drift.sh [--apply] [TEMPLATE_DIR]" >&2; exit 2 ;; - *) template_dir="$arg" ;; - esac - done - - strip_url() { - local u="${1%/}" - u="${u%.git}" - printf '%s' "${u%/}" - } - - repository="" - if [ -f plugin.config.json ]; then - repository="$(node -e 'try{const r=JSON.parse(require("fs").readFileSync("plugin.config.json","utf8")).repository;process.stdout.write(typeof r==="string"?r:"")}catch{}' 2>/dev/null)" - fi - if [ -n "$repository" ] && [ "$(strip_url "$repository")" = "$(strip_url "$TEMPLATE_URL")" ]; then - echo "template-drift: this is the template; nothing to compare" - exit 0 - fi - - if [ -z "$template_dir" ]; then - tmp="$(mktemp -d)" - trap 'rm -rf "$tmp"' EXIT - if ! git clone --depth 1 --quiet "$CLONE_URL" "$tmp/template" >/dev/null 2>&1; then - echo "template-drift: could not fetch the template" - [ "$apply" -eq 1 ] && exit 1 - exit 0 - fi - template_dir="$tmp/template" - fi - - list="$template_dir/scripts/template-files.txt" - if [ ! -f "$list" ]; then - echo "template-drift: could not fetch the template" - [ "$apply" -eq 1 ] && exit 1 - exit 0 - fi - - trim() { - local s="$1" - s="${s#"${s%%[![:space:]]*}"}" - s="${s%"${s##*[![:space:]]}"}" - printf '%s' "$s" - } - - paths=() - markers=() - optionals=() - while IFS= read -r line || [ -n "$line" ]; do - line="$(trim "$line")" - case "$line" in '' | '#'*) continue ;; esac - optional=0 - if [ "${line:0:1}" = "?" ]; then - optional=1 - line="$(trim "${line:1}")" - fi - path="$line" - marker="" - if [[ "$line" == *' :: '* ]]; then - path="$(trim "${line%% :: *}")" - marker="$(trim "${line#* :: }")" - fi - case "$path" in - /* | *..*) - echo "template-files.txt: bad path \"$path\"" >&2 - exit 1 - ;; - esac - paths+=("$path") - markers+=("$marker") - optionals+=("$optional") - done <"$list" - - total=${#paths[@]} - differ=0 - applied=0 - failed=0 - - # Print a finding: a plain line, or a GitHub Actions warning annotation. - report() { # path text - if [ "${GITHUB_ACTIONS:-}" = "true" ]; then - echo "::warning file=$1::$2 ($TEMPLATE_REPO)" - else - echo "$2" - fi - } - - # Line number of the first line of FILE containing MARKER (empty if none). - marker_line() { - grep -nF -m1 -e "$2" "$1" 2>/dev/null | head -n1 | cut -d: -f1 - } - - # Write stdin to PATH through a temporary file in the same directory, then move it into place, - # so a running script is never overwritten in place (bash reads a script as it runs). - put_file() { # path - local dir tmp - dir="$(dirname "$1")" - mkdir -p "$dir" || return 1 - [ -d "$1" ] && return 1 - tmp="$(mktemp "$dir/.drift.XXXXXX")" || return 1 - if [ -f "$1" ]; then cp -p "$1" "$tmp" 2>/dev/null; else chmod 644 "$tmp"; fi - if cat >"$tmp" && mv -f "$tmp" "$1"; then return 0; fi - rm -f "$tmp" - return 1 - } - - # Apply: write stdin to PATH and say so, or say it failed. - apply_file() { # path note - if put_file "$1"; then - applied=$((applied + 1)) - echo "updated $1$2" - else - echo "$1: could not write" >&2 - failed=1 - fi - } - - for i in "${!paths[@]}"; do - path="${paths[$i]}" - marker="${markers[$i]}" - src="$template_dir/$path" - if [ "${optionals[$i]}" = 1 ] && [ -z "$marker" ] && [ ! -f "$path" ]; then - total=$((total - 1)) - continue - fi - if [ ! -f "$src" ]; then - differ=$((differ + 1)) - report "$path" "$path: not in the template" - [ "$apply" -eq 1 ] && failed=1 - continue - fi - - if [ -z "$marker" ]; then - if [ -f "$path" ] && cmp -s "$src" "$path"; then continue; fi - differ=$((differ + 1)) - if [ "$apply" -eq 1 ]; then - apply_file "$path" "" <"$src" - elif [ -f "$path" ]; then - report "$path" "$path differs from the template" - else - report "$path" "$path is missing" - fi - continue - fi - - tn="$(marker_line "$src" "$marker")" - if [ -z "$tn" ]; then - differ=$((differ + 1)) - report "$path" "$path: marker \"$marker\" not found in the template" - [ "$apply" -eq 1 ] && failed=1 - continue - fi - if [ ! -f "$path" ]; then - differ=$((differ + 1)) - if [ "$apply" -eq 1 ]; then - apply_file "$path" "" <"$src" - else - report "$path" "$path is missing" - fi - continue - fi - ln="$(marker_line "$path" "$marker")" - if [ -z "$ln" ]; then - differ=$((differ + 1)) - if [ "$apply" -eq 1 ]; then - echo "$path: marker \"$marker\" not found" >&2 - failed=1 - else - report "$path" "$path: marker \"$marker\" not found" - fi - continue - fi - if [ "$(tail -n +"$tn" "$src")" = "$(tail -n +"$ln" "$path")" ]; then continue; fi - differ=$((differ + 1)) - if [ "$apply" -eq 1 ]; then - apply_file "$path" " (below the marker)" < <(head -n "$((ln - 1))" "$path"; tail -n +"$tn" "$src") - else - report "$path" "$path differs from the template below the marker" - fi - done - - if [ "$apply" -eq 1 ]; then - echo "template-drift: applied $applied of $total" - exit "$failed" - fi - echo "template-drift: $differ of $total differ" - exit 0 -} - -main "$@"; exit $? diff --git a/scripts/template-files.txt b/scripts/template-files.txt deleted file mode 100644 index 16198b7..0000000 --- a/scripts/template-files.txt +++ /dev/null @@ -1,19 +0,0 @@ -# Files a plugin made from this template keeps identical to the template's. -# "path" = the whole file; "path :: MARKER" = from the first line containing MARKER to the end. -# "? path" = an optional whole file: skipped (not reported, not created) while the plugin has none; -# once it exists it is compared and applied like any other. Not supported on "path :: MARKER" lines. -# Report: bash scripts/template-drift.sh Pull the template's versions in: add --apply -scripts/build.ts -scripts/generate.ts -scripts/types.ts -scripts/frontmatter.ts -scripts/template-drift.sh -scripts/template-files.txt -tests/build.test.ts -tests/template-drift.test.ts -tsconfig.json -.github/workflows/check.yml -.github/workflows/release.yml -? .github/workflows/notify-marketplace.yml -CLAUDE.md -AGENTS.md :: template-reference: diff --git a/tests/template-drift.test.ts b/tests/template-drift.test.ts deleted file mode 100644 index cc2a4b5..0000000 --- a/tests/template-drift.test.ts +++ /dev/null @@ -1,233 +0,0 @@ -// Drift script tests: throwaway template and plugin folders in the temp directory (no network). -import { test } from 'node:test'; -import assert from 'node:assert/strict'; -import { spawnSync } from 'node:child_process'; -import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; -import { tmpdir } from 'node:os'; -import { dirname, join } from 'node:path'; - -const SCRIPT = join(import.meta.dirname, '..', 'scripts', 'template-drift.sh'); -const TEMPLATE_URL = 'https://github.com/rjstone/harness-plugin-template'; -const TEMPLATE_AGENTS = '# Template\n\nshared\n'; - -function put(root: string, path: string, content: string) { - mkdirSync(dirname(join(root, path)), { recursive: true }); - writeFileSync(join(root, path), content); -} - -const read = (root: string, p: string) => readFileSync(join(root, p), 'utf8'); - -// A template folder and a plugin folder holding identical copies, except that the plugin's -// AGENTS.md has its own first line. -function pair(repository = 'https://github.com/someone/demo-plugin') { - const base = mkdtempSync(join(tmpdir(), 'hpt-drift-')); - const template = join(base, 'template'); - const plugin = join(base, 'plugin'); - put(template, 'plugin.config.json', JSON.stringify({ repository: TEMPLATE_URL })); - put(template, 'scripts/template-files.txt', - '# comment\n\na.txt\nsub/b.txt\nAGENTS.md :: template-reference:\n'); - put(plugin, 'plugin.config.json', JSON.stringify({ repository })); - for (const root of [template, plugin]) { - put(root, 'a.txt', 'alpha\n'); - put(root, 'sub/b.txt', 'beta\n'); - } - put(template, 'AGENTS.md', TEMPLATE_AGENTS); - put(plugin, 'AGENTS.md', '# Mine\n\nshared\n'); - return { base, template, plugin }; -} - -function drift(plugin: string, args: string[], env: Record = {}) { - const r = spawnSync('bash', [SCRIPT, ...args], { - cwd: plugin, encoding: 'utf8', - env: { ...process.env, GITHUB_ACTIONS: '', ...env }, - }); - return { status: r.status, stdout: r.stdout, stderr: r.stderr }; -} - -test('identical files: nothing to report', () => { - const { base, template, plugin } = pair(); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /template-drift: 0 of 3 differ/); - rmSync(base, { recursive: true }); -}); - -test('a changed file is reported and the exit code stays 0', () => { - const { base, template, plugin } = pair(); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /^a\.txt differs from the template$/m); - assert.match(r.stdout, /1 of 3 differ/); - rmSync(base, { recursive: true }); -}); - -test('in CI the report is a warning annotation', () => { - const { base, template, plugin } = pair(); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, [template], { GITHUB_ACTIONS: 'true' }); - assert.equal(r.status, 0); - assert.match(r.stdout, - /^::warning file=a\.txt::a\.txt differs from the template \(rjstone\/harness-plugin-template\)$/m); - rmSync(base, { recursive: true }); -}); - -test('a file the plugin lacks is reported', () => { - const { base, template, plugin } = pair(); - rmSync(join(plugin, 'sub/b.txt')); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /^sub\/b\.txt is missing$/m); - rmSync(base, { recursive: true }); -}); - -test('above the marker is the plugin\'s own', () => { - const { base, template, plugin } = pair(); - const r = drift(plugin, [template]); - assert.match(r.stdout, /0 of 3 differ/); - rmSync(base, { recursive: true }); -}); - -test('below the marker is compared', () => { - const { base, template, plugin } = pair(); - put(plugin, 'AGENTS.md', '# Mine\n\nlocal edit\n'); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /^AGENTS\.md differs from the template below the marker$/m); - rmSync(base, { recursive: true }); -}); - -test('--apply copies files and replaces only what is below the marker', () => { - const { base, template, plugin } = pair(); - put(plugin, 'a.txt', 'changed\n'); - put(plugin, 'AGENTS.md', '# Mine\n\nlocal edit\n'); - const r = drift(plugin, ['--apply', template]); - assert.equal(r.status, 0); - assert.equal(read(plugin, 'a.txt'), 'alpha\n'); - assert.equal(read(plugin, 'AGENTS.md'), '# Mine\n\nshared\n'); - const again = drift(plugin, [template]); - assert.match(again.stdout, /0 of 3 differ/); - rmSync(base, { recursive: true }); -}); - -test('--apply leaves a file without the marker alone and exits 1', () => { - const { base, template, plugin } = pair(); - const own = '# Mine\nno marker here\n'; - put(plugin, 'AGENTS.md', own); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, ['--apply', template]); - assert.equal(r.status, 1); - assert.match(r.stderr, /AGENTS\.md: marker "template-reference:" not found/); - assert.equal(read(plugin, 'AGENTS.md'), own); - assert.equal(read(plugin, 'a.txt'), 'alpha\n'); - rmSync(base, { recursive: true }); -}); - -test('report mode says when the marker is missing', () => { - const { base, template, plugin } = pair(); - put(plugin, 'AGENTS.md', '# Mine\nno marker here\n'); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /^AGENTS\.md: marker "template-reference:" not found$/m); - rmSync(base, { recursive: true }); -}); - -test('a list entry outside the repo is refused', () => { - const { base, template, plugin } = pair(); - put(template, 'scripts/template-files.txt', 'a.txt\n../evil.txt\n'); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, ['--apply', template]); - assert.equal(r.status, 1); - assert.match(r.stderr, /template-files\.txt: bad path "\.\.\/evil\.txt"/); - assert.equal(read(plugin, 'a.txt'), 'changed\n'); - rmSync(base, { recursive: true }); -}); - -test('does nothing in the template itself', () => { - const { base, template, plugin } = pair(TEMPLATE_URL + '.git'); - put(plugin, 'a.txt', 'changed\n'); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.match(r.stdout, /this is the template; nothing to compare/); - rmSync(base, { recursive: true }); -}); - -test('a template that can\'t be fetched is a note, not a failure', () => { - const { base, plugin } = pair(); - const env = { TEMPLATE_DRIFT_URL: 'file:///nonexistent' }; - const r = drift(plugin, [], env); - assert.equal(r.status, 0); - assert.match(r.stdout, /could not fetch the template/); - assert.equal(drift(plugin, ['--apply'], env).status, 1); - rmSync(base, { recursive: true }); -}); - -test('--apply can replace the drift script it is running', () => { - const { base, template, plugin } = pair(); - const real = readFileSync(SCRIPT, 'utf8'); - put(plugin, 'scripts/template-drift.sh', real); - put(template, 'scripts/template-drift.sh', '# extra line 1\n# extra line 2\n# extra line 3\n' + real); - put(template, 'scripts/template-files.txt', 'scripts/template-drift.sh\n'); - const r = spawnSync('bash', [join(plugin, 'scripts/template-drift.sh'), '--apply', template], { - cwd: plugin, encoding: 'utf8', env: { ...process.env, GITHUB_ACTIONS: '' }, - }); - assert.doesNotMatch(r.stderr, /syntax error/); - assert.equal(r.status, 0); - assert.equal(read(plugin, 'scripts/template-drift.sh'), read(template, 'scripts/template-drift.sh')); - assert.match(r.stdout, /template-drift: applied 1 of 1$/m); - rmSync(base, { recursive: true }); -}); - -test('an optional entry that is missing locally is silent and not created', () => { - const { base, template, plugin } = pair(); - put(template, 'scripts/template-files.txt', 'a.txt\n? opt.txt\n'); - put(template, 'opt.txt', 'optional\n'); - const r = drift(plugin, [template]); - assert.equal(r.status, 0); - assert.doesNotMatch(r.stdout, /opt\.txt/); - assert.match(r.stdout, /0 of 1 differ/); - const a = drift(plugin, ['--apply', template]); - assert.equal(a.status, 0); - assert.equal(existsSync(join(plugin, 'opt.txt')), false); - rmSync(base, { recursive: true }); -}); - -test('an optional entry that exists and differs is reported and applied', () => { - const { base, template, plugin } = pair(); - put(template, 'scripts/template-files.txt', 'a.txt\n? opt.txt\n'); - put(template, 'opt.txt', 'optional\n'); - put(plugin, 'opt.txt', 'old\n'); - const r = drift(plugin, [template]); - assert.match(r.stdout, /^opt\.txt differs from the template$/m); - assert.match(r.stdout, /1 of 2 differ/); - drift(plugin, ['--apply', template]); - assert.equal(read(plugin, 'opt.txt'), 'optional\n'); - rmSync(base, { recursive: true }); -}); - -test('an entry missing from the template is exit 1 with --apply, exit 0 in report mode', () => { - const { base, template, plugin } = pair(); - put(template, 'scripts/template-files.txt', 'a.txt\nnot-there.txt\n'); - put(plugin, 'a.txt', 'changed\n'); - assert.equal(drift(plugin, [template]).status, 0); - const r = drift(plugin, ['--apply', template]); - assert.equal(r.status, 1); - assert.match(r.stdout, /not-there\.txt: not in the template/); - assert.match(r.stdout, /applied 1 of 2/); - rmSync(base, { recursive: true }); -}); - -test('--apply that cannot write says failed, not updated, and exits 1', () => { - const { base, template, plugin } = pair(); - put(plugin, 'a.txt', 'changed\n'); - // A directory where the file should go makes the move fail. - rmSync(join(plugin, 'sub/b.txt')); - mkdirSync(join(plugin, 'sub/b.txt')); - const r = drift(plugin, ['--apply', template]); - assert.equal(r.status, 1); - assert.doesNotMatch(r.stdout, /updated sub\/b\.txt/); - assert.match(r.stderr, /sub\/b\.txt: could not write/); - assert.equal(read(plugin, 'a.txt'), 'alpha\n'); - rmSync(base, { recursive: true }); -});