1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 16:36:08 +01:00
Files
core/script/gen_copilot_instructions.py

125 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generate .github/copilot-instructions.md from AGENTS.md and skills.
Necessary until copilot can handle skills.
"""
from __future__ import annotations
from pathlib import Path
import sys
GENERATED_MESSAGE = (
f"<!-- Automatically generated by {Path(__file__).name}, do not edit -->\n\n"
)
SKILLS_DIR = Path(".claude/skills")
AGENTS_FILE = Path("AGENTS.md")
OUTPUT_FILE = Path(".github/copilot-instructions.md")
EXCLUDED_SKILLS = {"github-pr-reviewer"}
COPILOT_SPECIFIC_INSTRUCTIONS = """
# Copilot code review instructions
- Start review comments with a short, one-sentence summary of the suggested fix.
- Do not add comments about code style, formatting or linting issues.
"""
def gather_skills() -> list[tuple[str, Path]]:
"""Gather all skills from the skills directory.
Returns a list of tuples (skill_name, skill_file_path).
"""
skills: list[tuple[str, Path]] = []
if not SKILLS_DIR.exists():
return skills
for skill_dir in sorted(SKILLS_DIR.iterdir()):
if not skill_dir.is_dir():
continue
if skill_dir.name in EXCLUDED_SKILLS:
continue
skill_file = skill_dir / "SKILL.md"
if not skill_file.exists():
continue
skill_content = skill_file.read_text()
# Extract skill name from frontmatter if present
skill_name = skill_dir.name
if skill_content.startswith("---"):
# Parse YAML frontmatter
end_idx = skill_content.find("---", 3)
if end_idx != -1:
frontmatter = skill_content[3:end_idx]
for line in frontmatter.split("\n"):
if line.startswith("name:"):
skill_name = line[5:].strip()
break
skills.append((skill_name, skill_file))
return skills
def generate_output() -> str:
"""Generate the copilot-instructions.md content."""
if not AGENTS_FILE.exists():
print(f"Error: {AGENTS_FILE} not found")
sys.exit(1)
output_parts: list[str] = [GENERATED_MESSAGE, COPILOT_SPECIFIC_INSTRUCTIONS]
# Add AGENTS.md content
agents_content = AGENTS_FILE.read_text()
output_parts.append(agents_content.strip())
output_parts.append("")
# Add skills section as a bullet list of name: path
skills = gather_skills()
if skills:
output_parts.append("")
output_parts.append("# Skills")
output_parts.append("")
for skill_name, skill_file in skills:
output_parts.append(f"- {skill_name}: {skill_file}")
output_parts.append("")
return "\n".join(output_parts)
def main(validate: bool = False) -> int:
"""Run the script."""
if not Path("homeassistant").is_dir():
print("Run this from HA root dir")
return 1
content = generate_output()
if validate:
if not OUTPUT_FILE.exists():
print(f"Error: {OUTPUT_FILE} does not exist")
return 1
existing = OUTPUT_FILE.read_text()
if existing != content:
print(f"Error: {OUTPUT_FILE} is out of date")
print("Please run: python -m script.gen_copilot_instructions")
return 1
print(f"{OUTPUT_FILE} is up to date")
return 0
OUTPUT_FILE.write_text(content)
print(f"Generated {OUTPUT_FILE}")
return 0
if __name__ == "__main__":
_validate = len(sys.argv) > 1 and sys.argv[1] == "validate"
sys.exit(main(_validate))