sync-to-codex-plugin.sh 11 KB

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