-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathsync-to-profile.sh
More file actions
executable file
·528 lines (440 loc) · 14.3 KB
/
sync-to-profile.sh
File metadata and controls
executable file
·528 lines (440 loc) · 14.3 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/bin/bash
# =============================================================================
# Agent OS Sync to Profile Script
# Syncs project standards back to a base profile for reuse
# =============================================================================
set -e
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE_DIR="$(dirname "$SCRIPT_DIR")"
PROJECT_DIR="$(pwd)"
# Source common functions
source "$SCRIPT_DIR/common-functions.sh"
# -----------------------------------------------------------------------------
# Default Values
# -----------------------------------------------------------------------------
VERBOSE="false"
PROFILE=""
NEW_PROFILE=""
SYNC_ALL="false"
OVERWRITE="false"
# Arrays for file handling
declare -a STANDARDS_FILES
declare -a SELECTED_FILES
# -----------------------------------------------------------------------------
# Help Function
# -----------------------------------------------------------------------------
show_help() {
cat << EOF
Usage: $0 [OPTIONS]
Sync project standards back to a base profile for reuse.
Options:
--profile <name> Target profile (skips selection prompt)
--new-profile <name> Create a new profile with these standards
--all Sync all standards (skips file selection)
--overwrite Overwrite existing files without prompting
--verbose Show detailed output
-h, --help Show this help message
Examples:
$0
$0 --profile rails
$0 --all --overwrite
$0 --new-profile nextjs --all
EOF
exit 0
}
# -----------------------------------------------------------------------------
# Parse Command Line Arguments
# -----------------------------------------------------------------------------
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--profile)
PROFILE="$2"
shift 2
;;
--new-profile)
NEW_PROFILE="$2"
shift 2
;;
--all)
SYNC_ALL="true"
shift
;;
--overwrite)
OVERWRITE="true"
shift
;;
--verbose)
VERBOSE="true"
shift
;;
-h|--help)
show_help
;;
*)
print_error "Unknown option: $1"
show_help
;;
esac
done
}
# -----------------------------------------------------------------------------
# Validation Functions
# -----------------------------------------------------------------------------
validate_base_installation() {
if [[ ! -d "$BASE_DIR" ]]; then
print_error "Agent OS base installation not found"
exit 1
fi
if [[ ! -d "$BASE_DIR/profiles" ]]; then
print_error "No profiles directory in base installation"
exit 1
fi
}
validate_project_standards() {
local standards_dir="$PROJECT_DIR/agent-os/standards"
if [[ ! -d "$standards_dir" ]]; then
print_error "No standards directory found at agent-os/standards/"
echo ""
echo "Run project-install.sh first to set up Agent OS in this project."
exit 1
fi
}
# -----------------------------------------------------------------------------
# Standards Discovery
# -----------------------------------------------------------------------------
find_standards_files() {
local standards_dir="$PROJECT_DIR/agent-os/standards"
STANDARDS_FILES=()
# Find all .md files, excluding .backups directory
while IFS= read -r -d '' file; do
local relative_path="${file#$standards_dir/}"
STANDARDS_FILES+=("$relative_path")
done < <(find "$standards_dir" -name "*.md" -type f ! -path "*/.backups/*" -print0 2>/dev/null | sort -z)
if [[ ${#STANDARDS_FILES[@]} -eq 0 ]]; then
print_error "No standards to sync."
echo ""
echo "Create standards first using /discover-standards or manually."
exit 1
fi
print_verbose "Found ${#STANDARDS_FILES[@]} standards files"
}
# -----------------------------------------------------------------------------
# Profile Selection
# -----------------------------------------------------------------------------
list_existing_profiles() {
local profiles=()
for dir in "$BASE_DIR/profiles"/*/; do
if [[ -d "$dir" ]]; then
local name=$(basename "$dir")
profiles+=("$name")
fi
done
echo "${profiles[@]}"
}
select_profile() {
# If --new-profile was specified, use that
if [[ -n "$NEW_PROFILE" ]]; then
PROFILE="$NEW_PROFILE"
create_profile_if_needed
return
fi
# If --profile was specified, validate it
if [[ -n "$PROFILE" ]]; then
if [[ ! -d "$BASE_DIR/profiles/$PROFILE" ]]; then
echo ""
read -p "Profile '$PROFILE' doesn't exist. Create it? (y/n): " create_choice
if [[ "$create_choice" =~ ^[Yy] ]]; then
create_new_profile "$PROFILE"
else
print_error "Cancelled."
exit 1
fi
fi
return
fi
# Interactive profile selection
echo ""
print_status "Available profiles:"
echo ""
local profiles=($(list_existing_profiles))
local i=1
for profile in "${profiles[@]}"; do
echo " $i) $profile"
((i++))
done
echo " $i) [Create new profile]"
echo ""
local max_choice=$i
local choice
while true; do
read -p "Select profile (1-$max_choice): " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le "$max_choice" ]]; then
break
fi
echo "Invalid choice. Please enter a number between 1 and $max_choice."
done
if [[ "$choice" -eq "$max_choice" ]]; then
# Create new profile
echo ""
read -p "Enter new profile name: " PROFILE
if [[ -z "$PROFILE" ]]; then
print_error "Profile name cannot be empty."
exit 1
fi
create_new_profile "$PROFILE"
else
PROFILE="${profiles[$((choice-1))]}"
fi
print_verbose "Selected profile: $PROFILE"
}
create_profile_if_needed() {
if [[ ! -d "$BASE_DIR/profiles/$PROFILE" ]]; then
create_new_profile "$PROFILE"
fi
}
create_new_profile() {
local name="$1"
local profile_dir="$BASE_DIR/profiles/$name"
mkdir -p "$profile_dir/standards"
print_success "Created new profile: $name"
}
# -----------------------------------------------------------------------------
# File Selection
# -----------------------------------------------------------------------------
select_files() {
# If --all was specified, select all files
if [[ "$SYNC_ALL" == "true" ]]; then
SELECTED_FILES=("${STANDARDS_FILES[@]}")
print_verbose "Selected all ${#SELECTED_FILES[@]} files"
return
fi
# Initialize selection array (all selected by default)
local selected=()
for ((i=0; i<${#STANDARDS_FILES[@]}; i++)); do
selected[$i]=1
done
# Calculate lines to clear (files + 5 for header/footer)
local lines_to_clear=$((${#STANDARDS_FILES[@]} + 7))
display_file_selection() {
echo ""
print_status "Select standards to sync:"
echo ""
local i=1
for file in "${STANDARDS_FILES[@]}"; do
if [[ ${selected[$((i-1))]} -eq 1 ]]; then
echo " $i) [x] $file"
else
echo " $i) [ ] $file"
fi
((i++))
done
echo ""
echo ""
echo " Enter number to toggle a) All n) None d) Done"
echo ""
}
clear_display() {
# Move cursor up and clear lines
for ((i=0; i<lines_to_clear; i++)); do
tput cuu1 2>/dev/null || echo -ne "\033[1A"
tput el 2>/dev/null || echo -ne "\033[2K"
done
}
local first_display=true
while true; do
if [[ "$first_display" == "true" ]]; then
first_display=false
else
clear_display
fi
display_file_selection
read -p "Toggle (1-${#STANDARDS_FILES[@]}), a, n, or d: " choice
case "$choice" in
a|A)
for ((i=0; i<${#STANDARDS_FILES[@]}; i++)); do
selected[$i]=1
done
;;
n|N)
for ((i=0; i<${#STANDARDS_FILES[@]}; i++)); do
selected[$i]=0
done
;;
d|D)
break
;;
*)
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le ${#STANDARDS_FILES[@]} ]]; then
local idx=$((choice-1))
if [[ ${selected[$idx]} -eq 1 ]]; then
selected[$idx]=0
else
selected[$idx]=1
fi
fi
# Invalid input just redisplays
;;
esac
done
# Build selected files array
SELECTED_FILES=()
for ((i=0; i<${#STANDARDS_FILES[@]}; i++)); do
if [[ ${selected[$i]} -eq 1 ]]; then
SELECTED_FILES+=("${STANDARDS_FILES[$i]}")
fi
done
if [[ ${#SELECTED_FILES[@]} -eq 0 ]]; then
print_error "No files selected."
exit 1
fi
print_verbose "Selected ${#SELECTED_FILES[@]} files"
}
# -----------------------------------------------------------------------------
# Conflict Detection
# -----------------------------------------------------------------------------
check_conflicts() {
local profile_standards="$BASE_DIR/profiles/$PROFILE/standards"
local conflicts=()
for file in "${SELECTED_FILES[@]}"; do
if [[ -f "$profile_standards/$file" ]]; then
conflicts+=("$file")
fi
done
if [[ ${#conflicts[@]} -eq 0 ]]; then
return 0
fi
# If --overwrite specified, just backup and continue
if [[ "$OVERWRITE" == "true" ]]; then
backup_files "${conflicts[@]}"
return 0
fi
# Prompt user
echo ""
print_warning "${#conflicts[@]} file(s) already exist in profile '$PROFILE':"
for file in "${conflicts[@]}"; do
echo " - $file"
done
echo ""
while true; do
echo "What do you want to do?"
echo " 1) Overwrite all (with backup)"
echo " 2) Skip existing files"
echo " 3) Cancel"
echo ""
read -p "Choice (1-3): " conflict_choice
case "$conflict_choice" in
1)
backup_files "${conflicts[@]}"
return 0
;;
2)
# Remove conflicts from selected files
local new_selected=()
for file in "${SELECTED_FILES[@]}"; do
local is_conflict=false
for conflict in "${conflicts[@]}"; do
if [[ "$file" == "$conflict" ]]; then
is_conflict=true
break
fi
done
if [[ "$is_conflict" == "false" ]]; then
new_selected+=("$file")
fi
done
SELECTED_FILES=("${new_selected[@]}")
if [[ ${#SELECTED_FILES[@]} -eq 0 ]]; then
print_warning "No files left to sync after skipping conflicts."
exit 0
fi
return 0
;;
3)
print_error "Cancelled."
exit 1
;;
*)
echo "Invalid choice."
;;
esac
done
}
# -----------------------------------------------------------------------------
# Backup Functions
# -----------------------------------------------------------------------------
backup_files() {
local files=("$@")
if [[ ${#files[@]} -eq 0 ]]; then
return
fi
local profile_standards="$BASE_DIR/profiles/$PROFILE/standards"
local timestamp=$(date +"%Y-%m-%d-%H%M")
local backup_dir="$profile_standards/.backups/$timestamp"
mkdir -p "$backup_dir"
local backup_count=0
for file in "${files[@]}"; do
local source_file="$profile_standards/$file"
local backup_file="$backup_dir/$file"
if [[ -f "$source_file" ]]; then
mkdir -p "$(dirname "$backup_file")"
cp "$source_file" "$backup_file"
((++backup_count))
print_verbose "Backed up: $file"
fi
done
if [[ "$backup_count" -gt 0 ]]; then
print_success "Backed up $backup_count file(s) to .backups/$timestamp/"
fi
}
# -----------------------------------------------------------------------------
# Sync Execution
# -----------------------------------------------------------------------------
execute_sync() {
local project_standards="$PROJECT_DIR/agent-os/standards"
local profile_standards="$BASE_DIR/profiles/$PROFILE/standards"
local sync_count=0
for file in "${SELECTED_FILES[@]}"; do
local source_file="$project_standards/$file"
local dest_file="$profile_standards/$file"
# Create directory if needed
mkdir -p "$(dirname "$dest_file")"
# Copy the file
cp "$source_file" "$dest_file"
((++sync_count))
print_verbose "Synced: $file"
done
echo ""
print_success "Synced $sync_count file(s) to profile '$PROFILE'"
}
# -----------------------------------------------------------------------------
# Main Execution
# -----------------------------------------------------------------------------
main() {
print_section "Agent OS Sync to Profile"
# Parse arguments
parse_arguments "$@"
# Validations
validate_base_installation
validate_project_standards
# Find standards files
find_standards_files
# Select target profile
select_profile
# Select files to sync
select_files
# Show summary
echo ""
print_status "Sync summary:"
echo " Profile: $PROFILE"
echo " Files to sync: ${#SELECTED_FILES[@]}"
echo ""
# Check for conflicts and handle them
check_conflicts
# Execute sync
execute_sync
echo ""
}
# Run main function
main "$@"