validate-context-refs.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # validate-context-refs.sh - Validates that all context references follow the strict convention
  3. set -e
  4. echo "🔍 Validating context references..."
  5. echo ""
  6. errors=0
  7. warnings=0
  8. # Check if .opencode directory exists
  9. if [ ! -d ".opencode" ]; then
  10. echo "❌ No .opencode directory found"
  11. echo " Run this script from the repository root"
  12. exit 1
  13. fi
  14. # Validate agent files
  15. echo "Checking agent files..."
  16. if [ -d ".opencode/agent" ]; then
  17. while IFS= read -r file; do
  18. rel_file="${file#./}"
  19. # Check for forbidden dynamic variables
  20. if grep -E '@\$[^0-9]|@\$\{' "$file" > /dev/null 2>&1; then
  21. echo "❌ Dynamic context reference in: $rel_file"
  22. grep -n '@\$' "$file" | head -3
  23. errors=$((errors + 1))
  24. fi
  25. # Check for context references that don't follow convention
  26. # Allow: @.opencode/context/, @AGENTS.md, @.cursorrules, @$1, @$2, etc.
  27. if grep -E '@[^~$]' "$file" | \
  28. grep -v '@\.opencode/context/' | \
  29. grep -v '@AGENTS\.md' | \
  30. grep -v '@\.cursorrules' | \
  31. grep -v '@\$[0-9]' | \
  32. grep -v '^#' | \
  33. grep -v 'email' | \
  34. grep -v 'mailto' > /dev/null 2>&1; then
  35. echo "⚠️ Non-standard reference in: $rel_file"
  36. grep -E '@[^~$]' "$file" | \
  37. grep -v '@\.opencode/context/' | \
  38. grep -v '@AGENTS\.md' | \
  39. grep -v '@\.cursorrules' | \
  40. grep -v '@\$[0-9]' | \
  41. grep -v '^#' | \
  42. grep -v 'email' | \
  43. grep -v 'mailto' | head -2
  44. warnings=$((warnings + 1))
  45. fi
  46. done < <(find .opencode/agent -type f -name "*.md" 2>/dev/null)
  47. fi
  48. # Validate command files
  49. echo "Checking command files..."
  50. if [ -d ".opencode/command" ]; then
  51. while IFS= read -r file; do
  52. rel_file="${file#./}"
  53. # Check for forbidden dynamic variables
  54. if grep -E '@\$[^0-9]|@\$\{' "$file" > /dev/null 2>&1; then
  55. echo "❌ Dynamic context reference in: $rel_file"
  56. grep -n '@\$' "$file" | head -3
  57. errors=$((errors + 1))
  58. fi
  59. # Check for non-standard references
  60. if grep -E '@[^~$]' "$file" | \
  61. grep -v '@\.opencode/context/' | \
  62. grep -v '@AGENTS\.md' | \
  63. grep -v '@\.cursorrules' | \
  64. grep -v '@\$[0-9]' | \
  65. grep -v '^#' | \
  66. grep -v 'email' > /dev/null 2>&1; then
  67. echo "⚠️ Non-standard reference in: $rel_file"
  68. warnings=$((warnings + 1))
  69. fi
  70. done < <(find .opencode/command -type f -name "*.md" 2>/dev/null)
  71. fi
  72. # Validate context files (they can reference other context files)
  73. echo "Checking context files..."
  74. if [ -d ".opencode/context" ]; then
  75. while IFS= read -r file; do
  76. rel_file="${file#./}"
  77. # Check for dynamic variables
  78. if grep -E '@\$[^0-9]|@\$\{' "$file" > /dev/null 2>&1; then
  79. echo "❌ Dynamic context reference in: $rel_file"
  80. errors=$((errors + 1))
  81. fi
  82. # Check for context cross-references
  83. if grep '@' "$file" | grep -v '@\.opencode/context/' | grep -v '^#' | grep -v 'email' > /dev/null 2>&1; then
  84. echo "⚠️ Context file has non-standard reference: $rel_file"
  85. warnings=$((warnings + 1))
  86. fi
  87. done < <(find .opencode/context -type f -name "*.md" 2>/dev/null)
  88. fi
  89. # Check for shell commands with hardcoded paths
  90. echo "Checking for shell commands with paths..."
  91. while IFS= read -r file; do
  92. rel_file="${file#./}"
  93. if grep '!\`.*\.opencode/context' "$file" > /dev/null 2>&1; then
  94. echo "ℹ️ Shell command with path in: $rel_file"
  95. echo " (Will be transformed during installation)"
  96. fi
  97. done < <(find .opencode -type f -name "*.md" 2>/dev/null)
  98. # Summary
  99. echo ""
  100. echo "=========================================="
  101. if [ $errors -gt 0 ]; then
  102. echo "❌ Validation failed with $errors error(s) and $warnings warning(s)"
  103. echo ""
  104. echo "Errors must be fixed before installation."
  105. echo "All context references must use: @.opencode/context/{category}/{file}.md"
  106. exit 1
  107. elif [ $warnings -gt 0 ]; then
  108. echo "⚠️ Validation passed with $warnings warning(s)"
  109. echo ""
  110. echo "Warnings indicate non-standard references that may not work correctly."
  111. echo "Consider updating them to use: @.opencode/context/{category}/{file}.md"
  112. exit 0
  113. else
  114. echo "✅ All validations passed!"
  115. echo ""
  116. echo "All context references follow the correct convention."
  117. exit 0
  118. fi