sync-to-codex-plugin.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "websiteURL": "https://github.com/obra/superpowers",
  108. "privacyPolicyURL": "https://docs.github.com/site-policy/privacy-policies/github-general-privacy-statement",
  109. "termsOfServiceURL": "https://docs.github.com/en/site-policy/github-terms/github-terms-of-service",
  110. "defaultPrompt": [
  111. "Use Superpowers to plan this feature before we code",
  112. "Debug this bug with a systematic root-cause workflow",
  113. "Turn this approved design into an implementation plan"
  114. ],
  115. "brandColor": "#F59E0B",
  116. "screenshots": []
  117. }
  118. }
  119. EOF
  120. }
  121. # =============================================================================
  122. # Args
  123. # =============================================================================
  124. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  125. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  126. BASE="$DEFAULT_BASE"
  127. DRY_RUN=0
  128. YES=0
  129. LOCAL_CHECKOUT=""
  130. usage() {
  131. sed -n 's/^# \{0,1\}//;2,20p' "$0"
  132. exit "${1:-0}"
  133. }
  134. while [[ $# -gt 0 ]]; do
  135. case "$1" in
  136. -n|--dry-run) DRY_RUN=1; shift ;;
  137. -y|--yes) YES=1; shift ;;
  138. --local) LOCAL_CHECKOUT="$2"; shift 2 ;;
  139. --base) BASE="$2"; shift 2 ;;
  140. -h|--help) usage 0 ;;
  141. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  142. esac
  143. done
  144. # =============================================================================
  145. # Preflight
  146. # =============================================================================
  147. die() { echo "ERROR: $*" >&2; exit 1; }
  148. command -v rsync >/dev/null || die "rsync not found in PATH"
  149. command -v git >/dev/null || die "git not found in PATH"
  150. command -v gh >/dev/null || die "gh not found — install GitHub CLI"
  151. command -v python3 >/dev/null || die "python3 not found in PATH"
  152. gh auth status >/dev/null 2>&1 || die "gh not authenticated — run 'gh auth login'"
  153. [[ -d "$UPSTREAM/.git" ]] || die "upstream '$UPSTREAM' is not a git checkout"
  154. [[ -f "$UPSTREAM/package.json" ]] || die "upstream has no package.json — cannot read version"
  155. # Read the upstream version from package.json
  156. UPSTREAM_VERSION="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$UPSTREAM/package.json")"
  157. [[ -n "$UPSTREAM_VERSION" ]] || die "could not read 'version' from upstream package.json"
  158. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  159. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  160. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  161. confirm() {
  162. [[ $YES -eq 1 ]] && return 0
  163. read -rp "$1 [y/N] " ans
  164. [[ "$ans" == "y" || "$ans" == "Y" ]]
  165. }
  166. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  167. echo "WARNING: upstream is on '$UPSTREAM_BRANCH', not 'main'"
  168. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  169. fi
  170. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  171. if [[ -n "$UPSTREAM_STATUS" ]]; then
  172. echo "WARNING: upstream has uncommitted changes:"
  173. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  174. echo "Sync will use working-tree state, not HEAD ($UPSTREAM_SHORT)."
  175. confirm "Continue anyway?" || exit 1
  176. fi
  177. # =============================================================================
  178. # Prepare destination (clone fork fresh, or use --local)
  179. # =============================================================================
  180. CLEANUP_DIR=""
  181. cleanup() {
  182. [[ -n "$CLEANUP_DIR" ]] && rm -rf "$CLEANUP_DIR"
  183. }
  184. trap cleanup EXIT
  185. if [[ -n "$LOCAL_CHECKOUT" ]]; then
  186. DEST_REPO="$(cd "$LOCAL_CHECKOUT" && pwd)"
  187. [[ -d "$DEST_REPO/.git" ]] || die "--local path '$DEST_REPO' is not a git checkout"
  188. else
  189. echo "Cloning $FORK..."
  190. CLEANUP_DIR="$(mktemp -d)"
  191. DEST_REPO="$CLEANUP_DIR/openai-codex-plugins"
  192. gh repo clone "$FORK" "$DEST_REPO" >/dev/null
  193. fi
  194. DEST="$DEST_REPO/$DEST_REL"
  195. # Checkout base branch
  196. cd "$DEST_REPO"
  197. git checkout -q "$BASE" 2>/dev/null || die "base branch '$BASE' doesn't exist in $FORK"
  198. [[ -d "$DEST" ]] || die "base branch '$BASE' has no '$DEST_REL/' — merge the bootstrap PR first, or pass --base <branch>"
  199. # =============================================================================
  200. # Create sync branch
  201. # =============================================================================
  202. TIMESTAMP="$(date -u +%Y%m%d-%H%M%S)"
  203. SYNC_BRANCH="sync/superpowers-${UPSTREAM_SHORT}-${TIMESTAMP}"
  204. git checkout -q -b "$SYNC_BRANCH"
  205. # =============================================================================
  206. # Build rsync args (excludes only — overlay is regenerated separately)
  207. # =============================================================================
  208. RSYNC_ARGS=(-av --delete)
  209. for pat in "${EXCLUDES[@]}"; do RSYNC_ARGS+=(--exclude="$pat"); done
  210. # =============================================================================
  211. # Dry run preview (always shown)
  212. # =============================================================================
  213. echo ""
  214. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  215. echo "Version: $UPSTREAM_VERSION"
  216. echo "Fork: $FORK"
  217. echo "Base: $BASE"
  218. echo "Branch: $SYNC_BRANCH"
  219. echo ""
  220. echo "=== Preview (rsync --dry-run) ==="
  221. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  222. echo "=== End preview ==="
  223. echo ""
  224. echo "Overlay file (.codex-plugin/plugin.json) will be regenerated with"
  225. echo "version $UPSTREAM_VERSION regardless of rsync output."
  226. if [[ $DRY_RUN -eq 1 ]]; then
  227. echo ""
  228. echo "Dry run only. Nothing was changed or pushed."
  229. exit 0
  230. fi
  231. # =============================================================================
  232. # Apply
  233. # =============================================================================
  234. echo ""
  235. confirm "Apply changes, push branch, and open PR?" || { echo "Aborted."; exit 1; }
  236. echo ""
  237. echo "Syncing upstream content..."
  238. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  239. echo "Regenerating overlay file..."
  240. generate_plugin_json "$DEST/.codex-plugin/plugin.json" "$UPSTREAM_VERSION"
  241. # Bail early if nothing actually changed
  242. cd "$DEST_REPO"
  243. if [[ -z "$(git status --porcelain "$DEST_REL")" ]]; then
  244. echo "No changes — embedded plugin was already in sync with upstream $UPSTREAM_SHORT (v$UPSTREAM_VERSION)."
  245. exit 0
  246. fi
  247. # =============================================================================
  248. # Commit, push, open PR
  249. # =============================================================================
  250. git add "$DEST_REL"
  251. git commit --quiet -m "sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT
  252. Automated sync via scripts/sync-to-codex-plugin.sh
  253. Upstream: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  254. Branch: $SYNC_BRANCH"
  255. echo "Pushing $SYNC_BRANCH to $FORK..."
  256. git push -u origin "$SYNC_BRANCH" --quiet
  257. PR_TITLE="sync superpowers v$UPSTREAM_VERSION from upstream main @ $UPSTREAM_SHORT"
  258. PR_BODY="Automated sync from superpowers upstream \`main\` @ \`$UPSTREAM_SHORT\` (v$UPSTREAM_VERSION).
  259. Run via: \`scripts/sync-to-codex-plugin.sh\`
  260. Upstream commit: https://github.com/obra/superpowers/commit/$UPSTREAM_SHA
  261. 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."
  262. echo "Opening PR..."
  263. PR_URL="$(gh pr create \
  264. --repo "$FORK" \
  265. --base "$BASE" \
  266. --head "$SYNC_BRANCH" \
  267. --title "$PR_TITLE" \
  268. --body "$PR_BODY")"
  269. PR_NUM="${PR_URL##*/}"
  270. DIFF_URL="https://github.com/$FORK/pull/$PR_NUM/files"
  271. echo ""
  272. echo "PR opened: $PR_URL"
  273. echo "Diff view: $DIFF_URL"