|
| 1 | +package docs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "chuchu/internal/llm" |
| 12 | +) |
| 13 | + |
| 14 | +type ReadmeUpdater struct { |
| 15 | + provider llm.Provider |
| 16 | + model string |
| 17 | + workDir string |
| 18 | +} |
| 19 | + |
| 20 | +type UpdateResult struct { |
| 21 | + Updated bool |
| 22 | + Changes []string |
| 23 | + NewText string |
| 24 | + Error error |
| 25 | +} |
| 26 | + |
| 27 | +func NewReadmeUpdater(provider llm.Provider, model, workDir string) *ReadmeUpdater { |
| 28 | + return &ReadmeUpdater{ |
| 29 | + provider: provider, |
| 30 | + model: model, |
| 31 | + workDir: workDir, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (u *ReadmeUpdater) UpdateReadme(ctx context.Context) (*UpdateResult, error) { |
| 36 | + readmePath := filepath.Join(u.workDir, "README.md") |
| 37 | + |
| 38 | + currentReadme, err := os.ReadFile(readmePath) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("failed to read README: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + changes, err := u.detectChanges() |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("failed to detect changes: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + if len(changes) == 0 { |
| 49 | + return &UpdateResult{ |
| 50 | + Updated: false, |
| 51 | + Changes: []string{}, |
| 52 | + }, nil |
| 53 | + } |
| 54 | + |
| 55 | + updatedReadme, err := u.generateUpdate(ctx, string(currentReadme), changes) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to generate update: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + result := &UpdateResult{ |
| 61 | + Updated: true, |
| 62 | + Changes: changes, |
| 63 | + NewText: updatedReadme, |
| 64 | + } |
| 65 | + |
| 66 | + return result, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (u *ReadmeUpdater) detectChanges() ([]string, error) { |
| 70 | + var changes []string |
| 71 | + |
| 72 | + recentCommits, err := u.getRecentCommits() |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + for _, commit := range recentCommits { |
| 78 | + if strings.HasPrefix(commit, "feat:") || strings.HasPrefix(commit, "feat(") { |
| 79 | + changes = append(changes, commit) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + newFiles, err := u.getNewFiles() |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + if len(newFiles) > 0 { |
| 88 | + changes = append(changes, fmt.Sprintf("Added %d new file(s)", len(newFiles))) |
| 89 | + } |
| 90 | + |
| 91 | + newCommands, err := u.detectNewCommands() |
| 92 | + if err == nil && len(newCommands) > 0 { |
| 93 | + for _, cmd := range newCommands { |
| 94 | + changes = append(changes, fmt.Sprintf("New command: %s", cmd)) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return changes, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (u *ReadmeUpdater) getRecentCommits() ([]string, error) { |
| 102 | + cmd := exec.Command("git", "log", "--oneline", "-10", "--no-merges") |
| 103 | + cmd.Dir = u.workDir |
| 104 | + output, err := cmd.Output() |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + lines := strings.Split(strings.TrimSpace(string(output)), "\n") |
| 110 | + var commits []string |
| 111 | + for _, line := range lines { |
| 112 | + parts := strings.SplitN(line, " ", 2) |
| 113 | + if len(parts) == 2 { |
| 114 | + commits = append(commits, parts[1]) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return commits, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (u *ReadmeUpdater) getNewFiles() ([]string, error) { |
| 122 | + cmd := exec.Command("git", "diff", "--name-status", "HEAD~5..HEAD") |
| 123 | + cmd.Dir = u.workDir |
| 124 | + output, err := cmd.Output() |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + |
| 129 | + var newFiles []string |
| 130 | + lines := strings.Split(strings.TrimSpace(string(output)), "\n") |
| 131 | + for _, line := range lines { |
| 132 | + if strings.HasPrefix(line, "A\t") { |
| 133 | + file := strings.TrimPrefix(line, "A\t") |
| 134 | + if strings.HasSuffix(file, ".go") && strings.Contains(file, "cmd/") { |
| 135 | + newFiles = append(newFiles, file) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return newFiles, nil |
| 141 | +} |
| 142 | + |
| 143 | +func (u *ReadmeUpdater) detectNewCommands() ([]string, error) { |
| 144 | + cmdPath := filepath.Join(u.workDir, "cmd/chu") |
| 145 | + |
| 146 | + entries, err := os.ReadDir(cmdPath) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + |
| 151 | + var commands []string |
| 152 | + for _, entry := range entries { |
| 153 | + if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go") { |
| 154 | + name := strings.TrimSuffix(entry.Name(), ".go") |
| 155 | + if name != "main" { |
| 156 | + commands = append(commands, name) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return commands, nil |
| 162 | +} |
| 163 | + |
| 164 | +func (u *ReadmeUpdater) generateUpdate(ctx context.Context, currentReadme string, changes []string) (string, error) { |
| 165 | + changesText := strings.Join(changes, "\n- ") |
| 166 | + |
| 167 | + prompt := fmt.Sprintf(`Update this README.md based on recent changes. |
| 168 | +
|
| 169 | +Current README: |
| 170 | +%s |
| 171 | +
|
| 172 | +Recent changes: |
| 173 | +- %s |
| 174 | +
|
| 175 | +Rules: |
| 176 | +1. Keep existing structure and sections |
| 177 | +2. Update feature lists and capabilities |
| 178 | +3. Add/update examples for new commands |
| 179 | +4. Maintain professional tone |
| 180 | +5. Keep badges and links intact |
| 181 | +6. Update version/status if significant features added |
| 182 | +7. DO NOT remove important content |
| 183 | +8. Add brief descriptions for new features |
| 184 | +
|
| 185 | +Return ONLY the complete updated README.md, no explanations.`, currentReadme, changesText) |
| 186 | + |
| 187 | + resp, err := u.provider.Chat(ctx, llm.ChatRequest{ |
| 188 | + UserPrompt: prompt, |
| 189 | + Model: u.model, |
| 190 | + }) |
| 191 | + |
| 192 | + if err != nil { |
| 193 | + return "", err |
| 194 | + } |
| 195 | + |
| 196 | + updated := strings.TrimSpace(resp.Text) |
| 197 | + |
| 198 | + if strings.HasPrefix(updated, "```markdown") { |
| 199 | + updated = strings.TrimPrefix(updated, "```markdown\n") |
| 200 | + updated = strings.TrimSuffix(updated, "```") |
| 201 | + } else if strings.HasPrefix(updated, "```") { |
| 202 | + updated = strings.TrimPrefix(updated, "```\n") |
| 203 | + updated = strings.TrimSuffix(updated, "```") |
| 204 | + } |
| 205 | + |
| 206 | + return strings.TrimSpace(updated), nil |
| 207 | +} |
| 208 | + |
| 209 | +func (u *ReadmeUpdater) ApplyUpdate(readmePath, newContent string) error { |
| 210 | + backupPath := readmePath + ".backup" |
| 211 | + |
| 212 | + if err := os.Rename(readmePath, backupPath); err != nil { |
| 213 | + return fmt.Errorf("failed to create backup: %w", err) |
| 214 | + } |
| 215 | + |
| 216 | + if err := os.WriteFile(readmePath, []byte(newContent), 0644); err != nil { |
| 217 | + os.Rename(backupPath, readmePath) |
| 218 | + return fmt.Errorf("failed to write README: %w", err) |
| 219 | + } |
| 220 | + |
| 221 | + os.Remove(backupPath) |
| 222 | + return nil |
| 223 | +} |
0 commit comments