|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Consolidated prerequisite checking script |
| 4 | +# |
| 5 | +# This script provides unified prerequisite checking for Spec-Driven Development workflow. |
| 6 | +# It replaces the functionality previously spread across multiple scripts. |
| 7 | +# |
| 8 | +# Usage: ./check-prerequisites.sh [OPTIONS] |
| 9 | +# |
| 10 | +# OPTIONS: |
| 11 | +# --json Output in JSON format |
| 12 | +# --require-tasks Require tasks.md to exist (for implementation phase) |
| 13 | +# --include-tasks Include tasks.md in AVAILABLE_DOCS list |
| 14 | +# --paths-only Only output path variables (no validation) |
| 15 | +# --template NAME Include composed template content in JSON output |
| 16 | +# --help, -h Show help message |
| 17 | +# |
| 18 | +# OUTPUTS: |
| 19 | +# JSON mode: {"FEATURE_DIR":"...", "AVAILABLE_DOCS":["..."]} |
| 20 | +# Text mode: FEATURE_DIR:... \n AVAILABLE_DOCS: \n ✓/✗ file.md |
| 21 | +# Paths only: REPO_ROOT: ... \n BRANCH: ... \n FEATURE_DIR: ... etc. |
| 22 | + |
| 23 | +set -e |
| 24 | + |
| 25 | +# Parse command line arguments |
| 26 | +JSON_MODE=false |
| 27 | +REQUIRE_TASKS=false |
| 28 | +INCLUDE_TASKS=false |
| 29 | +PATHS_ONLY=false |
| 30 | +TEMPLATE_NAME="" |
| 31 | + |
| 32 | +while [[ $# -gt 0 ]]; do |
| 33 | + case "$1" in |
| 34 | + --json) |
| 35 | + JSON_MODE=true |
| 36 | + ;; |
| 37 | + --require-tasks) |
| 38 | + REQUIRE_TASKS=true |
| 39 | + ;; |
| 40 | + --include-tasks) |
| 41 | + INCLUDE_TASKS=true |
| 42 | + ;; |
| 43 | + --paths-only) |
| 44 | + PATHS_ONLY=true |
| 45 | + ;; |
| 46 | + --template) |
| 47 | + shift |
| 48 | + if [[ $# -eq 0 ]]; then |
| 49 | + echo "ERROR: --template requires a template name" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + TEMPLATE_NAME="$1" |
| 53 | + ;; |
| 54 | + --help|-h) |
| 55 | + cat << 'EOF' |
| 56 | +Usage: check-prerequisites.sh [OPTIONS] |
| 57 | +
|
| 58 | +Consolidated prerequisite checking for Spec-Driven Development workflow. |
| 59 | +
|
| 60 | +OPTIONS: |
| 61 | + --json Output in JSON format |
| 62 | + --require-tasks Require tasks.md to exist (for implementation phase) |
| 63 | + --include-tasks Include tasks.md in AVAILABLE_DOCS list |
| 64 | + --paths-only Only output path variables (no prerequisite validation) |
| 65 | + --template NAME Include composed template content in JSON output |
| 66 | + --help, -h Show this help message |
| 67 | +
|
| 68 | +EXAMPLES: |
| 69 | + # Check task prerequisites (plan.md required) |
| 70 | + ./check-prerequisites.sh --json |
| 71 | +
|
| 72 | + # Check implementation prerequisites (plan.md + tasks.md required) |
| 73 | + ./check-prerequisites.sh --json --require-tasks --include-tasks |
| 74 | +
|
| 75 | + # Get feature paths only (no validation) |
| 76 | + ./check-prerequisites.sh --paths-only |
| 77 | +
|
| 78 | +EOF |
| 79 | + exit 0 |
| 80 | + ;; |
| 81 | + *) |
| 82 | + echo "ERROR: Unknown option '$1'. Use --help for usage information." >&2 |
| 83 | + exit 1 |
| 84 | + ;; |
| 85 | + esac |
| 86 | + shift |
| 87 | +done |
| 88 | + |
| 89 | +# Source common functions |
| 90 | +SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 91 | +source "$SCRIPT_DIR/common.sh" |
| 92 | + |
| 93 | +# Get feature paths. |
| 94 | +# In --paths-only mode this is pure resolution, so pass --no-persist to opt out |
| 95 | +# of the feature.json write side effect (issue #3025). |
| 96 | +if $PATHS_ONLY; then |
| 97 | + _paths_output=$(get_feature_paths --no-persist) || { echo "ERROR: Failed to resolve feature paths" >&2; exit 1; } |
| 98 | +else |
| 99 | + _paths_output=$(get_feature_paths) || { echo "ERROR: Failed to resolve feature paths" >&2; exit 1; } |
| 100 | +fi |
| 101 | +eval "$_paths_output" |
| 102 | +unset _paths_output |
| 103 | + |
| 104 | +# If paths-only mode, output paths and exit (no validation) |
| 105 | +if $PATHS_ONLY; then |
| 106 | + if $JSON_MODE; then |
| 107 | + # Minimal JSON paths payload (no validation performed) |
| 108 | + if has_jq; then |
| 109 | + jq -cn \ |
| 110 | + --arg repo_root "$REPO_ROOT" \ |
| 111 | + --arg branch "$CURRENT_BRANCH" \ |
| 112 | + --arg feature_dir "$FEATURE_DIR" \ |
| 113 | + --arg feature_spec "$FEATURE_SPEC" \ |
| 114 | + --arg impl_plan "$IMPL_PLAN" \ |
| 115 | + --arg tasks "$TASKS" \ |
| 116 | + '{REPO_ROOT:$repo_root,BRANCH:$branch,FEATURE_DIR:$feature_dir,FEATURE_SPEC:$feature_spec,IMPL_PLAN:$impl_plan,TASKS:$tasks}' |
| 117 | + else |
| 118 | + printf '{"REPO_ROOT":"%s","BRANCH":"%s","FEATURE_DIR":"%s","FEATURE_SPEC":"%s","IMPL_PLAN":"%s","TASKS":"%s"}\n' \ |
| 119 | + "$(json_escape "$REPO_ROOT")" "$(json_escape "$CURRENT_BRANCH")" "$(json_escape "$FEATURE_DIR")" "$(json_escape "$FEATURE_SPEC")" "$(json_escape "$IMPL_PLAN")" "$(json_escape "$TASKS")" |
| 120 | + fi |
| 121 | + else |
| 122 | + echo "REPO_ROOT: $REPO_ROOT" |
| 123 | + echo "BRANCH: $CURRENT_BRANCH" |
| 124 | + echo "FEATURE_DIR: $FEATURE_DIR" |
| 125 | + echo "FEATURE_SPEC: $FEATURE_SPEC" |
| 126 | + echo "IMPL_PLAN: $IMPL_PLAN" |
| 127 | + echo "TASKS: $TASKS" |
| 128 | + fi |
| 129 | + exit 0 |
| 130 | +fi |
| 131 | + |
| 132 | +# Validate required directories and files |
| 133 | +if [[ ! -d "$FEATURE_DIR" ]]; then |
| 134 | + echo "ERROR: Feature directory not found: $FEATURE_DIR" >&2 |
| 135 | + echo "Run \$speckit-specify first to create the feature structure." >&2 |
| 136 | + exit 1 |
| 137 | +fi |
| 138 | + |
| 139 | +if [[ ! -f "$IMPL_PLAN" ]]; then |
| 140 | + echo "ERROR: plan.md not found in $FEATURE_DIR" >&2 |
| 141 | + echo "Run \$speckit-plan first to create the implementation plan." >&2 |
| 142 | + exit 1 |
| 143 | +fi |
| 144 | + |
| 145 | +# Check for tasks.md if required |
| 146 | +if $REQUIRE_TASKS && [[ ! -f "$TASKS" ]]; then |
| 147 | + echo "ERROR: tasks.md not found in $FEATURE_DIR" >&2 |
| 148 | + echo "Run \$speckit-tasks first to create the task list." >&2 |
| 149 | + exit 1 |
| 150 | +fi |
| 151 | + |
| 152 | +# Build list of available documents |
| 153 | +docs=() |
| 154 | + |
| 155 | +# Always check these optional docs |
| 156 | +[[ -f "$RESEARCH" ]] && docs+=("research.md") |
| 157 | +[[ -f "$DATA_MODEL" ]] && docs+=("data-model.md") |
| 158 | + |
| 159 | +# Check contracts directory (only if it exists and has files) |
| 160 | +if [[ -d "$CONTRACTS_DIR" ]] && [[ -n "$(ls -A "$CONTRACTS_DIR" 2>/dev/null)" ]]; then |
| 161 | + docs+=("contracts/") |
| 162 | +fi |
| 163 | + |
| 164 | +[[ -f "$QUICKSTART" ]] && docs+=("quickstart.md") |
| 165 | + |
| 166 | +# Include tasks.md if requested and it exists |
| 167 | +if $INCLUDE_TASKS && [[ -f "$TASKS" ]]; then |
| 168 | + docs+=("tasks.md") |
| 169 | +fi |
| 170 | + |
| 171 | +TEMPLATE_CONTENT="" |
| 172 | +if [[ -n "$TEMPLATE_NAME" ]]; then |
| 173 | + if TEMPLATE_CONTENT=$(resolve_template_content "$TEMPLATE_NAME" "$REPO_ROOT"; status=$?; printf x; exit "$status"); then |
| 174 | + TEMPLATE_CONTENT="${TEMPLATE_CONTENT%x}" |
| 175 | + else |
| 176 | + echo "ERROR: Could not resolve required $TEMPLATE_NAME from the template override stack for $REPO_ROOT" >&2 |
| 177 | + exit 1 |
| 178 | + fi |
| 179 | +fi |
| 180 | + |
| 181 | +# Output results |
| 182 | +if $JSON_MODE; then |
| 183 | + # Build JSON array of documents |
| 184 | + if has_jq; then |
| 185 | + if [[ ${#docs[@]} -eq 0 ]]; then |
| 186 | + json_docs="[]" |
| 187 | + else |
| 188 | + json_docs=$(printf '%s\n' "${docs[@]}" | jq -R . | jq -s .) |
| 189 | + fi |
| 190 | + if [[ -n "$TEMPLATE_NAME" ]]; then |
| 191 | + jq -cn \ |
| 192 | + --arg feature_dir "$FEATURE_DIR" \ |
| 193 | + --argjson docs "$json_docs" \ |
| 194 | + --arg template_content "$TEMPLATE_CONTENT" \ |
| 195 | + '{FEATURE_DIR:$feature_dir,AVAILABLE_DOCS:$docs,TEMPLATE_CONTENT:$template_content}' |
| 196 | + else |
| 197 | + jq -cn \ |
| 198 | + --arg feature_dir "$FEATURE_DIR" \ |
| 199 | + --argjson docs "$json_docs" \ |
| 200 | + '{FEATURE_DIR:$feature_dir,AVAILABLE_DOCS:$docs}' |
| 201 | + fi |
| 202 | + else |
| 203 | + if [[ ${#docs[@]} -eq 0 ]]; then |
| 204 | + json_docs="[]" |
| 205 | + else |
| 206 | + json_docs=$(for d in "${docs[@]}"; do printf '"%s",' "$(json_escape "$d")"; done) |
| 207 | + json_docs="[${json_docs%,}]" |
| 208 | + fi |
| 209 | + if [[ -n "$TEMPLATE_NAME" ]]; then |
| 210 | + printf '{"FEATURE_DIR":"%s","AVAILABLE_DOCS":%s,"TEMPLATE_CONTENT":"%s"}\n' \ |
| 211 | + "$(json_escape "$FEATURE_DIR")" "$json_docs" "$(json_escape "$TEMPLATE_CONTENT")" |
| 212 | + else |
| 213 | + printf '{"FEATURE_DIR":"%s","AVAILABLE_DOCS":%s}\n' "$(json_escape "$FEATURE_DIR")" "$json_docs" |
| 214 | + fi |
| 215 | + fi |
| 216 | +else |
| 217 | + # Text output |
| 218 | + echo "FEATURE_DIR:$FEATURE_DIR" |
| 219 | + echo "AVAILABLE_DOCS:" |
| 220 | + |
| 221 | + # Show status of each potential document |
| 222 | + check_file "$RESEARCH" "research.md" |
| 223 | + check_file "$DATA_MODEL" "data-model.md" |
| 224 | + check_dir "$CONTRACTS_DIR" "contracts/" |
| 225 | + check_file "$QUICKSTART" "quickstart.md" |
| 226 | + |
| 227 | + if $INCLUDE_TASKS; then |
| 228 | + check_file "$TASKS" "tasks.md" |
| 229 | + fi |
| 230 | +fi |
0 commit comments