sync-to-codex-plugin.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env bash
  2. #
  3. # sync-to-codex-plugin.sh
  4. #
  5. # Syncs this superpowers checkout into a Codex plugin mirror directory.
  6. # Pulls every file except the EXCLUDES list, never touches the PROTECTS list.
  7. # Leaves changes unstaged in the destination so a human can review before committing.
  8. #
  9. # Usage:
  10. # ./scripts/sync-to-codex-plugin.sh # sync with confirmation
  11. # ./scripts/sync-to-codex-plugin.sh -n # dry run, show changes only
  12. # ./scripts/sync-to-codex-plugin.sh -y # skip confirmation prompt
  13. # ./scripts/sync-to-codex-plugin.sh --dest /path/to/plugins/superpowers
  14. #
  15. # Environment:
  16. # CODEX_PLUGIN_DEST Destination plugin path (default: sibling openai-codex-plugins checkout)
  17. set -euo pipefail
  18. # =============================================================================
  19. # Config — edit these lists as the upstream or canonical shape evolves
  20. # =============================================================================
  21. # Paths in upstream that should NOT land in the embedded plugin.
  22. # Rsync --exclude patterns (trailing slash = directory).
  23. EXCLUDES=(
  24. # Dotfiles and infra
  25. ".claude/"
  26. ".claude-plugin/"
  27. ".codex/"
  28. ".cursor-plugin/"
  29. ".git/"
  30. ".gitattributes"
  31. ".github/"
  32. ".gitignore"
  33. ".opencode/"
  34. ".version-bump.json"
  35. ".worktrees/"
  36. ".DS_Store"
  37. # Root ceremony files (not part of a canonical Codex plugin)
  38. "AGENTS.md"
  39. "CHANGELOG.md"
  40. "CLAUDE.md"
  41. "CODE_OF_CONDUCT.md"
  42. "GEMINI.md"
  43. "RELEASE-NOTES.md"
  44. "gemini-extension.json"
  45. "package.json"
  46. # Directories not shipped by canonical Codex plugins
  47. "commands/"
  48. "docs/"
  49. "hooks/"
  50. "lib/"
  51. "scripts/"
  52. "tests/"
  53. "tmp/"
  54. )
  55. # Paths in the destination that are hand-authored Codex overlays.
  56. # Rsync will never touch these — including when --delete would otherwise
  57. # remove them because they don't exist in upstream.
  58. PROTECTS=(
  59. ".codex-plugin/"
  60. "agents/openai.yaml"
  61. )
  62. # =============================================================================
  63. # Paths
  64. # =============================================================================
  65. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  66. UPSTREAM="$(cd "$SCRIPT_DIR/.." && pwd)"
  67. # Default dest: sibling openai-codex-plugins checkout, if it exists
  68. DEFAULT_DEST="${CODEX_PLUGIN_DEST:-$(dirname "$UPSTREAM")/openai-codex-plugins/plugins/superpowers}"
  69. # =============================================================================
  70. # Args
  71. # =============================================================================
  72. DEST="$DEFAULT_DEST"
  73. DRY_RUN=0
  74. YES=0
  75. usage() {
  76. sed -n 's/^# \{0,1\}//;2,20p' "$0"
  77. exit "${1:-0}"
  78. }
  79. while [[ $# -gt 0 ]]; do
  80. case "$1" in
  81. --dest) DEST="$2"; shift 2 ;;
  82. -n|--dry-run) DRY_RUN=1; shift ;;
  83. -y|--yes) YES=1; shift ;;
  84. -h|--help) usage 0 ;;
  85. *) echo "Unknown arg: $1" >&2; usage 2 ;;
  86. esac
  87. done
  88. # =============================================================================
  89. # Validate environment
  90. # =============================================================================
  91. if [[ ! -d "$UPSTREAM/.git" ]]; then
  92. echo "ERROR: Upstream '$UPSTREAM' is not a git checkout." >&2
  93. exit 1
  94. fi
  95. if [[ ! -d "$DEST" ]]; then
  96. echo "ERROR: Destination '$DEST' does not exist." >&2
  97. echo "Set CODEX_PLUGIN_DEST or pass --dest <path>." >&2
  98. exit 1
  99. fi
  100. confirm() {
  101. local prompt="$1"
  102. [[ $YES -eq 1 ]] && return 0
  103. read -rp "$prompt [y/N] " ans
  104. [[ "$ans" == "y" || "$ans" == "Y" ]]
  105. }
  106. # Check upstream branch
  107. UPSTREAM_BRANCH="$(cd "$UPSTREAM" && git branch --show-current)"
  108. UPSTREAM_SHA="$(cd "$UPSTREAM" && git rev-parse HEAD)"
  109. UPSTREAM_SHORT="$(cd "$UPSTREAM" && git rev-parse --short HEAD)"
  110. if [[ "$UPSTREAM_BRANCH" != "main" ]]; then
  111. echo "WARNING: Upstream is on branch '$UPSTREAM_BRANCH', not 'main'."
  112. confirm "Sync from '$UPSTREAM_BRANCH' anyway?" || exit 1
  113. fi
  114. # Check upstream working tree is clean
  115. UPSTREAM_STATUS="$(cd "$UPSTREAM" && git status --porcelain)"
  116. if [[ -n "$UPSTREAM_STATUS" ]]; then
  117. echo "WARNING: Upstream has uncommitted changes:"
  118. echo "$UPSTREAM_STATUS" | sed 's/^/ /'
  119. echo "Sync will use the working-tree state, not HEAD ($UPSTREAM_SHORT)."
  120. confirm "Continue anyway?" || exit 1
  121. fi
  122. # =============================================================================
  123. # Build rsync args
  124. # =============================================================================
  125. RSYNC_ARGS=(-av --delete)
  126. for pat in "${EXCLUDES[@]}"; do
  127. RSYNC_ARGS+=(--exclude="$pat")
  128. done
  129. for pat in "${PROTECTS[@]}"; do
  130. RSYNC_ARGS+=(--filter="protect $pat")
  131. done
  132. # =============================================================================
  133. # Dry run first, always
  134. # =============================================================================
  135. echo ""
  136. echo "Upstream: $UPSTREAM ($UPSTREAM_BRANCH @ $UPSTREAM_SHORT)"
  137. echo "Dest: $DEST"
  138. echo ""
  139. echo "=== Preview (rsync --dry-run) ==="
  140. rsync "${RSYNC_ARGS[@]}" --dry-run --itemize-changes "$UPSTREAM/" "$DEST/"
  141. echo "=== End preview ==="
  142. if [[ $DRY_RUN -eq 1 ]]; then
  143. echo ""
  144. echo "Dry run only. Nothing was changed."
  145. exit 0
  146. fi
  147. # =============================================================================
  148. # Apply
  149. # =============================================================================
  150. echo ""
  151. confirm "Apply these changes?" || { echo "Aborted."; exit 1; }
  152. echo ""
  153. echo "Syncing..."
  154. rsync "${RSYNC_ARGS[@]}" "$UPSTREAM/" "$DEST/"
  155. echo "Done."
  156. echo ""
  157. # =============================================================================
  158. # Report
  159. # =============================================================================
  160. DEST_GIT_ROOT="$(cd "$DEST" && git rev-parse --show-toplevel 2>/dev/null || echo "")"
  161. if [[ -n "$DEST_GIT_ROOT" ]]; then
  162. DEST_REL="${DEST#$DEST_GIT_ROOT/}"
  163. CHANGES="$(cd "$DEST_GIT_ROOT" && git status --porcelain "$DEST_REL")"
  164. if [[ -z "$CHANGES" ]]; then
  165. echo "No changes — destination was already in sync with upstream $UPSTREAM_SHORT."
  166. exit 0
  167. fi
  168. echo "Changes pending review:"
  169. echo "$CHANGES" | sed 's/^/ /'
  170. echo ""
  171. echo "Upstream SHA: $UPSTREAM_SHA"
  172. echo ""
  173. echo "Suggested commit message:"
  174. echo " sync superpowers from upstream main @ $UPSTREAM_SHORT"
  175. echo ""
  176. echo "Review with: git -C $DEST_GIT_ROOT diff -- $DEST_REL"
  177. else
  178. echo "Destination is not a git checkout — cannot report changes."
  179. fi