|
| 1 | +# Cookiecutter Template Modernization Plan |
| 2 | + |
| 3 | +## TASK PURPOSE & CRITICAL RULES |
| 4 | + |
| 5 | +**PURPOSE:** Update the cookiecutter template to generate files that exactly match the reference structure in `.templates/leetcode/.example/{{cookiecutter.problem_name}}/` |
| 6 | + |
| 7 | +**REFERENCE DIRECTORIES (NEVER MODIFY - THESE ARE EXAMPLES):** |
| 8 | + |
| 9 | +- `.templates/leetcode/.example/{{cookiecutter.problem_name}}/` - Shows what the template SHOULD generate |
| 10 | +- `leetcode/.example/` - Generated file examples for comparison |
| 11 | + |
| 12 | +**ACTUAL TEMPLATE DIRECTORY (MODIFY THIS):** |
| 13 | + |
| 14 | +- `.templates/leetcode/{{cookiecutter.problem_name}}/` - The cookiecutter template files to update |
| 15 | + |
| 16 | +**WORKFLOW:** |
| 17 | + |
| 18 | +1. Look at `.templates/leetcode/.example/{{cookiecutter.problem_name}}/` to see target structure |
| 19 | +2. Modify `.templates/leetcode/{{cookiecutter.problem_name}}/` to match the reference |
| 20 | +3. Generate with `make p-gen` |
| 21 | +4. Compare generated files vs reference with `make p-validate` |
| 22 | + |
| 23 | +**ERROR PREVENTION:** The template directory does NOT have `.example` in the path! |
| 24 | + |
| 25 | +## Analysis Summary |
| 26 | + |
| 27 | +**Target Structure**: `leetcode/.example/` contains the reference implementation |
| 28 | +**Key Differences Found:** |
| 29 | + |
| 30 | +- `leetcode/.example/` has `__init__.py` files (missing in old template) |
| 31 | +- `leetcode/.example/` uses modern Python syntax (`TreeNode | None` vs `Optional[TreeNode]`) |
| 32 | +- `leetcode/.example/` follows project's coding standards more closely |
| 33 | +- Template must generate files identical to `leetcode/.example/` structure |
| 34 | + |
| 35 | +## Implementation Plan |
| 36 | + |
| 37 | +### 0. Explicit File Content Analysis |
| 38 | + |
| 39 | +- **Tool**: `.amazonq/plan/compare_template_files.py` (already exists - no need to implement) |
| 40 | +- **Usage**: |
| 41 | + - `poetry run python .amazonq/plan/compare_template_files.py generated --problem=PROBLEM_NAME` - Compare generated files vs reference |
| 42 | +- **Analysis**: Line-by-line diff of all file types |
| 43 | +- **Document**: Exact differences and required changes |
| 44 | +- **Verify**: Template variables handle all variations |
| 45 | + |
| 46 | +### 1. Incremental Template Updates (File-by-File Approach) |
| 47 | + |
| 48 | +#### Phase 1: Add `__init__.py` |
| 49 | + |
| 50 | +- **Add**: Empty `__init__.py` file to template |
| 51 | +- **Validate**: `make p-gen` → `make p-validate` → `make lint` |
| 52 | + |
| 53 | +#### Phase 2: Fix `solution.py` |
| 54 | + |
| 55 | +- **Update**: Modern syntax (`TreeNode | None`), clean template logic |
| 56 | +- **Validate**: `make p-gen` → `make p-validate` → `make lint` |
| 57 | + |
| 58 | +#### Phase 3: Fix `tests.py` |
| 59 | + |
| 60 | +- **Update**: Relative imports (`from .solution`), clean structure |
| 61 | +- **Validate**: `make p-gen` → `make p-validate` → `make lint` |
| 62 | + |
| 63 | +#### Phase 4: Fix `README.md` |
| 64 | + |
| 65 | +- **Update**: Clean formatting, proper markdown |
| 66 | +- **Validate**: `make p-gen` → `make p-validate` → `make lint` |
| 67 | + |
| 68 | +#### Phase 5: Fix `playground.ipynb` |
| 69 | + |
| 70 | +- **Update**: Clean cells without execution state |
| 71 | +- **Validate**: `make p-gen` → `make p-validate` → `make lint` |
| 72 | + |
| 73 | +**Benefits**: Isolated debugging, safer progression, easier rollback |
| 74 | + |
| 75 | +### 2. Multi-Problem Type Testing |
| 76 | + |
| 77 | +- **Test Cases**: Ensure template handles all problem types |
| 78 | + - Basic array: `two_sum` (return `list[int]`) |
| 79 | + - Tree: `invert_binary_tree` (return `TreeNode | None`) |
| 80 | + - String: problems returning `str` |
| 81 | + - Boolean: problems returning `bool` |
| 82 | + - No imports vs TreeNode/ListNode imports |
| 83 | +- **Validation**: Each type generates correctly |
| 84 | + |
| 85 | +### 3. Modernize cookiecutter.json Schema |
| 86 | + |
| 87 | +- **Base on**: Existing `.templates/leetcode/.example/examples/` JSON5 files |
| 88 | +- **Ensure**: All template variables are properly defined |
| 89 | +- **Add**: Missing fields found in real examples |
| 90 | +- **Note**: Variable mapping handled by `gen.py` `convert_arrays_to_nested()` |
| 91 | + |
| 92 | +### 4. Update Template Files |
| 93 | + |
| 94 | +#### solution.py |
| 95 | + |
| 96 | +- Use modern type hints (`TreeNode | None` not `Optional[TreeNode]`) |
| 97 | +- Match exact import patterns from real examples |
| 98 | +- Ensure proper TODO placeholder format |
| 99 | + |
| 100 | +#### tests.py |
| 101 | + |
| 102 | +- Follow `@logged_test` decorator pattern |
| 103 | +- Use parametrized pytest structure |
| 104 | +- Match logging format from real examples |
| 105 | + |
| 106 | +#### README.md |
| 107 | + |
| 108 | +- Follow exact format from real examples |
| 109 | +- Include proper problem description formatting |
| 110 | + |
| 111 | +#### playground.ipynb |
| 112 | + |
| 113 | +- Ensure notebook structure matches real examples |
| 114 | + |
| 115 | +### 5. Template Generation Logic |
| 116 | + |
| 117 | +- **File**: `.templates/leetcode/gen.py` (already handles variable mapping) |
| 118 | +- **Integration**: Works with `make p-gen PROBLEM=name` (verified in Makefile) |
| 119 | +- **Update**: Handle new `__init__.py` file |
| 120 | +- **Process**: JSON → `gen.py` → cookiecutter → `leetcode/$(PROBLEM)/` |
| 121 | + |
| 122 | +### 6. Automated Validation System |
| 123 | + |
| 124 | +- **Tool**: Reusable `.amazonq/plan/compare_template_files.py` |
| 125 | +- **Usage**: |
| 126 | + ```bash |
| 127 | + # Validate current template generates correct files |
| 128 | + poetry run python .amazonq/plan/compare_template_files.py generated --problem=invert_binary_tree |
| 129 | + ``` |
| 130 | +- **Makefile**: `make p-validate PROBLEM=name` (implemented) |
| 131 | +- **Test**: Template regression testing |
| 132 | +- **Ensure**: `make p-gen` + `make lint` + `make p-test` all pass |
| 133 | + |
| 134 | +### 7. Testing & Validation |
| 135 | + |
| 136 | +- **Test**: Template generation with existing JSON files |
| 137 | +- **Verify**: Generated files match `leetcode/.example/` structure exactly |
| 138 | +- **Compare**: Automated diff against reference files |
| 139 | +- **Ensure**: `make p-gen` works seamlessly |
| 140 | +- **Test**: Recreation process from `.prompt/` files |
| 141 | +- **Validate**: Multi-problem type generation |
| 142 | + |
| 143 | +## Key Template Variables to Ensure |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "problem_name": "snake_case_name", |
| 148 | + "class_name": "PascalCaseName", |
| 149 | + "method_name": "snake_case_method", |
| 150 | + "problem_number": "226", |
| 151 | + "problem_title": "Display Title", |
| 152 | + "difficulty": "Easy|Medium|Hard", |
| 153 | + "topics": "Comma, Separated, Topics", |
| 154 | + "tags": ["grind-75", "blind-75"], |
| 155 | + "problem_description": "Full description", |
| 156 | + "examples": [{"input": "...", "output": "..."}], |
| 157 | + "constraints": "Formatted constraints", |
| 158 | + "parameters": "typed_params: list[int]", |
| 159 | + "return_type": "TreeNode | None", |
| 160 | + "imports": "from leetcode_py.tree_node import TreeNode", |
| 161 | + "test_cases": [{"args": [...], "expected": ...}] |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +## Success Criteria |
| 166 | + |
| 167 | +### Phase-by-Phase Validation (File-by-File) |
| 168 | + |
| 169 | +1. ✅ **Phase 1**: `__init__.py` files generated correctly |
| 170 | +2. ✅ **Phase 2**: `solution.py` with modern syntax (`TreeNode | None`) |
| 171 | +3. ✅ **Phase 3**: `tests.py` with relative imports and clean structure |
| 172 | +4. ✅ **Phase 4**: `README.md` with clean formatting |
| 173 | +5. ✅ **Phase 5**: `playground.ipynb` with clean cells |
| 174 | + |
| 175 | +### Multi-Problem Type Validation |
| 176 | + |
| 177 | +5. ✅ Basic problems (array/string) generate correctly |
| 178 | +6. ✅ Tree problems generate correctly |
| 179 | +7. ✅ Different return types handled (`bool`, `int`, `str`, `list`, etc.) |
| 180 | + |
| 181 | +### Automated Validation |
| 182 | + |
| 183 | +8. ✅ Automated diff shows no differences vs `leetcode/.example/` |
| 184 | +9. ✅ `make p-validate` passes for all problem types |
| 185 | +10. ✅ Recreation from `.prompt/` works flawlessly |
| 186 | +11. ✅ All linting passes (`make lint`) |
| 187 | +12. ✅ Tests run successfully (`make p-test`) |
| 188 | + |
| 189 | +## Files to Modify |
| 190 | + |
| 191 | +### Template Files |
| 192 | + |
| 193 | +1. `.templates/leetcode/{{cookiecutter.problem_name}}/` |
| 194 | + - **Add**: `__init__.py` (empty file) |
| 195 | + - **Update**: `solution.py` (modern syntax, imports) |
| 196 | + - **Update**: `tests.py` (match `leetcode/.example/` format) |
| 197 | + - **Update**: `README.md` (match `leetcode/.example/` format) |
| 198 | + - **Update**: `playground.ipynb` (match structure) |
| 199 | + |
| 200 | +### Configuration |
| 201 | + |
| 202 | +2. `.templates/leetcode/cookiecutter.json` |
| 203 | + - Align with JSON5 examples |
| 204 | + - Add missing variables |
| 205 | + |
| 206 | +### Generation Logic |
| 207 | + |
| 208 | +3. `.templates/leetcode/gen.py` |
| 209 | + - Handle `__init__.py` generation |
| 210 | + - Maintain existing variable mapping |
| 211 | + |
| 212 | +### Validation Tools |
| 213 | + |
| 214 | +4. **Reusable**: `.amazonq/plan/compare_template_files.py` (handles both template and generated comparisons) |
| 215 | +5. **New**: Makefile target `make p-validate` |
| 216 | + |
| 217 | +## Risk Mitigation |
| 218 | + |
| 219 | +- **Incremental phases** prevent all-or-nothing failures |
| 220 | +- **Automated validation** catches regressions immediately |
| 221 | +- **Multi-problem testing** ensures template generalization |
| 222 | +- **Explicit file comparison** documents exact requirements |
| 223 | + |
| 224 | +This plan ensures the template generates files that exactly match `leetcode/.example/` while maintaining the robust generation process described in the rules. |
0 commit comments