()
| 263 | # ============================================================ |
| 264 | |
| 265 | def main(): |
| 266 | print("=" * 60) |
| 267 | print("Tutorial 09: Config System 配置系统演示") |
| 268 | print("=" * 60) |
| 269 | |
| 270 | # --- 创建临时目录模拟项目结构 --- |
| 271 | tmp_root = tempfile.mkdtemp(prefix="tutorial_config_") |
| 272 | project_dir = os.path.join(tmp_root, "my-project") |
| 273 | claude_dir = os.path.join(project_dir, ".claude") |
| 274 | home_claude = os.path.join(tmp_root, "fake_home", ".claude") |
| 275 | os.makedirs(claude_dir) |
| 276 | os.makedirs(home_claude) |
| 277 | |
| 278 | # --- 1. 写入全局用户配置 --- |
| 279 | user_config = { |
| 280 | "model": "claude-sonnet-4-20250514", |
| 281 | "permissionMode": "workspace-write", |
| 282 | "theme": "dark", |
| 283 | "hooks": { |
| 284 | "PostToolUse": [ |
| 285 | {"command": "printf 'global post hook'"} |
| 286 | ] |
| 287 | } |
| 288 | } |
| 289 | with open(os.path.join(home_claude, "settings.json"), "w") as f: |
| 290 | json.dump(user_config, f, indent=2) |
| 291 | |
| 292 | # --- 2. 写入项目配置 --- |
| 293 | project_config = { |
| 294 | "model": "claude-opus-4-6", # 覆盖全局的 model |
| 295 | "hooks": { |
| 296 | "PreToolUse": [ |
| 297 | {"command": "printf 'project pre hook'"} |
| 298 | ] |
| 299 | }, |
| 300 | "projectRules": "Follow PEP 8 style guide" |
| 301 | } |
| 302 | with open(os.path.join(claude_dir, "settings.json"), "w") as f: |
| 303 | json.dump(project_config, f, indent=2) |
| 304 | |
| 305 | # --- 3. 写入本地配置 --- |
| 306 | local_config = { |
| 307 | "theme": "light", # 覆盖全局的 theme |
| 308 | "debugMode": True, |
| 309 | } |
| 310 | with open(os.path.join(claude_dir, "settings.local.json"), "w") as f: |
| 311 | json.dump(local_config, f, indent=2) |
| 312 | |
| 313 | # --- 4. 加载配置 --- |
| 314 | print("\n--- 配置文件发现 ---") |
| 315 | loader = ConfigLoader(project_dir, home_claude) |
| 316 | |
| 317 | for entry in loader.discover(): |
| 318 | exists = os.path.exists(entry.path) |
| 319 | filename = os.path.basename(entry.path) |
| 320 | marker = " [EXISTS]" if exists else "" |
| 321 | print(f" [{entry.source.value:7s}] {filename}{marker}") |
| 322 |
no test coverage detected