-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathverify-sync.sh
More file actions
executable file
·268 lines (222 loc) · 7.97 KB
/
Copy pathverify-sync.sh
File metadata and controls
executable file
·268 lines (222 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env bash
#
# verify-sync.sh — Verify marketplace.json is in sync with plugin.json sources of truth
#
# Usage: bash scripts/verify-sync.sh [--run-sync]
#
# Checks:
# 1. Field-level comparison (description, version, author, keywords, license) per plugin
# 2. Orphan entries in marketplace.json (no plugin.json on disk)
# 3. Missing entries (plugin.json on disk but not in marketplace.json)
# 4. Optionally runs sync-plugins.sh and checks idempotency (--run-sync)
#
# Exit 0 if all in sync, exit 1 if any drift detected.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
MARKETPLACE="$REPO_ROOT/.claude-plugin/marketplace.json"
PLUGINS_DIR="$REPO_ROOT/plugins"
SYNC_SCRIPT="$REPO_ROOT/scripts/sync-plugins.sh"
RUN_SYNC=false
if [[ "${1:-}" == "--run-sync" ]]; then
RUN_SYNC=true
fi
# --- Preflight ---
if ! command -v jq &>/dev/null; then
echo "Error: jq is required but not installed." >&2
exit 1
fi
if [[ ! -f "$MARKETPLACE" ]]; then
echo "Error: marketplace.json not found at $MARKETPLACE" >&2
exit 1
fi
# --- Counters ---
MATCH_COUNT=0
DRIFT_COUNT=0
match() {
echo " [MATCH] $1"
MATCH_COUNT=$((MATCH_COUNT + 1))
}
drift() {
echo " [DRIFT] $1"
DRIFT_COUNT=$((DRIFT_COUNT + 1))
}
# --- Step 1: Field-level comparison for each self-developed plugin ---
declare -a DISK_PLUGINS=()
for plugin_json in "$PLUGINS_DIR"/*/.claude-plugin/plugin.json; do
[[ -f "$plugin_json" ]] || continue
plugin_dir="$(dirname "$(dirname "$plugin_json")")"
plugin_name="$(basename "$plugin_dir")"
DISK_PLUGINS+=("$plugin_name")
echo ""
echo "Checking sync: $plugin_name"
# Read from plugin.json (source of truth)
p_name="$(jq -r '.name' "$plugin_json")"
p_desc="$(jq -r '.description' "$plugin_json")"
p_version="$(jq -r '.version' "$plugin_json")"
p_author_name="$(jq -r '.author.name' "$plugin_json")"
p_author_url="$(jq -r '.author.url // empty' "$plugin_json")"
p_keywords="$(jq -c '.keywords // empty' "$plugin_json")"
p_license="$(jq -r '.license // empty' "$plugin_json")"
# Check if plugin exists in marketplace.json
m_exists="$(jq --arg pname "$p_name" '[.plugins[] | select(.name == $pname)] | length' "$MARKETPLACE")"
if [[ "$m_exists" -eq 0 ]]; then
drift "plugin '$p_name' not found in marketplace.json"
continue
fi
# Read from marketplace.json
m_desc="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | .description' "$MARKETPLACE")"
m_version="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | .version' "$MARKETPLACE")"
m_author_name="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | .author.name' "$MARKETPLACE")"
m_author_url="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | (.author.url // empty)' "$MARKETPLACE")"
m_keywords="$(jq -c --arg pname "$p_name" '.plugins[] | select(.name == $pname) | (.keywords // empty)' "$MARKETPLACE")"
m_license="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | (.license // empty)' "$MARKETPLACE")"
m_source="$(jq -r --arg pname "$p_name" '.plugins[] | select(.name == $pname) | .source' "$MARKETPLACE")"
# Compare description
if [[ "$m_desc" == "$p_desc" ]]; then
match "description"
else
drift "description: marketplace='$m_desc' vs plugin.json='$p_desc'"
fi
# Compare version
if [[ "$m_version" == "$p_version" ]]; then
match "version"
else
drift "version: marketplace='$m_version' vs plugin.json='$p_version'"
fi
# Compare author.name
if [[ "$m_author_name" == "$p_author_name" ]]; then
match "author.name"
else
drift "author.name: marketplace='$m_author_name' vs plugin.json='$p_author_name'"
fi
# Compare author.url (only if present in plugin.json)
if [[ -n "$p_author_url" ]]; then
if [[ "$m_author_url" == "$p_author_url" ]]; then
match "author.url"
else
drift "author.url: marketplace='$m_author_url' vs plugin.json='$p_author_url'"
fi
else
match "author.url (not required)"
fi
# Compare keywords (optional: must be equal, or absent on both sides)
if [[ "$m_keywords" == "$p_keywords" ]]; then
if [[ -n "$p_keywords" ]]; then
match "keywords"
else
match "keywords (not required)"
fi
else
drift "keywords: marketplace='$m_keywords' vs plugin.json='$p_keywords'"
fi
# Compare license (optional: must be equal, or absent on both sides)
if [[ "$m_license" == "$p_license" ]]; then
if [[ -n "$p_license" ]]; then
match "license"
else
match "license (not required)"
fi
else
drift "license: marketplace='$m_license' vs plugin.json='$p_license'"
fi
# Check source is local
if [[ "$m_source" == "./plugins/"* ]]; then
match "source is local path"
else
drift "source is not a local path: '$m_source'"
fi
done
# --- Step 2: Detect orphan entries ---
# Marketplace entries with local source that have no plugin.json on disk
echo ""
orphan_found=false
while IFS= read -r entry_name; do
entry_source="$(jq -r --arg pname "$entry_name" '.plugins[] | select(.name == $pname) | .source' "$MARKETPLACE")"
# Only check local-source plugins
if [[ "$entry_source" != "./plugins/"* ]]; then
continue
fi
# Check if this plugin exists on disk
# (${arr[@]+...} keeps bash 3.2's set -u happy when the array is empty)
found=false
for dp in ${DISK_PLUGINS[@]+"${DISK_PLUGINS[@]}"}; do
if [[ "$dp" == "$entry_name" ]]; then
found=true
break
fi
done
if ! $found; then
drift "orphan entry in marketplace.json: '$entry_name' (no plugin.json on disk)"
orphan_found=true
fi
done < <(jq -r '.plugins[].name' "$MARKETPLACE")
if ! $orphan_found; then
echo "No orphan entries in marketplace.json"
fi
# --- Step 3: Detect missing entries ---
# plugin.json on disk but not in marketplace.json
missing_found=false
for dp in ${DISK_PLUGINS[@]+"${DISK_PLUGINS[@]}"}; do
m_count="$(jq --arg pname "$dp" '[.plugins[] | select(.name == $pname)] | length' "$MARKETPLACE")"
if [[ "$m_count" -eq 0 ]]; then
drift "missing from marketplace.json: '$dp' (has plugin.json on disk)"
missing_found=true
fi
done
if ! $missing_found; then
echo "No missing plugins from marketplace.json"
fi
# --- Step 4: Optional sync idempotency check ---
if $RUN_SYNC; then
echo ""
echo "Running sync idempotency check..."
if [[ ! -f "$SYNC_SCRIPT" ]]; then
echo "Error: sync-plugins.sh not found at $SYNC_SCRIPT" >&2
exit 1
fi
# Back up the files sync-plugins.sh writes. The EXIT trap restores them and
# removes the temp files on every exit path, including a failed sync run,
# so this script never leaves the repo modified.
MARKETPLACE_BAK="$(mktemp)"
README_BAK="$(mktemp)"
SYNC_LOG="$(mktemp)"
cp "$MARKETPLACE" "$MARKETPLACE_BAK"
cp "$REPO_ROOT/README.md" "$README_BAK"
restore_backups() {
if [[ -f "$MARKETPLACE_BAK" ]]; then
cp "$MARKETPLACE_BAK" "$MARKETPLACE"
fi
if [[ -f "$README_BAK" ]]; then
cp "$README_BAK" "$REPO_ROOT/README.md"
fi
rm -f "$MARKETPLACE_BAK" "$README_BAK" "$SYNC_LOG"
}
trap restore_backups EXIT
# Run sync, keeping its output for diagnostics
if ! bash "$SYNC_SCRIPT" > "$SYNC_LOG" 2>&1; then
echo "Error: sync-plugins.sh failed during idempotency check; its output was:" >&2
cat "$SYNC_LOG" >&2
echo "Restoring marketplace.json and README.md from backup." >&2
exit 1
fi
# Compare (the trap restores the originals afterwards either way)
if diff -q "$MARKETPLACE_BAK" "$MARKETPLACE" >/dev/null 2>&1; then
match "marketplace.json unchanged after sync (idempotent)"
else
drift "marketplace.json changed after running sync-plugins.sh"
fi
if diff -q "$README_BAK" "$REPO_ROOT/README.md" >/dev/null 2>&1; then
match "README.md unchanged after sync (idempotent)"
else
drift "README.md changed after running sync-plugins.sh"
fi
fi
# --- Summary ---
echo ""
if [[ $DRIFT_COUNT -eq 0 ]]; then
echo "Summary: All plugins in sync ($MATCH_COUNT checks passed)"
exit 0
else
echo "Summary: $MATCH_COUNT matched, $DRIFT_COUNT drifted"
exit 1
fi