sync-to-codex-plugin.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. "defaultPrompt": [
  122. "I've got an idea for something I'd like to build.",
  123. "Let's add a feature to this project."
  124. ],
  125. "brandColor": "#F59E0B",
  126. "composerIcon": "./assets/superpowers-small.svg",
  127. "logo": "./assets/app-icon.png",
  128. "screenshots": []
  129. }
  130. }
  131. EOF
  132. }
  133. # =============================================================================
  134. # Args
  135. # =============================================================================
  136. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  137. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  138. BASE="$DEFAULT_BASE"
  139. DRY_RUN=0
  140. YES=0
  141. LOCAL_CHECKOUT=""
  142. BOOTSTRAP=0
  143. ASSETS_SRC=""
  144. usage() {
  145. sed -n 's/^# \{0,1\}//;2,27p' "$0"
  146. exit "${1:-0}"
  147. }
  148. while [[ $# -gt 0 ]]; do
  149. case "$1" in
  150. -n|--dry-run) DRY_RUN=1; shift ;;
  151. -y|--yes) YES=1; shift ;;
  152. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  153. --base) BASE="$2"; shift 2 ;;
  154. --bootstrap) BOOTSTRAP=1; shift ;;
  155. --assets-src) ASSETS_SRC="$2"; shift 2 ;;
  156. -h|--help) usage 0 ;;
  157. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  158. esac
  159. done
  160. # =============================================================================
  161. # Preflight
  162. # =============================================================================
  163. die() { echo "ERROR: $*" >&2; exit 1; }
  164. command -v rsync >/dev/null || die "rsync not found in PATH"
  165. command -v git >/dev/null || die "git not found in PATH"
  166. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  167. command -v python3 >/dev/null || die "python3 not found in PATH"
  168. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  169. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  170. [[ -f "$UPSTREAM/package.json" ]] || die "upstream has no package.json — cannot read version"
  171. # Bootstrap-mode validation
  172. if [[ $BOOTSTRAP -eq 1 ]]; then
  173. [[ -n "$ASSETS_SRC" ]] || die "--bootstrap requires --assets-src <path>"
  174. ASSETS_SRC="$(cd "$ASSETS_SRC" 2>/dev/null && pwd)" || die "assets source '$ASSETS_SRC' is not a directory"
  175. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.svg" ]] || die "assets source missing PrimeRadiant_Favicon.svg"
  176. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.png" ]] || die "assets source missing PrimeRadiant_Favicon.png"
  177. fi
  178. # Read the upstream version from package.json
  179. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/package.json")"
  180. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from upstream package.json"
  181. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  182. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  183. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  184. confirm() {
  185. [[ $YES -eq 1 ]] && return 0
  186. read -rp "$1 [y/N] " ans
  187. [[ "$ans" == "y" || "$ans" == "Y" ]]
  188. }
  189. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  190. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  191. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  192. fi
  193. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  194. if [[ -n "$UPSTREAM_STATUS" ]]; then
  195. echo "WARNING: upstream has uncommitted changes:"
  196. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  197. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  198. confirm "Continue anyway?" || exit 1
  199. fi
  200. # =============================================================================
  201. # Prepare destination (clone fork fresh, or use --local)
  202. # =============================================================================
  203. CLEANUP_DIR=""
  204. cleanup() {
  205. [[ -n "$CLEANUP_DIR" ]] && rm -rf "$CLEANUP_DIR"
  206. }
  207. trap cleanup EXIT
  208. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  209. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  210. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  211. else
  212. echo "Cloning $FORK..."
  213. CLEANUP_DIR="$(mktemp -d)"
  214. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  215. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  216. fi
  217. DEST="$DEST_REPO/$DEST_REL"
  218. # Checkout base branch
  219. cd "$DEST_REPO"
  220. git checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  221. # Plugin-existence check depends on mode
  222. if [[ $BOOTSTRAP -eq 1 ]]; then
  223. [[ ! -d "$DEST" ]] || die "--bootstrap but base branch '$BASE' already has '$DEST_REL/' — use normal sync instead"
  224. mkdir -p "$DEST"
  225. else
  226. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — use --bootstrap + --assets-src, or pass --base <branch>"
  227. fi
  228. # =============================================================================
  229. # Create sync branch
  230. # =============================================================================
  231. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  232. if [[ $BOOTSTRAP -eq 1 ]]; then
  233. SYNC_BRANCH="bootstrap/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  234. else
  235. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  236. fi
  237. git checkout -q -b "$SYNC_BRANCH"
  238. # =============================================================================
  239. # Build rsync args
  240. # =============================================================================
  241. RSYNC_ARGS=(-av --delete)
  242. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  243. # =============================================================================
  244. # Dry run preview (always shown)
  245. # =============================================================================
  246. echo ""
  247. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  248. echo "Version: $UPSTREAM_VERSION"
  249. echo "Fork: $FORK"
  250. echo "Base: $BASE"
  251. echo "Branch: $SYNC_BRANCH"
  252. if [[ $BOOTSTRAP -eq 1 ]]; then
  253. echo "Mode: BOOTSTRAP (creating initial plugin from scratch)"
  254. echo "Assets: $ASSETS_SRC"
  255. fi
  256. echo ""
  257. echo "=== Preview (rsync --dry-run) ==="
  258. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  259. echo "=== End preview ==="
  260. echo ""
  261. echo "Overlay file (.codex-plugin/plugin.json) will be regenerated with"
  262. echo "version $UPSTREAM_VERSION regardless of rsync output."
  263. if [[ $BOOTSTRAP -eq 1 ]]; then
  264. echo "Assets (superpowers-small.svg, app-icon.png) will be seeded from:"
  265. echo " $ASSETS_SRC"
  266. fi
  267. if [[ $DRY_RUN -eq 1 ]]; then
  268. echo ""
  269. echo "Dry run only. Nothing was changed or pushed."
  270. exit 0
  271. fi
  272. # =============================================================================
  273. # Apply
  274. # =============================================================================
  275. echo ""
  276. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  277. echo ""
  278. echo "Syncing upstream content..."
  279. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  280. if [[ $BOOTSTRAP -eq 1 ]]; then
  281. echo "Seeding brand assets..."
  282. mkdir -p "$DEST/assets"
  283. cp "$ASSETS_SRC/PrimeRadiant_Favicon.svg" "$DEST/assets/superpowers-small.svg"
  284. cp "$ASSETS_SRC/PrimeRadiant_Favicon.png" "$DEST/assets/app-icon.png"
  285. fi
  286. echo "Regenerating overlay file..."
  287. generate_plugin_json "$DEST/.codex-plugin/plugin.json" "$UPSTREAM_VERSION"
  288. # Bail early if nothing actually changed
  289. cd "$DEST_REPO"
  290. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  291. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  292. exit 0
  293. fi
  294. # =============================================================================
  295. # Commit, push, open PR
  296. # =============================================================================
  297. git add "$DEST_REL"
  298. if [[ $BOOTSTRAP -eq 1 ]]; then
  299. COMMIT_TITLE="bootstrap superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  300. PR_BODY="Initial bootstrap of the superpowers plugin from upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  301. Creates \`plugins/superpowers/\` from scratch: upstream content via rsync, \`.codex-plugin/plugin.json\` regenerated inline, brand assets seeded from a local Brand Assets directory.
  302. Run via: \`scripts/sync-to-codex-plugin.sh --bootstrap --assets-src <path>\`
  303. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  304. This is a one-time bootstrap. Subsequent syncs will be normal (non-bootstrap) runs and will not touch the \`assets/\` directory."
  305. else
  306. COMMIT_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  307. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  308. Run via: \`scripts/sync-to-codex-plugin.sh\`
  309. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  310. 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."
  311. fi
  312. git commit --quiet -m "$COMMIT_TITLE
  313. Automated sync via scripts/sync-to-codex-plugin.sh
  314. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  315. Branch: $SYNC_BRANCH"
  316. echo "Pushing $SYNC_BRANCH to $FORK..."
  317. git push -u origin "$SYNC_BRANCH" --quiet
  318. echo "Opening PR..."
  319. PR_URL="$(gh pr create \
  320. --repo "$FORK" \
  321. --base "$BASE" \
  322. --head "$SYNC_BRANCH" \
  323. --title "$COMMIT_TITLE" \
  324. --body "$PR_BODY")"
  325. PR_NUM="${PR_URL##*/}"
  326. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  327. echo ""
  328. echo "PR opened: $PR_URL"
  329. echo "Diff view: $DIFF_URL"