sync-to-codex-plugin.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 with confirm
  16. # ./scripts/sync-to-codex-plugin.sh -n # dry run, no clone/push/PR
  17. # ./scripts/sync-to-codex-plugin.sh -y # skip confirmation
  18. # ./scripts/sync-to-codex-plugin.sh --local PATH # use existing checkout
  19. # ./scripts/sync-to-codex-plugin.sh --base BRANCH # target branch (default: main)
  20. #
  21. # Requires: bash, rsync, git, gh (authenticated), python3.
  22. set -euo pipefail
  23. # =============================================================================
  24. # Config — edit as upstream or canonical plugin shape evolves
  25. # =============================================================================
  26. FORK="prime-radiant-inc/openai-codex-plugins"
  27. DEFAULT_BASE="main"
  28. DEST_REL="plugins/superpowers"
  29. # Paths in upstream that should NOT land in the embedded plugin.
  30. # The Codex-overlay file is here too — it's managed by the generate step,
  31. # not by rsync.
  32. EXCLUDES=(
  33. # Dotfiles and infra
  34. ".claude/"
  35. ".claude-plugin/"
  36. ".codex/"
  37. ".cursor-plugin/"
  38. ".git/"
  39. ".gitattributes"
  40. ".github/"
  41. ".gitignore"
  42. ".opencode/"
  43. ".version-bump.json"
  44. ".worktrees/"
  45. ".DS_Store"
  46. # Root ceremony files
  47. "AGENTS.md"
  48. "CHANGELOG.md"
  49. "CLAUDE.md"
  50. "GEMINI.md"
  51. "RELEASE-NOTES.md"
  52. "gemini-extension.json"
  53. "package.json"
  54. # Directories not shipped by canonical Codex plugins
  55. "commands/"
  56. "docs/"
  57. "hooks/"
  58. "lib/"
  59. "scripts/"
  60. "tests/"
  61. "tmp/"
  62. # Codex-overlay file — regenerated below, not synced
  63. ".codex-plugin/"
  64. )
  65. # =============================================================================
  66. # Generated overlay file
  67. # =============================================================================
  68. # Writes the Codex plugin manifest to "$1" with the given upstream version.
  69. # Args: dest_path, version
  70. generate_plugin_json() {
  71. local dest="$1"
  72. local version="$2"
  73. mkdir -p "$(dirname "$dest")"
  74. cat > "$dest" <<EOF
  75. {
  76. "name": "superpowers",
  77. "version": "$version",
  78. "description": "Core skills library for Codex: planning, TDD, debugging, and collaboration workflows.",
  79. "author": {
  80. "name": "Jesse Vincent",
  81. "email": "jesse@fsck.com",
  82. "url": "https://github.com/obra"
  83. },
  84. "homepage": "https://github.com/obra/superpowers",
  85. "repository": "https://github.com/obra/superpowers",
  86. "license": "MIT",
  87. "keywords": [
  88. "skills",
  89. "planning",
  90. "tdd",
  91. "debugging",
  92. "code-review",
  93. "workflow"
  94. ],
  95. "skills": "./skills/",
  96. "interface": {
  97. "displayName": "Superpowers",
  98. "shortDescription": "Planning, TDD, debugging, and delivery workflows for coding agents",
  99. "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.",
  100. "developerName": "Jesse Vincent",
  101. "category": "Coding",
  102. "capabilities": [
  103. "Interactive",
  104. "Read",
  105. "Write"
  106. ],
  107. "brandColor": "#F59E0B",
  108. "composerIcon": "./assets/superpowers-small.svg",
  109. "logo": "./assets/app-icon.png",
  110. "screenshots": []
  111. }
  112. }
  113. EOF
  114. }
  115. # =============================================================================
  116. # Args
  117. # =============================================================================
  118. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  119. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  120. BASE="$DEFAULT_BASE"
  121. DRY_RUN=0
  122. YES=0
  123. LOCAL_CHECKOUT=""
  124. usage() {
  125. sed -n 's/^# \{0,1\}//;2,20p' "$0"
  126. exit "${1:-0}"
  127. }
  128. while [[ $# -gt 0 ]]; do
  129. case "$1" in
  130. -n|--dry-run) DRY_RUN=1; shift ;;
  131. -y|--yes) YES=1; shift ;;
  132. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  133. --base) BASE="$2"; shift 2 ;;
  134. -h|--help) usage 0 ;;
  135. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  136. esac
  137. done
  138. # =============================================================================
  139. # Preflight
  140. # =============================================================================
  141. die() { echo "ERROR: $*" >&2; exit 1; }
  142. command -v rsync >/dev/null || die "rsync not found in PATH"
  143. command -v git >/dev/null || die "git not found in PATH"
  144. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  145. command -v python3 >/dev/null || die "python3 not found in PATH"
  146. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  147. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  148. [[ -f "$UPSTREAM/package.json" ]] || die "upstream has no package.json — cannot read version"
  149. # Read the upstream version from package.json
  150. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/package.json")"
  151. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from upstream package.json"
  152. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  153. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  154. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  155. confirm() {
  156. [[ $YES -eq 1 ]] && return 0
  157. read -rp "$1 [y/N] " ans
  158. [[ "$ans" == "y" || "$ans" == "Y" ]]
  159. }
  160. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  161. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  162. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  163. fi
  164. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  165. if [[ -n "$UPSTREAM_STATUS" ]]; then
  166. echo "WARNING: upstream has uncommitted changes:"
  167. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  168. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  169. confirm "Continue anyway?" || exit 1
  170. fi
  171. # =============================================================================
  172. # Prepare destination (clone fork fresh, or use --local)
  173. # =============================================================================
  174. CLEANUP_DIR=""
  175. cleanup() {
  176. [[ -n "$CLEANUP_DIR" ]] && rm -rf "$CLEANUP_DIR"
  177. }
  178. trap cleanup EXIT
  179. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  180. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  181. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  182. else
  183. echo "Cloning $FORK..."
  184. CLEANUP_DIR="$(mktemp -d)"
  185. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  186. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  187. fi
  188. DEST="$DEST_REPO/$DEST_REL"
  189. # Checkout base branch
  190. cd "$DEST_REPO"
  191. git checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  192. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — merge the bootstrap PR first, or pass --base <branch>"
  193. # =============================================================================
  194. # Create sync branch
  195. # =============================================================================
  196. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  197. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  198. git checkout -q -b "$SYNC_BRANCH"
  199. # =============================================================================
  200. # Build rsync args (excludes only — overlay is regenerated separately)
  201. # =============================================================================
  202. RSYNC_ARGS=(-av --delete)
  203. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  204. # =============================================================================
  205. # Dry run preview (always shown)
  206. # =============================================================================
  207. echo ""
  208. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  209. echo "Version: $UPSTREAM_VERSION"
  210. echo "Fork: $FORK"
  211. echo "Base: $BASE"
  212. echo "Branch: $SYNC_BRANCH"
  213. echo ""
  214. echo "=== Preview (rsync --dry-run) ==="
  215. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  216. echo "=== End preview ==="
  217. echo ""
  218. echo "Overlay file (.codex-plugin/plugin.json) will be regenerated with"
  219. echo "version $UPSTREAM_VERSION regardless of rsync output."
  220. if [[ $DRY_RUN -eq 1 ]]; then
  221. echo ""
  222. echo "Dry run only. Nothing was changed or pushed."
  223. exit 0
  224. fi
  225. # =============================================================================
  226. # Apply
  227. # =============================================================================
  228. echo ""
  229. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  230. echo ""
  231. echo "Syncing upstream content..."
  232. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  233. echo "Regenerating overlay file..."
  234. generate_plugin_json "$DEST/.codex-plugin/plugin.json" "$UPSTREAM_VERSION"
  235. # Bail early if nothing actually changed
  236. cd "$DEST_REPO"
  237. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  238. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  239. exit 0
  240. fi
  241. # =============================================================================
  242. # Commit, push, open PR
  243. # =============================================================================
  244. git add "$DEST_REL"
  245. git commit --quiet -m "sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT
  246. Automated sync via scripts/sync-to-codex-plugin.sh
  247. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  248. Branch: $SYNC_BRANCH"
  249. echo "Pushing $SYNC_BRANCH to $FORK..."
  250. git push -u origin "$SYNC_BRANCH" --quiet
  251. PR_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  252. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  253. Run via: \`scripts/sync-to-codex-plugin.sh\`
  254. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  255. 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."
  256. echo "Opening PR..."
  257. PR_URL="$(gh pr create \
  258. --repo "$FORK" \
  259. --base "$BASE" \
  260. --head "$SYNC_BRANCH" \
  261. --title "$PR_TITLE" \
  262. --body "$PR_BODY")"
  263. PR_NUM="${PR_URL##*/}"
  264. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  265. echo ""
  266. echo "PR opened: $PR_URL"
  267. echo "Diff view: $DIFF_URL"