create-skill.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # ==================== Help ====================
  4. usage() {
  5. cat <<'EOF'
  6. Usage:
  7. create-skill.sh <skill-name> [--minimal|--full] [--output <dir>] [--force]
  8. Notes:
  9. - <skill-name> MUST be lowercase, start with a letter, and only contain letters, digits, and hyphens
  10. - Default mode: --full
  11. - Default output: current directory (creates ./<skill-name>/)
  12. Examples:
  13. ./skills/claude-skills/scripts/create-skill.sh postgresql --full --output skills
  14. ./skills/claude-skills/scripts/create-skill.sh my-api --minimal
  15. EOF
  16. }
  17. die() {
  18. echo "Error: $*" >&2
  19. exit 1
  20. }
  21. # ==================== Arg Parsing ====================
  22. skill_name=""
  23. mode="full"
  24. output_dir="."
  25. force=0
  26. while [[ $# -gt 0 ]]; do
  27. case "$1" in
  28. -h|--help)
  29. usage
  30. exit 0
  31. ;;
  32. --minimal)
  33. mode="minimal"
  34. shift
  35. ;;
  36. --full)
  37. mode="full"
  38. shift
  39. ;;
  40. -o|--output)
  41. [[ $# -ge 2 ]] || die "--output requires a directory argument"
  42. output_dir="$2"
  43. shift 2
  44. ;;
  45. -f|--force)
  46. force=1
  47. shift
  48. ;;
  49. --)
  50. shift
  51. break
  52. ;;
  53. -*)
  54. die "Unknown argument: $1 (use --help)"
  55. ;;
  56. *)
  57. if [[ -z "$skill_name" ]]; then
  58. skill_name="$1"
  59. shift
  60. else
  61. die "Extra argument: $1 (only one <skill-name> is allowed)"
  62. fi
  63. ;;
  64. esac
  65. done
  66. [[ -n "$skill_name" ]] || { usage; exit 1; }
  67. if [[ ! "$skill_name" =~ ^[a-z][a-z0-9-]*$ ]]; then
  68. die "skill-name must be lowercase, start with a letter, and only contain letters/digits/hyphens (e.g. my-skill-name)"
  69. fi
  70. script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
  71. assets_dir="${script_dir}/../assets"
  72. template_path=""
  73. case "$mode" in
  74. minimal) template_path="${assets_dir}/template-minimal.md" ;;
  75. full) template_path="${assets_dir}/template-complete.md" ;;
  76. *) die "Internal error: unknown mode=$mode" ;;
  77. esac
  78. [[ -f "$template_path" ]] || die "Template not found: $template_path"
  79. mkdir -p "$output_dir"
  80. target_dir="${output_dir%/}/${skill_name}"
  81. if [[ -e "$target_dir" && "$force" -ne 1 ]]; then
  82. die "Target already exists: $target_dir (use --force to overwrite)"
  83. fi
  84. mkdir -p "$target_dir"/{assets,scripts,references}
  85. # ==================== Write Files ====================
  86. render_template() {
  87. local src="$1"
  88. local dest="$2"
  89. sed "s/{{skill_name}}/${skill_name}/g" "$src" > "$dest"
  90. }
  91. render_template "$template_path" "$target_dir/SKILL.md"
  92. cat > "$target_dir/references/index.md" <<EOF
  93. # ${skill_name} Reference Index
  94. ## Quick Links
  95. - Getting started: \`getting_started.md\`
  96. - API/CLI/config: \`api.md\` (if applicable)
  97. - Examples: \`examples.md\`
  98. - Troubleshooting: \`troubleshooting.md\`
  99. ## Notes
  100. - Put long-form content here: excerpts, evidence links, edge cases, FAQ
  101. - Keep \`SKILL.md\` Quick Reference short and directly usable
  102. EOF
  103. if [[ "$mode" == "full" ]]; then
  104. cat > "$target_dir/references/getting_started.md" <<'EOF'
  105. # Getting Started & Vocabulary
  106. ## Goals
  107. - Define the 10 most important terms in this domain
  108. - Provide the shortest path from zero to working
  109. EOF
  110. cat > "$target_dir/references/api.md" <<'EOF'
  111. # API / CLI / Config Reference (If Applicable)
  112. ## Suggested Structure
  113. - Organize by use case, not alphabetically
  114. - Key parameters: defaults, boundaries, common misuse
  115. - Common errors: message -> cause -> fix steps
  116. EOF
  117. cat > "$target_dir/references/examples.md" <<'EOF'
  118. # Long Examples
  119. Put examples longer than ~20 lines here, split by use case:
  120. - Use case 1: ...
  121. - Use case 2: ...
  122. EOF
  123. cat > "$target_dir/references/troubleshooting.md" <<'EOF'
  124. # Troubleshooting & Edge Cases
  125. Write as: symptom -> likely causes -> diagnosis -> fix.
  126. EOF
  127. fi
  128. # ==================== Summary ====================
  129. echo ""
  130. echo "OK: Skill generated: $target_dir/"
  131. echo ""
  132. echo "Layout:"
  133. echo " $target_dir/"
  134. echo " |-- SKILL.md"
  135. echo " |-- assets/"
  136. echo " |-- scripts/"
  137. echo " \\-- references/"
  138. echo " \\-- index.md"
  139. echo ""
  140. echo "Next steps:"
  141. echo " 1) Edit $target_dir/SKILL.md (triggers/boundaries/quick reference/examples)"
  142. echo " 2) Put long-form docs into $target_dir/references/ and update index.md"