-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathralph_enable_ci.sh
More file actions
executable file
·405 lines (355 loc) · 12.3 KB
/
ralph_enable_ci.sh
File metadata and controls
executable file
·405 lines (355 loc) · 12.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
#!/bin/bash
# Ralph Enable CI - Non-Interactive Version for Automation
# Adds Ralph configuration with sensible defaults
#
# Usage:
# ralph enable-ci # Auto-detect and enable
# ralph enable-ci --from beads # With specific task source
# ralph enable-ci --json # Output JSON result
#
# Exit codes:
# 0 - Success: Ralph enabled
# 1 - Error: General error
# 2 - Already enabled (use --force to override)
# 3 - Invalid arguments
# 4 - File not found (e.g., PRD file)
# 5 - Dependency missing (e.g., jq for --json)
#
# Version: 0.11.0
set -e
# Get script directory for library loading
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Try to load libraries from global installation first, then local
RALPH_HOME="${RALPH_HOME:-$HOME/.ralph}"
if [[ -f "$RALPH_HOME/lib/enable_core.sh" ]]; then
LIB_DIR="$RALPH_HOME/lib"
elif [[ -f "$SCRIPT_DIR/lib/enable_core.sh" ]]; then
LIB_DIR="$SCRIPT_DIR/lib"
else
echo '{"error": "Cannot find Ralph libraries", "code": 1}' >&2
exit 1
fi
# Disable colors for CI
export ENABLE_USE_COLORS="false"
# Source libraries
source "$LIB_DIR/enable_core.sh"
source "$LIB_DIR/task_sources.sh"
# =============================================================================
# CONFIGURATION
# =============================================================================
# Command line options
FORCE_OVERWRITE=false
TASK_SOURCE=""
PRD_FILE=""
GITHUB_LABEL="ralph-task"
PROJECT_NAME=""
PROJECT_TYPE=""
OUTPUT_JSON=false
QUIET=false
SHOW_HELP=false
# Version
VERSION="0.11.0"
# =============================================================================
# HELP
# =============================================================================
show_help() {
cat << EOF
Ralph Enable CI - Non-Interactive Version for Automation
Usage: ralph enable-ci [OPTIONS]
Options:
--from <source> Import tasks from: beads, github, prd, none
--prd <file> PRD file to convert (when --from prd)
--label <label> GitHub label filter (default: ralph-task)
--project-name <name> Override detected project name
--project-type <type> Override detected type (typescript, python, etc.)
--force Overwrite existing .ralph/ configuration
--json Output result as JSON
--quiet Suppress non-error output
-h, --help Show this help message
-v, --version Show version
Exit Codes:
0 - Success: Ralph enabled
1 - Error: General error
2 - Already enabled: Use --force to override
3 - Invalid arguments
4 - File not found (e.g., PRD file)
5 - Dependency missing (e.g., jq for --json)
Examples:
# Auto-detect and enable with defaults
ralph enable-ci
# Enable with beads tasks
ralph enable-ci --from beads
# Enable with GitHub issues
ralph enable-ci --from github --label "sprint-1"
# Enable with PRD conversion
ralph enable-ci --from prd --prd docs/requirements.md
# Force overwrite and output JSON
ralph enable-ci --force --json
# Override project detection
ralph enable-ci --project-name my-app --project-type typescript
JSON Output Format:
{
"success": true,
"project_name": "my-project",
"project_type": "typescript",
"files_created": [".ralph/PROMPT.md", ...],
"tasks_imported": 15,
"message": "Ralph enabled successfully"
}
EOF
}
# =============================================================================
# ARGUMENT PARSING
# =============================================================================
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
--from)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
TASK_SOURCE="$2"
shift 2
else
output_error "--from requires a source (beads, github, prd, none)"
exit $ENABLE_INVALID_ARGS
fi
;;
--prd)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
PRD_FILE="$2"
shift 2
else
output_error "--prd requires a file path"
exit $ENABLE_INVALID_ARGS
fi
;;
--label)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
GITHUB_LABEL="$2"
shift 2
else
output_error "--label requires a label name"
exit $ENABLE_INVALID_ARGS
fi
;;
--project-name)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
PROJECT_NAME="$2"
shift 2
else
output_error "--project-name requires a name"
exit $ENABLE_INVALID_ARGS
fi
;;
--project-type)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
PROJECT_TYPE="$2"
shift 2
else
output_error "--project-type requires a type"
exit $ENABLE_INVALID_ARGS
fi
;;
--force)
FORCE_OVERWRITE=true
shift
;;
--json)
if ! command -v jq &>/dev/null; then
echo "Error: --json requires jq to be installed" >&2
exit $ENABLE_DEPENDENCY_MISSING
fi
OUTPUT_JSON=true
shift
;;
--quiet)
QUIET=true
shift
;;
-h|--help)
SHOW_HELP=true
shift
;;
-v|--version)
if [[ "$OUTPUT_JSON" == "true" ]]; then
echo "{\"version\": \"$VERSION\"}"
else
echo "ralph enable-ci version $VERSION"
fi
exit 0
;;
*)
output_error "Unknown option: $1"
exit $ENABLE_INVALID_ARGS
;;
esac
done
}
# =============================================================================
# OUTPUT FUNCTIONS
# =============================================================================
# Track created files for JSON output
declare -a CREATED_FILES=()
TASKS_IMPORTED=0
output_message() {
if [[ "$QUIET" != "true" && "$OUTPUT_JSON" != "true" ]]; then
echo "$1"
fi
}
output_error() {
if [[ "$OUTPUT_JSON" == "true" ]]; then
echo "{\"error\": \"$1\", \"code\": 1}" >&2
else
echo "Error: $1" >&2
fi
}
output_success() {
local project_name="$1"
local project_type="$2"
if [[ "$OUTPUT_JSON" == "true" ]]; then
local files_json
files_json=$(printf '%s\n' "${CREATED_FILES[@]}" | jq -R . | jq -s .)
cat << EOF
{
"success": true,
"project_name": "$project_name",
"project_type": "$project_type",
"files_created": $files_json,
"tasks_imported": $TASKS_IMPORTED,
"message": "Ralph enabled successfully"
}
EOF
else
echo "Ralph enabled successfully for: $project_name ($project_type)"
echo "Files created: ${#CREATED_FILES[@]}"
echo "Tasks imported: $TASKS_IMPORTED"
fi
}
output_already_enabled() {
if [[ "$OUTPUT_JSON" == "true" ]]; then
echo '{"success": false, "code": 2, "message": "Ralph already enabled. Use --force to override."}'
else
echo "Ralph is already enabled in this project. Use --force to override."
fi
}
# =============================================================================
# MAIN LOGIC
# =============================================================================
main() {
# Parse arguments
parse_arguments "$@"
# Show help if requested
if [[ "$SHOW_HELP" == "true" ]]; then
show_help
exit 0
fi
output_message "Ralph Enable CI - Non-Interactive Mode"
output_message ""
# Check existing state (use || true to prevent set -e from exiting)
check_existing_ralph || true
if [[ "$RALPH_STATE" == "complete" && "$FORCE_OVERWRITE" != "true" ]]; then
output_already_enabled
exit $ENABLE_ALREADY_ENABLED
fi
# Detect project context
detect_project_context
output_message "Detected: $DETECTED_PROJECT_NAME ($DETECTED_PROJECT_TYPE)"
# Override with CLI options if provided
if [[ -n "$PROJECT_NAME" ]]; then
DETECTED_PROJECT_NAME="$PROJECT_NAME"
fi
if [[ -n "$PROJECT_TYPE" ]]; then
DETECTED_PROJECT_TYPE="$PROJECT_TYPE"
fi
# Auto-detect task source if not specified
if [[ -z "$TASK_SOURCE" ]]; then
detect_task_sources
if [[ "$DETECTED_BEADS_AVAILABLE" == "true" ]]; then
TASK_SOURCE="beads"
output_message "Auto-detected task source: beads"
elif [[ "$DETECTED_GITHUB_AVAILABLE" == "true" ]]; then
TASK_SOURCE="github"
output_message "Auto-detected task source: github"
elif [[ ${#DETECTED_PRD_FILES[@]} -gt 0 ]]; then
TASK_SOURCE="prd"
PRD_FILE="${DETECTED_PRD_FILES[0]}"
output_message "Auto-detected task source: prd ($PRD_FILE)"
else
TASK_SOURCE="none"
output_message "No task sources detected, using defaults"
fi
fi
# Import tasks
local imported_tasks=""
case "$TASK_SOURCE" in
beads)
if beads_tasks=$(fetch_beads_tasks 2>/dev/null); then
imported_tasks="$beads_tasks"
TASKS_IMPORTED=$(echo "$imported_tasks" | grep -c '^\- \[' || echo "0")
output_message "Imported $TASKS_IMPORTED tasks from beads"
fi
;;
github)
if github_tasks=$(fetch_github_tasks "$GITHUB_LABEL" 2>/dev/null); then
imported_tasks="$github_tasks"
TASKS_IMPORTED=$(echo "$imported_tasks" | grep -c '^\- \[' || echo "0")
output_message "Imported $TASKS_IMPORTED tasks from GitHub"
fi
;;
prd)
if [[ -n "$PRD_FILE" && -f "$PRD_FILE" ]]; then
if prd_tasks=$(extract_prd_tasks "$PRD_FILE" 2>/dev/null); then
imported_tasks="$prd_tasks"
TASKS_IMPORTED=$(echo "$imported_tasks" | grep -c '^\- \[' || echo "0")
output_message "Extracted $TASKS_IMPORTED tasks from PRD"
fi
else
output_error "PRD file not found: $PRD_FILE"
exit $ENABLE_FILE_NOT_FOUND
fi
;;
none|"")
output_message "Skipping task import"
;;
*)
output_error "Unknown task source: $TASK_SOURCE"
exit $ENABLE_ERROR
;;
esac
# Set up enable environment
export ENABLE_FORCE="$FORCE_OVERWRITE"
export ENABLE_SKIP_TASKS="false"
export ENABLE_PROJECT_NAME="$DETECTED_PROJECT_NAME"
export ENABLE_PROJECT_TYPE="$DETECTED_PROJECT_TYPE"
export ENABLE_TASK_CONTENT="$imported_tasks"
# Run core enable logic
output_message ""
output_message "Creating Ralph configuration..."
# Suppress enable_ralph_in_directory output when in JSON mode
if [[ "$OUTPUT_JSON" == "true" ]]; then
if ! enable_ralph_in_directory >/dev/null 2>&1; then
output_error "Failed to enable Ralph"
exit $ENABLE_ERROR
fi
else
if ! enable_ralph_in_directory; then
output_error "Failed to enable Ralph"
exit $ENABLE_ERROR
fi
fi
# Track created files
[[ -f ".ralph/PROMPT.md" ]] && CREATED_FILES+=(".ralph/PROMPT.md")
[[ -f ".ralph/fix_plan.md" ]] && CREATED_FILES+=(".ralph/fix_plan.md")
[[ -f ".ralph/AGENT.md" ]] && CREATED_FILES+=(".ralph/AGENT.md")
[[ -f ".ralphrc" ]] && CREATED_FILES+=(".ralphrc")
# Verify required files exist
if [[ ! -f ".ralph/PROMPT.md" ]] || [[ ! -f ".ralph/fix_plan.md" ]]; then
output_error "Required files were not created"
exit $ENABLE_ERROR
fi
# Output success
output_message ""
output_success "$DETECTED_PROJECT_NAME" "$DETECTED_PROJECT_TYPE"
exit $ENABLE_SUCCESS
}
# Run main
main "$@"