import os import sys import shutil import json import datetime # Configuration HMCMKIT_ROOT = ".hmcmkit" CONFIG_PATH = os.path.join(HMCMKIT_ROOT, "config.json") TEMPLATE_DIR = os.path.join(HMCMKIT_ROOT, "knowledge", "templates") def load_config(): with open(CONFIG_PATH, 'r', encoding='utf-8') as f: return json.load(f) def get_target_path(config, phase, feature_name): """ Resolves the target path based on config.json 'pdca' section. """ pdca_config = config.get('pdca', {}) path_key = f"{phase.lower()}DocPaths" # e.g. planDocPaths paths = pdca_config.get(path_key, []) if not paths: # Fallback defaults defaults = { "plan": f"docs/01-plan/{feature_name}.plan.md", "design": f"docs/02-design/{feature_name}.design.md", "gap-analysis": f"docs/03-check/{feature_name}.gap.md", "report": f"docs/04-act/{feature_name}.report.md" } return defaults.get(phase.lower(), f"docs/{phase}/{feature_name}.md") # Use the first configured path first_path = paths[0] return first_path.replace("{feature}", feature_name).replace("{type}", phase) def scaffold(phase, feature_name): config = load_config() # 1. Identify Template template_types = config.get('templates', {}).get('types', {}) template_file = template_types.get(phase.lower()) if not template_file: print(f"❌ Error: No template defined for phase '{phase}'") return False template_path = os.path.join(TEMPLATE_DIR, template_file) if not os.path.exists(template_path): # Try looking for Level Variants (e.g. plan-starter.template.md) # For now, just error print(f"❌ Error: Template file not found at {template_path}") return False # 2. Identify Target Path target_path = get_target_path(config, phase, feature_name) # 3. Create Directory os.makedirs(os.path.dirname(target_path), exist_ok=True) if os.path.exists(target_path): print(f"⚠️ Target file already exists: {target_path}") # v1.0.1: Auto Snapshot try: from snapshot import create_snapshot create_snapshot(target_path) print(f" (Backup created before potential overwrite)") except ImportError: pass return False # 4. Read & process Template with open(template_path, 'r', encoding='utf-8') as f: content = f.read() # Simple Variable Replacement content = content.replace("{{feature}}", feature_name) content = content.replace("{{date}}", datetime.datetime.now().strftime("%Y-%m-%d")) # 5. Write File with open(target_path, 'w', encoding='utf-8') as f: f.write(content) print(f"✅ Created {phase.upper()} document: {target_path}") return True if __name__ == "__main__": if len(sys.argv) < 3: print("Usage: python scaffold.py ") print("Phases: plan, design, gap-analysis, report") sys.exit(1) phase = sys.argv[1] feature = sys.argv[2] scaffold(phase, feature)