sync-to-codex-plugin.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. EXCLUDES=(
  40. # Dotfiles and infra
  41. ".claude/"
  42. ".claude-plugin/"
  43. ".codex/"
  44. ".cursor-plugin/"
  45. ".git/"
  46. ".gitattributes"
  47. ".github/"
  48. ".gitignore"
  49. ".opencode/"
  50. ".version-bump.json"
  51. ".worktrees/"
  52. ".DS_Store"
  53. # Root ceremony files
  54. "AGENTS.md"
  55. "CHANGELOG.md"
  56. "CLAUDE.md"
  57. "GEMINI.md"
  58. "RELEASE-NOTES.md"
  59. "gemini-extension.json"
  60. "package.json"
  61. # Directories not shipped by canonical Codex plugins
  62. "commands/"
  63. "docs/"
  64. "hooks/"
  65. "lib/"
  66. "scripts/"
  67. "tests/"
  68. "tmp/"
  69. # Codex-only paths — managed outside rsync
  70. ".codex-plugin/"
  71. "assets/"
  72. )
  73. # =============================================================================
  74. # Generated overlay file
  75. # =============================================================================
  76. # Writes the Codex plugin manifest to "$1" with the given upstream version.
  77. # Args: dest_path, version
  78. generate_plugin_json() {
  79. local dest="$1"
  80. local version="$2"
  81. mkdir -p "$(dirname "$dest")"
  82. cat > "$dest" <<EOF
  83. {
  84. "name": "superpowers",
  85. "version": "$version",
  86. "description": "Core skills library for Codex: planning, TDD, debugging, and collaboration workflows.",
  87. "author": {
  88. "name": "Jesse Vincent",
  89. "email": "jesse@fsck.com",
  90. "url": "https://github.com/obra"
  91. },
  92. "homepage": "https://github.com/obra/superpowers",
  93. "repository": "https://github.com/obra/superpowers",
  94. "license": "MIT",
  95. "keywords": [
  96. "skills",
  97. "planning",
  98. "tdd",
  99. "debugging",
  100. "code-review",
  101. "workflow"
  102. ],
  103. "skills": "./skills/",
  104. "interface": {
  105. "displayName": "Superpowers",
  106. "shortDescription": "Planning, TDD, debugging, and delivery workflows for coding agents",
  107. "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.",
  108. "developerName": "Jesse Vincent",
  109. "category": "Coding",
  110. "capabilities": [
  111. "Interactive",
  112. "Read",
  113. "Write"
  114. ],
  115. "brandColor": "#F59E0B",
  116. "composerIcon": "./assets/superpowers-small.svg",
  117. "logo": "./assets/app-icon.png",
  118. "screenshots": []
  119. }
  120. }
  121. EOF
  122. }
  123. # =============================================================================
  124. # Args
  125. # =============================================================================
  126. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  127. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  128. BASE="$DEFAULT_BASE"
  129. DRY_RUN=0
  130. YES=0
  131. LOCAL_CHECKOUT=""
  132. BOOTSTRAP=0
  133. ASSETS_SRC=""
  134. usage() {
  135. sed -n 's/^# \{0,1\}//;2,27p' "$0"
  136. exit "${1:-0}"
  137. }
  138. while [[ $# -gt 0 ]]; do
  139. case "$1" in
  140. -n|--dry-run) DRY_RUN=1; shift ;;
  141. -y|--yes) YES=1; shift ;;
  142. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  143. --base) BASE="$2"; shift 2 ;;
  144. --bootstrap) BOOTSTRAP=1; shift ;;
  145. --assets-src) ASSETS_SRC="$2"; shift 2 ;;
  146. -h|--help) usage 0 ;;
  147. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  148. esac
  149. done
  150. # =============================================================================
  151. # Preflight
  152. # =============================================================================
  153. die() { echo "ERROR: $*" >&2; exit 1; }
  154. command -v rsync >/dev/null || die "rsync not found in PATH"
  155. command -v git >/dev/null || die "git not found in PATH"
  156. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  157. command -v python3 >/dev/null || die "python3 not found in PATH"
  158. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  159. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  160. [[ -f "$UPSTREAM/package.json" ]] || die "upstream has no package.json — cannot read version"
  161. # Bootstrap-mode validation
  162. if [[ $BOOTSTRAP -eq 1 ]]; then
  163. [[ -n "$ASSETS_SRC" ]] || die "--bootstrap requires --assets-src <path>"
  164. ASSETS_SRC="$(cd "$ASSETS_SRC" 2>/dev/null && pwd)" || die "assets source '$ASSETS_SRC' is not a directory"
  165. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.svg" ]] || die "assets source missing PrimeRadiant_Favicon.svg"
  166. [[ -f "$ASSETS_SRC/PrimeRadiant_Favicon.png" ]] || die "assets source missing PrimeRadiant_Favicon.png"
  167. fi
  168. # Read the upstream version from package.json
  169. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/package.json")"
  170. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from upstream package.json"
  171. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  172. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  173. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  174. confirm() {
  175. [[ $YES -eq 1 ]] && return 0
  176. read -rp "$1 [y/N] " ans
  177. [[ "$ans" == "y" || "$ans" == "Y" ]]
  178. }
  179. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  180. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  181. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  182. fi
  183. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  184. if [[ -n "$UPSTREAM_STATUS" ]]; then
  185. echo "WARNING: upstream has uncommitted changes:"
  186. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  187. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  188. confirm "Continue anyway?" || exit 1
  189. fi
  190. # =============================================================================
  191. # Prepare destination (clone fork fresh, or use --local)
  192. # =============================================================================
  193. CLEANUP_DIR=""
  194. cleanup() {
  195. [[ -n "$CLEANUP_DIR" ]] && rm -rf "$CLEANUP_DIR"
  196. }
  197. trap cleanup EXIT
  198. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  199. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  200. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  201. else
  202. echo "Cloning $FORK..."
  203. CLEANUP_DIR="$(mktemp -d)"
  204. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  205. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  206. fi
  207. DEST="$DEST_REPO/$DEST_REL"
  208. # Checkout base branch
  209. cd "$DEST_REPO"
  210. git checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  211. # Plugin-existence check depends on mode
  212. if [[ $BOOTSTRAP -eq 1 ]]; then
  213. [[ ! -d "$DEST" ]] || die "--bootstrap but base branch '$BASE' already has '$DEST_REL/' — use normal sync instead"
  214. mkdir -p "$DEST"
  215. else
  216. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — use --bootstrap + --assets-src, or pass --base <branch>"
  217. fi
  218. # =============================================================================
  219. # Create sync branch
  220. # =============================================================================
  221. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  222. if [[ $BOOTSTRAP -eq 1 ]]; then
  223. SYNC_BRANCH="bootstrap/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  224. else
  225. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  226. fi
  227. git checkout -q -b "$SYNC_BRANCH"
  228. # =============================================================================
  229. # Build rsync args
  230. # =============================================================================
  231. RSYNC_ARGS=(-av --delete)
  232. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  233. # =============================================================================
  234. # Dry run preview (always shown)
  235. # =============================================================================
  236. echo ""
  237. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  238. echo "Version: $UPSTREAM_VERSION"
  239. echo "Fork: $FORK"
  240. echo "Base: $BASE"
  241. echo "Branch: $SYNC_BRANCH"
  242. if [[ $BOOTSTRAP -eq 1 ]]; then
  243. echo "Mode: BOOTSTRAP (creating initial plugin from scratch)"
  244. echo "Assets: $ASSETS_SRC"
  245. fi
  246. echo ""
  247. echo "=== Preview (rsync --dry-run) ==="
  248. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  249. echo "=== End preview ==="
  250. echo ""
  251. echo "Overlay file (.codex-plugin/plugin.json) will be regenerated with"
  252. echo "version $UPSTREAM_VERSION regardless of rsync output."
  253. if [[ $BOOTSTRAP -eq 1 ]]; then
  254. echo "Assets (superpowers-small.svg, app-icon.png) will be seeded from:"
  255. echo " $ASSETS_SRC"
  256. fi
  257. if [[ $DRY_RUN -eq 1 ]]; then
  258. echo ""
  259. echo "Dry run only. Nothing was changed or pushed."
  260. exit 0
  261. fi
  262. # =============================================================================
  263. # Apply
  264. # =============================================================================
  265. echo ""
  266. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  267. echo ""
  268. echo "Syncing upstream content..."
  269. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  270. if [[ $BOOTSTRAP -eq 1 ]]; then
  271. echo "Seeding brand assets..."
  272. mkdir -p "$DEST/assets"
  273. cp "$ASSETS_SRC/PrimeRadiant_Favicon.svg" "$DEST/assets/superpowers-small.svg"
  274. cp "$ASSETS_SRC/PrimeRadiant_Favicon.png" "$DEST/assets/app-icon.png"
  275. fi
  276. echo "Regenerating overlay file..."
  277. generate_plugin_json "$DEST/.codex-plugin/plugin.json" "$UPSTREAM_VERSION"
  278. # Bail early if nothing actually changed
  279. cd "$DEST_REPO"
  280. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  281. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  282. exit 0
  283. fi
  284. # =============================================================================
  285. # Commit, push, open PR
  286. # =============================================================================
  287. git add "$DEST_REL"
  288. if [[ $BOOTSTRAP -eq 1 ]]; then
  289. COMMIT_TITLE="bootstrap superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  290. PR_BODY="Initial bootstrap of the superpowers plugin from upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  291. Creates \`plugins/superpowers/\` from scratch: upstream content via rsync, \`.codex-plugin/plugin.json\` regenerated inline, brand assets seeded from a local Brand Assets directory.
  292. Run via: \`scripts/sync-to-codex-plugin.sh --bootstrap --assets-src <path>\`
  293. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  294. This is a one-time bootstrap. Subsequent syncs will be normal (non-bootstrap) runs and will not touch the \`assets/\` directory."
  295. else
  296. COMMIT_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  297. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  298. Run via: \`scripts/sync-to-codex-plugin.sh\`
  299. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  300. 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."
  301. fi
  302. git commit --quiet -m "$COMMIT_TITLE
  303. Automated sync via scripts/sync-to-codex-plugin.sh
  304. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  305. Branch: $SYNC_BRANCH"
  306. echo "Pushing $SYNC_BRANCH to $FORK..."
  307. git push -u origin "$SYNC_BRANCH" --quiet
  308. echo "Opening PR..."
  309. PR_URL="$(gh pr create \
  310. --repo "$FORK" \
  311. --base "$BASE" \
  312. --head "$SYNC_BRANCH" \
  313. --title "$COMMIT_TITLE" \
  314. --body "$PR_BODY")"
  315. PR_NUM="${PR_URL##*/}"
  316. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  317. echo ""
  318. echo "PR opened: $PR_URL"
  319. echo "Diff view: $DIFF_URL"