|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +This script uses a plugin architecture to parse rules and generate documentation. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from typing import Dict, List |
| 10 | + |
| 11 | +from sync_ai_rules.core.interfaces import RuleMetadata |
| 12 | +from sync_ai_rules.core.plugin_manager import PluginManager |
| 13 | +from sync_ai_rules.file_updater import update_documentation_file |
| 14 | + |
| 15 | + |
| 16 | +def find_project_root() -> str: |
| 17 | + """Find the project root by looking for key indicators.""" |
| 18 | + current = Path.cwd() |
| 19 | + |
| 20 | + # Look for .cursor/rules directory |
| 21 | + for path in [current] + list(current.parents): |
| 22 | + if (path / ".cursor" / "rules").exists(): |
| 23 | + return str(path) |
| 24 | + |
| 25 | + # Fallback to current directory |
| 26 | + return str(current) |
| 27 | + |
| 28 | + |
| 29 | +def get_category(file_path: str, rules_dir: str) -> str: |
| 30 | + """Get category name from file path.""" |
| 31 | + rel_path = os.path.relpath(file_path, rules_dir) |
| 32 | + folder = os.path.dirname(rel_path) |
| 33 | + |
| 34 | + if not folder or folder == ".": |
| 35 | + return "root" |
| 36 | + |
| 37 | + return folder |
| 38 | + |
| 39 | + |
| 40 | +def group_rules_by_category(rules: List[RuleMetadata]) -> Dict[str, List[RuleMetadata]]: |
| 41 | + """Group rules by their category.""" |
| 42 | + groups = {} |
| 43 | + |
| 44 | + for rule in rules: |
| 45 | + category = rule.category |
| 46 | + if category not in groups: |
| 47 | + groups[category] = [] |
| 48 | + groups[category].append(rule) |
| 49 | + |
| 50 | + return groups |
| 51 | + |
| 52 | + |
| 53 | +def main(): |
| 54 | + """Main entry point.""" |
| 55 | + # Find project root |
| 56 | + project_root = find_project_root() |
| 57 | + rules_dir = os.path.join(project_root, ".cursor", "rules") |
| 58 | + |
| 59 | + if not os.path.exists(rules_dir): |
| 60 | + print(f"Error: Rules directory not found: {rules_dir}") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + print(f"Syncing rules from: {rules_dir}") |
| 64 | + |
| 65 | + # Initialize plugin manager |
| 66 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 67 | + plugin_manager = PluginManager() |
| 68 | + plugin_manager.load_plugins(script_dir) |
| 69 | + |
| 70 | + # Scan for rules using available parsers |
| 71 | + rules = [] |
| 72 | + for root, dirs, files in os.walk(rules_dir): |
| 73 | + # Skip generated and personal directories |
| 74 | + if "generated" in Path(root).parts or "personal" in Path(root).parts: |
| 75 | + continue |
| 76 | + |
| 77 | + for file in files: |
| 78 | + file_path = os.path.join(root, file) |
| 79 | + |
| 80 | + # Find appropriate parser |
| 81 | + parser = plugin_manager.get_parser_for_file(file_path) |
| 82 | + if not parser: |
| 83 | + continue |
| 84 | + |
| 85 | + # Create parsing context |
| 86 | + context = { |
| 87 | + "relative_path": os.path.relpath(file_path, project_root), |
| 88 | + "category": get_category(file_path, rules_dir), |
| 89 | + } |
| 90 | + |
| 91 | + # Parse the rule |
| 92 | + rule = parser.parse(file_path, context) |
| 93 | + if rule: |
| 94 | + rules.append(rule) |
| 95 | + |
| 96 | + if not rules: |
| 97 | + print("No rules found to sync") |
| 98 | + return |
| 99 | + |
| 100 | + # Group rules by category |
| 101 | + grouped_rules = group_rules_by_category(rules) |
| 102 | + |
| 103 | + # Get markdown generator |
| 104 | + generator = plugin_manager.get_generator("markdown") |
| 105 | + if not generator: |
| 106 | + print("Error: Markdown generator not found") |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + # Generate content |
| 110 | + content = generator.generate(grouped_rules, {}) |
| 111 | + |
| 112 | + # Print summary |
| 113 | + total_rules = sum(len(rules) for rules in grouped_rules.values()) |
| 114 | + print(f"\nFound {total_rules} rules in {len(grouped_rules)} categories") |
| 115 | + |
| 116 | + # Update output files using generator's default filenames |
| 117 | + output_files = [ |
| 118 | + os.path.join(project_root, filename) for filename in generator.default_filenames |
| 119 | + ] |
| 120 | + |
| 121 | + for file_path in output_files: |
| 122 | + success, message = update_documentation_file( |
| 123 | + file_path, content, generator.get_section_markers() |
| 124 | + ) |
| 125 | + |
| 126 | + if success: |
| 127 | + print(f"✓ {message}") |
| 128 | + else: |
| 129 | + print(f"✗ {message}") |
| 130 | + |
| 131 | + print("\n✓ Rules synchronization completed!") |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments