-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclaudeconfig.sh
More file actions
executable file
·319 lines (266 loc) · 12 KB
/
claudeconfig.sh
File metadata and controls
executable file
·319 lines (266 loc) · 12 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env bash
set -e
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/functions.sh"
# Prerequisite checks
if ! command_available claude; then
echo "Error: claude command not found. Install Claude Code first."
exit 1
fi
if ! command_available jq; then
echo "Error: jq required for JSON merging. Install with: brew install jq"
exit 1
fi
# Read a JSON or JSONC file, stripping comments and trailing commas
# Uses node if available for robust JSONC parsing, falls back to sed
read_json() {
local file="$1"
if command -v node > /dev/null 2>&1; then
# Node handles JSONC natively with JSON5-like parsing
node -e "
const fs = require('fs');
const file = '$file';
const content = fs.readFileSync(file, 'utf8');
// Strip comments and trailing commas
const stripped = content
.replace(/\/\/.*$/gm, '') // Remove // comments
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove /* */ comments
.replace(/,(\s*[}\]])/g, '\$1'); // Remove trailing commas
try {
console.log(JSON.stringify(JSON.parse(stripped)));
} catch (e) {
process.stderr.write('Error parsing ' + file + ': ' + e.message + '\n');
process.exit(1);
}
"
else
# Fallback: simple sed-based stripping (less robust)
sed -E 's|//[^"]*$||g' < "$file" \
| tr '\n' '\f' \
| sed -E 's|,([[:space:]\f]*[}\]])|\1|g' \
| tr '\f' '\n' \
| jq '.'
fi
}
# Detect role (uses existing DOTPICKLES_ROLE from environment)
ROLE="${DOTPICKLES_ROLE:-personal}"
echo "Configuring Claude Code for role: $ROLE"
# Ensure ~/.claude exists and symlink managed files
setup_claude_directory() {
echo "Setting up ~/.claude directory..."
mkdir -p "$HOME/.claude"
mkdir -p "$HOME/.claude/skills"
# Symlink CLAUDE.md (user's global instructions)
local claude_md="$DIR/claude/CLAUDE.md"
local claude_md_target="$HOME/.claude/CLAUDE.md"
if [ -L "$claude_md_target" ]; then
echo " ✓ CLAUDE.md already symlinked"
elif [ -f "$claude_md_target" ]; then
echo " ⚠ CLAUDE.md exists as regular file, skipping (remove manually to symlink)"
else
ln -s "$claude_md" "$claude_md_target"
echo " ✓ CLAUDE.md symlinked"
fi
}
setup_claude_directory
# Generate settings.json from roles/ + stacks/
generate_settings() {
echo "Generating settings.json..."
local settings_file="$HOME/.claude/settings.json"
local temp_file="$(mktemp)"
# Local-only keys preserved from existing settings across regenerations
local local_keys=("enabledPlugins" "extraKnownMarketplaces")
# Extract local-only settings from existing file
local local_settings="{}"
if [ -f "$settings_file" ]; then
for key in "${local_keys[@]}"; do
if jq -e ".$key" "$settings_file" > /dev/null 2>&1; then
local_settings=$(echo "$local_settings" | jq --argjson val "$(jq ".$key" "$settings_file")" ". + {\"$key\": \$val}")
fi
done
fi
# --- Load base role ---
local base_role="$DIR/claude/roles/base.jsonc"
if [ ! -f "$base_role" ]; then
echo "Error: $base_role not found"
exit 1
fi
local base_json
base_json=$(read_json "$base_role")
# Extract settings (everything except permissions and sandbox)
local merged_settings
merged_settings=$(echo "$base_json" | jq 'del(.permissions, .sandbox)')
# Extract permissions arrays from base
local merged_allow merged_ask merged_deny
merged_allow=$(echo "$base_json" | jq '.permissions.allow // []')
merged_ask=$(echo "$base_json" | jq '.permissions.ask // []')
merged_deny=$(echo "$base_json" | jq '.permissions.deny // []')
# Extract permissions scalars (e.g. defaultMode) from base
local permissions_scalars
permissions_scalars=$(echo "$base_json" | jq '.permissions // {} | del(.allow, .ask, .deny)')
# Extract sandbox from base (scalars + arrays)
local sandbox_scalars sandbox_hosts sandbox_write_paths
sandbox_scalars=$(echo "$base_json" | jq '.sandbox // {} | del(.network.allowedHosts, .filesystem.allowWrite, .filesystem, .network) + (if .network then {network: (.network | del(.allowedHosts))} else {} end) | del(.network | nulls) | del(.filesystem | nulls)')
sandbox_hosts=$(echo "$base_json" | jq '.sandbox.network.allowedHosts // []')
sandbox_write_paths=$(echo "$base_json" | jq '.sandbox.filesystem.allowWrite // []')
echo " + Loaded base role"
# --- Load active role (if not base) ---
local role_file="$DIR/claude/roles/$ROLE.jsonc"
if [ -f "$role_file" ] && [ "$ROLE" != "base" ]; then
local role_json
role_json=$(read_json "$role_file")
# Deep merge settings keys (role overrides base)
local role_settings
role_settings=$(echo "$role_json" | jq 'del(.permissions, .sandbox)')
merged_settings=$(echo "$merged_settings" | jq --argjson role "$role_settings" '. * $role')
# Concat permissions arrays (not deep merge, which would replace)
merged_allow=$(echo "$merged_allow" | jq --argjson r "$(echo "$role_json" | jq '.permissions.allow // []')" '. + $r')
merged_ask=$(echo "$merged_ask" | jq --argjson r "$(echo "$role_json" | jq '.permissions.ask // []')" '. + $r')
merged_deny=$(echo "$merged_deny" | jq --argjson r "$(echo "$role_json" | jq '.permissions.deny // []')" '. + $r')
# Merge permissions scalars from role (role overrides base)
local role_permissions_scalars
role_permissions_scalars=$(echo "$role_json" | jq '.permissions // {} | del(.allow, .ask, .deny)')
permissions_scalars=$(echo "$permissions_scalars" | jq --argjson r "$role_permissions_scalars" '. * $r')
# Merge sandbox scalars from role (role overrides base)
local role_sandbox_scalars
role_sandbox_scalars=$(echo "$role_json" | jq '.sandbox // {} | del(.network.allowedHosts, .filesystem.allowWrite, .filesystem, .network) + (if .network then {network: (.network | del(.allowedHosts))} else {} end) | del(.network | nulls) | del(.filesystem | nulls)')
sandbox_scalars=$(echo "$sandbox_scalars" | jq --argjson r "$role_sandbox_scalars" '. * $r')
# Concat sandbox arrays
sandbox_hosts=$(echo "$sandbox_hosts" | jq --argjson r "$(echo "$role_json" | jq '.sandbox.network.allowedHosts // []')" '. + $r')
sandbox_write_paths=$(echo "$sandbox_write_paths" | jq --argjson r "$(echo "$role_json" | jq '.sandbox.filesystem.allowWrite // []')" '. + $r')
echo " + Loaded $ROLE role"
fi
# --- Load stacks (sorted for determinism) ---
for stack_file in "$DIR"/claude/stacks/*.jsonc; do
[ -f "$stack_file" ] || continue
local stack_name
stack_name=$(basename "$stack_file" .jsonc)
local stack_json
stack_json=$(read_json "$stack_file")
# Concat permissions
merged_allow=$(echo "$merged_allow" | jq --argjson s "$(echo "$stack_json" | jq '.permissions.allow // []')" '. + $s')
merged_ask=$(echo "$merged_ask" | jq --argjson s "$(echo "$stack_json" | jq '.permissions.ask // []')" '. + $s')
merged_deny=$(echo "$merged_deny" | jq --argjson s "$(echo "$stack_json" | jq '.permissions.deny // []')" '. + $s')
# Concat sandbox arrays
sandbox_hosts=$(echo "$sandbox_hosts" | jq --argjson s "$(echo "$stack_json" | jq '.sandbox.network.allowedHosts // []')" '. + $s')
sandbox_write_paths=$(echo "$sandbox_write_paths" | jq --argjson s "$(echo "$stack_json" | jq '.sandbox.filesystem.allowWrite // []')" '. + $s')
echo " + Merged $stack_name stack"
done
# Deduplicate and sort all arrays
merged_allow=$(echo "$merged_allow" | jq 'unique | sort')
merged_ask=$(echo "$merged_ask" | jq 'unique | sort')
merged_deny=$(echo "$merged_deny" | jq 'unique | sort')
sandbox_hosts=$(echo "$sandbox_hosts" | jq 'unique | sort')
sandbox_write_paths=$(echo "$sandbox_write_paths" | jq 'unique | sort')
# Assemble final JSON: settings + permissions + sandbox
local final_settings
final_settings=$(echo "$merged_settings" | jq \
--argjson allow "$merged_allow" \
--argjson ask "$merged_ask" \
--argjson deny "$merged_deny" \
--argjson perm_scalars "$permissions_scalars" \
--argjson sandbox_scalars "$sandbox_scalars" \
--argjson hosts "$sandbox_hosts" \
--argjson write_paths "$sandbox_write_paths" \
'. + {
permissions: ($perm_scalars + {allow: $allow, ask: $ask, deny: $deny}),
sandbox: ($sandbox_scalars + {
network: ($sandbox_scalars.network // {} | . + {allowedHosts: $hosts}),
filesystem: {allowWrite: $write_paths}
})
}')
# Merge in local-only settings
final_settings=$(echo "$final_settings" | jq --argjson local "$local_settings" '. * $local')
# Expand leading ~/ in .env string values. GIT_CONFIG_GLOBAL and similar
# don't expand ~, so JSONC stays portable and we resolve to absolute paths
# here before writing settings.json.
final_settings=$(echo "$final_settings" | jq --arg home "$HOME" '
if .env then
.env |= with_entries(
if (.value | type) == "string" and (.value | startswith("~/"))
then .value = ($home + (.value | ltrimstr("~")))
else .
end
)
else . end
')
# Write to temp file and validate
echo "$final_settings" > "$temp_file"
if ! jq empty "$temp_file" 2> /dev/null; then
echo "Error: Generated invalid JSON"
rm "$temp_file"
exit 1
fi
# Backup existing settings (first time only)
if [ -f "$settings_file" ] && [ ! -f "$settings_file.backup" ]; then
cp "$settings_file" "$settings_file.backup"
echo " ℹ Backed up existing settings to $settings_file.backup"
fi
# Move temp file to final location
mv "$temp_file" "$settings_file"
echo " ✓ Settings generated successfully"
}
generate_settings
# Configure marketplaces (non-interactive)
configure_marketplaces() {
echo "Configuring marketplaces..."
local known_marketplaces_file="$HOME/.claude/plugins/known_marketplaces.json"
local marketplaces_dir="$HOME/.claude/plugins/marketplaces"
# Ensure directories exist
mkdir -p "$marketplaces_dir"
# Define marketplaces with their GitHub repos
# Format: "marketplace-id:github-repo"
local marketplaces=(
"pickled-claude-plugins:technicalpickles/pickled-claude-plugins"
"superpowers-marketplace:obra/superpowers"
"claude-notifications-go:777genius/claude-notifications-go"
)
# Read existing marketplaces or start with empty object
local current_marketplaces="{}"
if [ -f "$known_marketplaces_file" ]; then
current_marketplaces=$(cat "$known_marketplaces_file")
fi
local updated=false
for entry in "${marketplaces[@]}"; do
IFS=':' read -r marketplace_id github_repo <<< "$entry"
local install_location="$marketplaces_dir/$marketplace_id"
# Check if marketplace already exists in JSON
if echo "$current_marketplaces" | jq -e ".[\"$marketplace_id\"]" > /dev/null 2>&1; then
echo " ✓ $marketplace_id (already configured)"
else
echo " + Adding $marketplace_id..."
# Add marketplace entry to JSON
current_marketplaces=$(echo "$current_marketplaces" | jq \
--arg id "$marketplace_id" \
--arg repo "$github_repo" \
--arg location "$install_location" \
--arg timestamp "$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")" \
'. + {($id): {source: {source: "github", repo: $repo}, installLocation: $location, lastUpdated: $timestamp}}')
updated=true
echo " ✓ Added to known_marketplaces.json"
fi
# Clone marketplace repo if not present
if [ ! -d "$install_location" ]; then
echo " + Cloning $marketplace_id from $github_repo..."
if git clone "https://github.com/$github_repo.git" "$install_location" 2> /dev/null; then
echo " ✓ Cloned successfully"
else
echo " ✗ Failed to clone (continuing anyway)"
fi
else
echo " ✓ $marketplace_id repository exists"
fi
done
# Write updated marketplaces JSON if changes were made
if [ "$updated" = true ]; then
echo "$current_marketplaces" | jq '.' > "$known_marketplaces_file"
echo " ✓ Updated known_marketplaces.json"
fi
}
configure_marketplaces
echo ""
echo "✓ Claude Code configuration complete"
echo " Settings: $HOME/.claude/settings.json"
echo " Role: $ROLE"
echo ""
echo "Note: Global skills are restored from ~/.agents/.skill-lock.json via skills.sh"