sync-to-codex-plugin.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/usr/bin/env bash
  2. #
  3. # sync-to-codex-plugin.sh
  4. #
  5. # Sync this superpowers checkout → prime-radiant-inc/openai-codex-plugins.
  6. # Clones the fork fresh into a temp dir, rsyncs upstream content, regenerates
  7. # the Codex overlay file (.codex-plugin/plugin.json) inline, commits, pushes a
  8. # sync branch, and opens a PR.
  9. # Path/user agnostic — auto-detects upstream from script location.
  10. #
  11. # Deterministic: running twice against the same upstream SHA produces PRs with
  12. # identical diffs, so two back-to-back runs can verify the tool itself.
  13. #
  14. # Usage:
  15. # ./scripts/sync-to-codex-plugin.sh # full run
  16. # ./scripts/sync-to-codex-plugin.sh -n # dry run
  17. # ./scripts/sync-to-codex-plugin.sh -y # skip confirm
  18. # ./scripts/sync-to-codex-plugin.sh --local PATH # existing checkout
  19. # ./scripts/sync-to-codex-plugin.sh --base BRANCH # default: main
  20. # ./scripts/sync-to-codex-plugin.sh --bootstrap --assets-src DIR # create initial plugin
  21. #
  22. # Bootstrap mode: skips the "plugin must exist on base" check and seeds
  23. # plugins/superpowers/assets/ from --assets-src <dir> which must contain
  24. # PrimeRadiant_Favicon.svg and PrimeRadiant_Favicon.png. Run once by one
  25. # team member to create the initial PR; every subsequent run is a normal
  26. # (non-bootstrap) sync.
  27. #
  28. # Requires: bash, rsync, git, gh (authenticated), python3.
  29. set -euo pipefail
  30. # =============================================================================
  31. # Config — edit as upstream or canonical plugin shape evolves
  32. # =============================================================================
  33. FORK="prime-radiant-inc/openai-codex-plugins"
  34. DEFAULT_BASE="main"
  35. DEST_REL="plugins/superpowers"
  36. # Paths in upstream that should NOT land in the embedded plugin.
  37. # The Codex-only paths are here too — they're managed by generate/bootstrap
  38. # steps, not by rsync.
  39. #
  40. # All patterns use a leading "/" to anchor them to the source root.
  41. # Unanchored patterns like "scripts/" would match any directory named
  42. # "scripts" at any depth — including legitimate nested dirs like
  43. # skills/brainstorming/scripts/. Anchoring prevents that.
  44. # (.DS_Store is intentionally unanchored — Finder creates them everywhere.)
  45. EXCLUDES=(
  46. # Dotfiles and infra — top-level only
  47. "/.claude/"
  48. "/.claude-plugin/"
  49. "/.codex/"
  50. "/.cursor-plugin/"
  51. "/.git/"
  52. "/.gitattributes"
  53. "/.github/"
  54. "/.gitignore"
  55. "/.opencode/"
  56. "/.version-bump.json"
  57. "/.worktrees/"
  58. ".DS_Store"
  59. # Root ceremony files
  60. "/AGENTS.md"
  61. "/CHANGELOG.md"
  62. "/CLAUDE.md"
  63. "/GEMINI.md"
  64. "/RELEASE-NOTES.md"
  65. "/gemini-extension.json"
  66. "/package.json"
  67. # Directories not shipped by canonical Codex plugins
  68. "/commands/"
  69. "/docs/"
  70. "/hooks/"
  71. "/lib/"
  72. "/scripts/"
  73. "/tests/"
  74. "/tmp/"
  75. # Codex-only paths — managed outside rsync
  76. "/.codex-plugin/"
  77. "/assets/"
  78. )
  79. # =============================================================================
  80. # Generated overlay file
  81. # =============================================================================
  82. # Writes the Codex plugin manifest to "$1" with the given upstream version.
  83. # Args: dest_path, version
  84. generate_plugin_json() {
  85. local dest="$1"
  86. local version="$2"
  87. mkdir -p "$(dirname "$dest")"
  88. cat > "$dest" <<EOF
  89. {
  90. "name": "superpowers",
  91. "version": "$version",
  92. "description": "Core skills library for Codex: planning, TDD, debugging, and collaboration workflows.",
  93. "author": {
  94. "name": "Jesse Vincent",
  95. "email": "jesse@fsck.com",
  96. "url": "https://github.com/obra"
  97. },
  98. "homepage": "https://github.com/obra/superpowers",
  99. "repository": "https://github.com/obra/superpowers",
  100. "license": "MIT",
  101. "keywords": [
  102. "skills",
  103. "planning",
  104. "tdd",
  105. "debugging",
  106. "code-review",
  107. "workflow"
  108. ],
  109. "skills": "./skills/",
  110. "interface": {
  111. "displayName": "Superpowers",
  112. "shortDescription": "Planning, TDD, debugging, and delivery workflows for coding agents",
  113. "longDescription": "Use Superpowers to guide agent work through brainstorming, implementation planning, test-driven development, systematic debugging, parallel execution, code review, and finish-the-branch workflows adapted for Codex.",
  114. "developerName": "Jesse Vincent",
  115. "category": "Coding",
  116. "capabilities": [
  117. "Interactive",
  118. "Read",
  119. "Write"
  120. ],
  121. "brandColor": "#F59E0B",
  122. "composerIcon": "./assets/superpowers-small.svg",
  123. "logo": "./assets/app-icon.png",
  124. "screenshots": []
  125. }
  126. }
  127. EOF
  128. }
  129. # =============================================================================
  130. # Args
  131. # =============================================================================
  132. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  133. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  134. BASE="$DEFAULT_BASE"
  135. DRY_RUN=0
  136. YES=0
  137. LOCAL_CHECKOUT=""
  138. BOOTSTRAP=0
  139. ASSETS_SRC=""
  140. usage() {
  141. sed -n 's/^# \{0,1\}//;2,27p' "$0"
  142. exit "${1:-0}"
  143. }
  144. while [[ $# -gt 0 ]]; do
  145. case "$1" in
  146. -n|--dry-run) DRY_RUN=1; shift ;;
  147. -y|--yes) YES=1; shift ;;
  148. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  149. --base) BASE="$2"; shift 2 ;;
  150. --bootstrap) BOOTSTRAP=1; shift ;;
  151. --assets-src) ASSETS_SRC="$2"; shift 2 ;;
  152. -h|--help) usage 0 ;;
  153. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  154. esac
  155. done
  156. # =============================================================================
  157. # Preflight
  158. # =============================================================================
  159. die() { echo "ERROR: $*" >&2; exit 1; }
  160. command -v rsync >/dev/null || die "rsync not found in PATH"
  161. command -v git >/dev/null || die "git not found in PATH"
  162. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  163. command -v python3 >/dev/null || die "python3 not found in PATH"
  164. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  165. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  166. [[ -f "$UPSTREAM/package.json" ]] || die "upstream has no package.json — cannot read version"
  167. # Bootstrap-mode validation
  168. if [[ $BOOTSTRAP -eq 1 ]]; then
  169. [[ -n "$ASSETS_SRC" ]] || die "--bootstrap requires --assets-src <path>"
  170. ASSETS_SRC="$(cd "$ASSETS_SRC" 2>/dev/null && pwd)" || die "assets source '$ASSETS_SRC' is not a directory"
  171. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.svg" ]] || die "assets source missing PrimeRadiant_Favicon.svg"
  172. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.png" ]] || die "assets source missing PrimeRadiant_Favicon.png"
  173. fi
  174. # Read the upstream version from package.json
  175. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/package.json")"
  176. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from upstream package.json"
  177. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  178. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  179. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  180. confirm() {
  181. [[ $YES -eq 1 ]] && return 0
  182. read -rp "$1 [y/N] " ans
  183. [[ "$ans" == "y" || "$ans" == "Y" ]]
  184. }
  185. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  186. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  187. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  188. fi
  189. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  190. if [[ -n "$UPSTREAM_STATUS" ]]; then
  191. echo "WARNING: upstream has uncommitted changes:"
  192. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  193. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  194. confirm "Continue anyway?" || exit 1
  195. fi
  196. # =============================================================================
  197. # Prepare destination (clone fork fresh, or use --local)
  198. # =============================================================================
  199. CLEANUP_DIR=""
  200. cleanup() {
  201. [[ -n "$CLEANUP_DIR" ]] && rm -rf "$CLEANUP_DIR"
  202. }
  203. trap cleanup EXIT
  204. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  205. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  206. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  207. else
  208. echo "Cloning $FORK..."
  209. CLEANUP_DIR="$(mktemp -d)"
  210. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  211. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  212. fi
  213. DEST="$DEST_REPO/$DEST_REL"
  214. # Checkout base branch
  215. cd "$DEST_REPO"
  216. git checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  217. # Plugin-existence check depends on mode
  218. if [[ $BOOTSTRAP -eq 1 ]]; then
  219. [[ ! -d "$DEST" ]] || die "--bootstrap but base branch '$BASE' already has '$DEST_REL/' — use normal sync instead"
  220. mkdir -p "$DEST"
  221. else
  222. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — use --bootstrap + --assets-src, or pass --base <branch>"
  223. fi
  224. # =============================================================================
  225. # Create sync branch
  226. # =============================================================================
  227. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  228. if [[ $BOOTSTRAP -eq 1 ]]; then
  229. SYNC_BRANCH="bootstrap/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  230. else
  231. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  232. fi
  233. git checkout -q -b "$SYNC_BRANCH"
  234. # =============================================================================
  235. # Build rsync args
  236. # =============================================================================
  237. RSYNC_ARGS=(-av --delete)
  238. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  239. # =============================================================================
  240. # Dry run preview (always shown)
  241. # =============================================================================
  242. echo ""
  243. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  244. echo "Version: $UPSTREAM_VERSION"
  245. echo "Fork: $FORK"
  246. echo "Base: $BASE"
  247. echo "Branch: $SYNC_BRANCH"
  248. if [[ $BOOTSTRAP -eq 1 ]]; then
  249. echo "Mode: BOOTSTRAP (creating initial plugin from scratch)"
  250. echo "Assets: $ASSETS_SRC"
  251. fi
  252. echo ""
  253. echo "=== Preview (rsync --dry-run) ==="
  254. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  255. echo "=== End preview ==="
  256. echo ""
  257. echo "Overlay file (.codex-plugin/plugin.json) will be regenerated with"
  258. echo "version $UPSTREAM_VERSION regardless of rsync output."
  259. if [[ $BOOTSTRAP -eq 1 ]]; then
  260. echo "Assets (superpowers-small.svg, app-icon.png) will be seeded from:"
  261. echo " $ASSETS_SRC"
  262. fi
  263. if [[ $DRY_RUN -eq 1 ]]; then
  264. echo ""
  265. echo "Dry run only. Nothing was changed or pushed."
  266. exit 0
  267. fi
  268. # =============================================================================
  269. # Apply
  270. # =============================================================================
  271. echo ""
  272. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  273. echo ""
  274. echo "Syncing upstream content..."
  275. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  276. if [[ $BOOTSTRAP -eq 1 ]]; then
  277. echo "Seeding brand assets..."
  278. mkdir -p "$DEST/assets"
  279. cp "$ASSETS_SRC/PrimeRadiant_Favicon.svg" "$DEST/assets/superpowers-small.svg"
  280. cp "$ASSETS_SRC/PrimeRadiant_Favicon.png" "$DEST/assets/app-icon.png"
  281. fi
  282. echo "Regenerating overlay file..."
  283. generate_plugin_json "$DEST/.codex-plugin/plugin.json" "$UPSTREAM_VERSION"
  284. # Bail early if nothing actually changed
  285. cd "$DEST_REPO"
  286. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  287. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  288. exit 0
  289. fi
  290. # =============================================================================
  291. # Commit, push, open PR
  292. # =============================================================================
  293. git add "$DEST_REL"
  294. if [[ $BOOTSTRAP -eq 1 ]]; then
  295. COMMIT_TITLE="bootstrap superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  296. PR_BODY="Initial bootstrap of the superpowers plugin from upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  297. Creates \`plugins/superpowers/\` from scratch: upstream content via rsync, \`.codex-plugin/plugin.json\` regenerated inline, brand assets seeded from a local Brand Assets directory.
  298. Run via: \`scripts/sync-to-codex-plugin.sh --bootstrap --assets-src <path>\`
  299. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  300. This is a one-time bootstrap. Subsequent syncs will be normal (non-bootstrap) runs and will not touch the \`assets/\` directory."
  301. else
  302. COMMIT_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  303. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  304. Run via: \`scripts/sync-to-codex-plugin.sh\`
  305. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  306. Running the sync tool again against the same upstream SHA should produce a PR with an identical diff — use that to verify the tool is behaving."
  307. fi
  308. git commit --quiet -m "$COMMIT_TITLE
  309. Automated sync via scripts/sync-to-codex-plugin.sh
  310. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  311. Branch: $SYNC_BRANCH"
  312. echo "Pushing $SYNC_BRANCH to $FORK..."
  313. git push -u origin "$SYNC_BRANCH" --quiet
  314. echo "Opening PR..."
  315. PR_URL="$(gh pr create \
  316. --repo "$FORK" \
  317. --base "$BASE" \
  318. --head "$SYNC_BRANCH" \
  319. --title "$COMMIT_TITLE" \
  320. --body "$PR_BODY")"
  321. PR_NUM="${PR_URL##*/}"
  322. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  323. echo ""
  324. echo "PR opened: $PR_URL"
  325. echo "Diff view: $DIFF_URL"