check-prerequisites.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env bash
  2. # Consolidated prerequisite checking script
  3. #
  4. # This script provides unified prerequisite checking for Spec-Driven Development workflow.
  5. # It replaces the functionality previously spread across multiple scripts.
  6. #
  7. # Usage: ./check-prerequisites.sh [OPTIONS]
  8. #
  9. # OPTIONS:
  10. # --json Output in JSON format
  11. # --require-tasks Require tasks.md to exist (for implementation phase)
  12. # --include-tasks Include tasks.md in AVAILABLE_DOCS list
  13. # --paths-only Only output path variables (no validation)
  14. # --help, -h Show help message
  15. #
  16. # OUTPUTS:
  17. # JSON mode: {"FEATURE_DIR":"...", "AVAILABLE_DOCS":["..."]}
  18. # Text mode: FEATURE_DIR:... \n AVAILABLE_DOCS: \n ✓/✗ file.md
  19. # Paths only: REPO_ROOT: ... \n BRANCH: ... \n FEATURE_DIR: ... etc.
  20. set -e
  21. # Parse command line arguments
  22. JSON_MODE=false
  23. REQUIRE_TASKS=false
  24. INCLUDE_TASKS=false
  25. PATHS_ONLY=false
  26. for arg in "$@"; do
  27. case "$arg" in
  28. --json)
  29. JSON_MODE=true
  30. ;;
  31. --require-tasks)
  32. REQUIRE_TASKS=true
  33. ;;
  34. --include-tasks)
  35. INCLUDE_TASKS=true
  36. ;;
  37. --paths-only)
  38. PATHS_ONLY=true
  39. ;;
  40. --help|-h)
  41. cat << 'EOF'
  42. Usage: check-prerequisites.sh [OPTIONS]
  43. Consolidated prerequisite checking for Spec-Driven Development workflow.
  44. OPTIONS:
  45. --json Output in JSON format
  46. --require-tasks Require tasks.md to exist (for implementation phase)
  47. --include-tasks Include tasks.md in AVAILABLE_DOCS list
  48. --paths-only Only output path variables (no prerequisite validation)
  49. --help, -h Show this help message
  50. EXAMPLES:
  51. # Check task prerequisites (plan.md required)
  52. ./check-prerequisites.sh --json
  53. # Check implementation prerequisites (plan.md + tasks.md required)
  54. ./check-prerequisites.sh --json --require-tasks --include-tasks
  55. # Get feature paths only (no validation)
  56. ./check-prerequisites.sh --paths-only
  57. EOF
  58. exit 0
  59. ;;
  60. *)
  61. echo "ERROR: Unknown option '$arg'. Use --help for usage information." >&2
  62. exit 1
  63. ;;
  64. esac
  65. done
  66. # Source common functions
  67. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  68. source "$SCRIPT_DIR/common.sh"
  69. # Get feature paths and validate branch
  70. eval $(get_feature_paths)
  71. check_feature_branch "$CURRENT_BRANCH" "$HAS_GIT" || exit 1
  72. # If paths-only mode, output paths and exit (support JSON + paths-only combined)
  73. if $PATHS_ONLY; then
  74. if $JSON_MODE; then
  75. # Minimal JSON paths payload (no validation performed)
  76. printf '{"REPO_ROOT":"%s","BRANCH":"%s","FEATURE_DIR":"%s","FEATURE_SPEC":"%s","IMPL_PLAN":"%s","TASKS":"%s"}\n' \
  77. "$REPO_ROOT" "$CURRENT_BRANCH" "$FEATURE_DIR" "$FEATURE_SPEC" "$IMPL_PLAN" "$TASKS"
  78. else
  79. echo "REPO_ROOT: $REPO_ROOT"
  80. echo "BRANCH: $CURRENT_BRANCH"
  81. echo "FEATURE_DIR: $FEATURE_DIR"
  82. echo "FEATURE_SPEC: $FEATURE_SPEC"
  83. echo "IMPL_PLAN: $IMPL_PLAN"
  84. echo "TASKS: $TASKS"
  85. fi
  86. exit 0
  87. fi
  88. # Validate required directories and files
  89. if [[ ! -d "$FEATURE_DIR" ]]; then
  90. echo "ERROR: Feature directory not found: $FEATURE_DIR" >&2
  91. echo "Run /specify first to create the feature structure." >&2
  92. exit 1
  93. fi
  94. if [[ ! -f "$IMPL_PLAN" ]]; then
  95. echo "ERROR: plan.md not found in $FEATURE_DIR" >&2
  96. echo "Run /plan first to create the implementation plan." >&2
  97. exit 1
  98. fi
  99. # Check for tasks.md if required
  100. if $REQUIRE_TASKS && [[ ! -f "$TASKS" ]]; then
  101. echo "ERROR: tasks.md not found in $FEATURE_DIR" >&2
  102. echo "Run /tasks first to create the task list." >&2
  103. exit 1
  104. fi
  105. # Build list of available documents
  106. docs=()
  107. # Always check these optional docs
  108. [[ -f "$RESEARCH" ]] && docs+=("research.md")
  109. [[ -f "$DATA_MODEL" ]] && docs+=("data-model.md")
  110. # Check contracts directory (only if it exists and has files)
  111. if [[ -d "$CONTRACTS_DIR" ]] && [[ -n "$(ls -A "$CONTRACTS_DIR" 2>/dev/null)" ]]; then
  112. docs+=("contracts/")
  113. fi
  114. [[ -f "$QUICKSTART" ]] && docs+=("quickstart.md")
  115. # Include tasks.md if requested and it exists
  116. if $INCLUDE_TASKS && [[ -f "$TASKS" ]]; then
  117. docs+=("tasks.md")
  118. fi
  119. # Output results
  120. if $JSON_MODE; then
  121. # Build JSON array of documents
  122. if [[ ${#docs[@]} -eq 0 ]]; then
  123. json_docs="[]"
  124. else
  125. json_docs=$(printf '"%s",' "${docs[@]}")
  126. json_docs="[${json_docs%,}]"
  127. fi
  128. printf '{"FEATURE_DIR":"%s","AVAILABLE_DOCS":%s}\n' "$FEATURE_DIR" "$json_docs"
  129. else
  130. # Text output
  131. echo "FEATURE_DIR:$FEATURE_DIR"
  132. echo "AVAILABLE_DOCS:"
  133. # Show status of each potential document
  134. check_file "$RESEARCH" "research.md"
  135. check_file "$DATA_MODEL" "data-model.md"
  136. check_dir "$CONTRACTS_DIR" "contracts/"
  137. check_file "$QUICKSTART" "quickstart.md"
  138. if $INCLUDE_TASKS; then
  139. check_file "$TASKS" "tasks.md"
  140. fi
  141. fi