|
| 1 | +#!/usr/bin/env bash |
| 2 | +# RustySpec common shell functions |
| 3 | +# Sourced by other scripts: source "$(dirname "$0")/common.sh" |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Find the project root by walking up to rustyspec.toml |
| 8 | +get_repo_root() { |
| 9 | + local dir="$PWD" |
| 10 | + while [ "$dir" != "/" ]; do |
| 11 | + if [ -f "$dir/rustyspec.toml" ] || [ -d "$dir/.rustyspec" ]; then |
| 12 | + echo "$dir" |
| 13 | + return 0 |
| 14 | + fi |
| 15 | + dir="$(dirname "$dir")" |
| 16 | + done |
| 17 | + echo "Error: not inside a RustySpec project" >&2 |
| 18 | + return 1 |
| 19 | +} |
| 20 | + |
| 21 | +# Get the current feature branch or fallback |
| 22 | +get_current_branch() { |
| 23 | + # Level 1: RUSTYSPEC_FEATURE env var |
| 24 | + if [ -n "${RUSTYSPEC_FEATURE:-}" ]; then |
| 25 | + echo "$RUSTYSPEC_FEATURE" |
| 26 | + return 0 |
| 27 | + fi |
| 28 | + |
| 29 | + # Level 2: git branch |
| 30 | + if command -v git &>/dev/null && git rev-parse --is-inside-work-tree &>/dev/null; then |
| 31 | + local branch |
| 32 | + branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" |
| 33 | + if [[ "$branch" =~ ^[0-9]{3}- ]]; then |
| 34 | + echo "$branch" |
| 35 | + return 0 |
| 36 | + fi |
| 37 | + fi |
| 38 | + |
| 39 | + # Level 3: latest specs/ directory |
| 40 | + local root |
| 41 | + root="$(get_repo_root)" || return 1 |
| 42 | + local latest |
| 43 | + latest="$(ls -d "$root"/specs/[0-9][0-9][0-9]-* 2>/dev/null | sort | tail -1 || true)" |
| 44 | + if [ -n "$latest" ]; then |
| 45 | + basename "$latest" |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + |
| 49 | + echo "Error: no feature found" >&2 |
| 50 | + return 1 |
| 51 | +} |
| 52 | + |
| 53 | +# Find a feature directory by numeric prefix |
| 54 | +find_feature_dir() { |
| 55 | + local prefix="${1:?Usage: find_feature_dir <prefix>}" |
| 56 | + local root |
| 57 | + root="$(get_repo_root)" || return 1 |
| 58 | + |
| 59 | + local matches |
| 60 | + matches="$(ls -d "$root"/specs/"${prefix}"-* 2>/dev/null || true)" |
| 61 | + local count |
| 62 | + count="$(echo "$matches" | grep -c . || true)" |
| 63 | + |
| 64 | + if [ "$count" -eq 0 ]; then |
| 65 | + echo "Error: no feature matching '$prefix' in specs/" >&2 |
| 66 | + return 1 |
| 67 | + elif [ "$count" -eq 1 ]; then |
| 68 | + basename "$matches" |
| 69 | + else |
| 70 | + basename "$(echo "$matches" | sort | tail -1)" |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +# Output all feature paths as shell variables |
| 75 | +get_feature_paths() { |
| 76 | + local root |
| 77 | + root="$(get_repo_root)" || return 1 |
| 78 | + local branch |
| 79 | + branch="$(get_current_branch)" || return 1 |
| 80 | + local feature_dir="$root/specs/$branch" |
| 81 | + |
| 82 | + echo "REPO_ROOT=\"$root\"" |
| 83 | + echo "CURRENT_BRANCH=\"$branch\"" |
| 84 | + echo "HAS_GIT=$(git rev-parse --is-inside-work-tree 2>/dev/null && echo true || echo false)" |
| 85 | + echo "FEATURE_DIR=\"$feature_dir\"" |
| 86 | + echo "FEATURE_SPEC=\"$feature_dir/spec.md\"" |
| 87 | + echo "IMPL_PLAN=\"$feature_dir/plan.md\"" |
| 88 | + echo "TASKS=\"$feature_dir/tasks.md\"" |
| 89 | + echo "RESEARCH=\"$feature_dir/research.md\"" |
| 90 | + echo "DATA_MODEL=\"$feature_dir/data-model.md\"" |
| 91 | + echo "QUICKSTART=\"$feature_dir/quickstart.md\"" |
| 92 | + echo "CONTRACTS_DIR=\"$feature_dir/contracts\"" |
| 93 | +} |
| 94 | + |
| 95 | +# Escape string for safe JSON embedding |
| 96 | +json_escape() { |
| 97 | + local str="${1:-}" |
| 98 | + str="${str//\\/\\\\}" |
| 99 | + str="${str//\"/\\\"}" |
| 100 | + str="${str//$'\n'/\\n}" |
| 101 | + str="${str//$'\t'/\\t}" |
| 102 | + str="${str//$'\r'/\\r}" |
| 103 | + echo "$str" |
| 104 | +} |
0 commit comments