-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadges.go
More file actions
417 lines (369 loc) · 10.7 KB
/
Copy pathbadges.go
File metadata and controls
417 lines (369 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package devflow
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const DevFlowRepository = "github.com/tinywasm/devflow"
// Badges is responsible for creating and managing a collection of badges.
// It handles parsing input arguments, generating the SVG image, and preparing
// the necessary markdown to embed the badges in a file.
type Badges struct {
args []string
outputFile string
readmeFile string
// stored initialization/parsing error; methods must check this
err error
// Go handler
goH *Go
// configuration (moved from package-level constants)
svgHeight int
badgeHeight int
fontSize int
labelPadding int
valuePadding int
badgeSpacing int
labelBg string
// text used in the svg comment/header
svgInfo string
log func(...any)
}
// NewBadges creates and initializes a new Badges handler.
func NewBadges(args ...string) *Badges {
// Create handler with defaults
h := &Badges{
outputFile: "docs/img/badges.svg",
svgHeight: 20,
badgeHeight: 20,
fontSize: 11,
labelPadding: 6,
valuePadding: 6,
badgeSpacing: 5,
labelBg: "#6c757d",
svgInfo: DevFlowRepository,
log: func(...any) {},
}
// Create a default Go handler.
g, _ := NewGo(nil)
h.goH = g
// If the first argument is a directory, treat it as the current working directory.
if len(args) > 0 {
if fi, err := os.Stat(args[0]); err == nil && fi.IsDir() {
gitPath := filepath.Join(args[0], ".git")
if _, serr := os.Stat(gitPath); os.IsNotExist(serr) {
h.err = fmt.Errorf("Git repository not found")
return h
}
// remove the injected current-dir arg
args = args[1:]
}
}
if len(args) == 0 {
h.err = fmt.Errorf("no badges specified, usage: badges.sh \"label:value:color\" \"label:value:color\"")
return h
}
// assign args and process args to detect special commands (output_svgfile: and readmefile:)
h.args = args
// defaults
h.readmeFile = "README.md"
for _, a := range args {
if strings.HasPrefix(a, "output_svgfile:") {
h.outputFile = a[len("output_svgfile:"):]
continue
}
if strings.HasPrefix(a, "readmefile:") {
h.readmeFile = a[len("readmefile:"):]
continue
}
}
return h
}
// SetLog sets the logger function
func (h *Badges) SetLog(fn func(...any)) {
if fn != nil {
h.log = fn
if h.goH != nil {
h.goH.SetLog(fn)
}
}
}
// BuildBadges generates the SVG image, writes it to the specified output file,
// and returns a slice of strings intended for updating a markdown file.
func (h *Badges) BuildBadges() ([]string, error) {
if h.err != nil {
return nil, h.err
}
// First pass: collect parse errors/warnings like the bash implementation
var errorMessages []string
generatedBadgesCount := 0
for _, p := range h.args {
// skip special commands
if strings.HasPrefix(p, "output_svgfile:") || strings.HasPrefix(p, "readmefile:") {
continue
}
_, perr := parseBadge(p)
if perr != nil {
// special command sentinel
if strings.Contains(perr.Error(), "special command") {
continue
}
// Normalize messages to match the bash tests expectations
if strings.Contains(perr.Error(), "invalid badge format") {
errorMessages = append(errorMessages, fmt.Sprintf("Error: Invalid badge format: %s", p))
} else if strings.Contains(perr.Error(), "empty fields in badge") {
errorMessages = append(errorMessages, fmt.Sprintf("Error: Empty fields in badge: %s", p))
} else {
errorMessages = append(errorMessages, fmt.Sprintf("Error: %s", perr.Error()))
}
continue
}
generatedBadgesCount++
}
// Generate SVG
svgBytes, _, genErr := h.GenerateSVG()
// Print accumulated error messages
if len(errorMessages) > 0 {
for _, m := range errorMessages {
h.log(m)
}
}
if genErr != nil {
// No valid badges case should match bash: print explicit message
if generatedBadgesCount == 0 {
h.log("Error: No valid badges to generate")
}
return nil, genErr
}
// ensure directory exists when writing file
if err := os.MkdirAll(filepath.Dir(h.outputFile), 0o755); err != nil {
return nil, fmt.Errorf("create output dir: %w", err)
}
// Check if file exists and content is the same (don't rewrite if identical)
shouldWrite := true
if existing, err := os.ReadFile(h.outputFile); err == nil {
if bytes.Equal(existing, svgBytes) {
shouldWrite = false
}
}
if shouldWrite {
if err := os.WriteFile(h.outputFile, svgBytes, 0o644); err != nil {
return nil, fmt.Errorf("write svg file: %w", err)
}
}
// Update README with badge img line
if err := h.UpdateReadme(); err != nil {
return nil, fmt.Errorf("update readme: %w", err)
}
return nil, nil
}
// UpdateReadme updates the README file with the badge image line.
// Detects and migrates old START_SECTION/END_SECTION markers,
// replaces existing badge img lines, or inserts a new one.
func (h *Badges) UpdateReadme() error {
if h.err != nil {
return h.err
}
// Read the README file
content, err := os.ReadFile(h.readmeFile)
if err != nil {
// File doesn't exist yet, create it with just the badge
newContent := h.BadgeMarkdown() + "\n"
return os.WriteFile(h.readmeFile, []byte(newContent), 0o644)
}
currentContent := string(content)
lines := strings.Split(currentContent, "\n")
badgeLine := h.BadgeMarkdown()
sectionStart := "<!-- START_SECTION:BADGES_SECTION -->"
sectionEnd := "<!-- END_SECTION:BADGES_SECTION -->"
// First pass: find and remove old section markers
var newLines []string
markerFoundAt := -1
for i := 0; i < len(lines); i++ {
trimmed := strings.TrimSpace(lines[i])
// Check for start marker
if trimmed == sectionStart {
if markerFoundAt == -1 {
markerFoundAt = len(newLines)
}
// Skip lines until end marker (including content between them)
for i < len(lines)-1 {
i++
if strings.TrimSpace(lines[i]) == sectionEnd {
break
}
}
// Don't add any lines between markers - they'll be replaced by badge
continue
}
newLines = append(newLines, lines[i])
}
// If markers were found, insert badge line at that position
if markerFoundAt >= 0 {
newLines = append(newLines[:markerFoundAt], append([]string{badgeLine}, newLines[markerFoundAt:]...)...)
} else {
// No markers found - check for standalone badge img line to replace
foundBadgeAt := -1
for i, line := range newLines {
// Match standalone img lines (not inside <a> tags)
if strings.TrimSpace(line) == badgeLine {
foundBadgeAt = i
break
}
// Also match old format img lines that need updating
if matched, _ := regexp.MatchString(`^\s*<img src=".*badges\.svg">\s*$`, line); matched {
foundBadgeAt = i
break
}
}
if foundBadgeAt >= 0 {
// Replace existing badge line
newLines[foundBadgeAt] = badgeLine
} else {
// No badge line found - check if we need to insert one
// Insert after line 1 (assuming title/header) if not already present
if len(newLines) > 1 {
newLines = append(newLines[:1], append([]string{badgeLine}, newLines[1:]...)...)
} else {
newLines = append(newLines, badgeLine)
}
}
}
newContent := strings.Join(newLines, "\n")
// Only write if content changed
if newContent == currentContent {
h.log("Badge line already up to date")
return nil
}
if err := os.WriteFile(h.readmeFile, []byte(newContent), 0o644); err != nil {
return fmt.Errorf("write readme: %w", err)
}
h.log("Updated badges in", h.readmeFile)
return nil
}
// OutputFile returns the configured path for the output SVG file.
func (h *Badges) OutputFile() string {
if h.err != nil {
return ""
}
return h.outputFile
}
// ReadmeFile returns the configured path for the markdown file to be updated.
func (h *Badges) ReadmeFile() string {
if h.err != nil {
return ""
}
return h.readmeFile
}
// BadgeMarkdown generates the markdown snippet for embedding the badge image.
func (h *Badges) BadgeMarkdown() string {
if h.err != nil {
return ""
}
return fmt.Sprintf(`<img src="%s">`, h.outputFile)
}
// Err returns any error that occurred during the initialization or processing
func (h *Badges) Err() error {
return h.err
}
// GoHandler returns the internal Go handler
func (h *Badges) GoHandler() *Go {
return h.goH
}
func GetGoVersion() string {
out, err := RunCommand("go", "version")
if err != nil {
return "unknown"
}
parts := strings.Fields(out)
if len(parts) >= 3 {
return strings.TrimPrefix(parts[2], "go")
}
return "unknown"
}
func GetBadgeColor(typ, value string) string {
switch typ {
case "license", "go":
return "#007acc"
case "tests":
if value == "Passing" {
return "#4c1"
}
return "#e05d44"
case "coverage":
val, _ := strconv.ParseFloat(value, 64)
if val >= 80 {
return "#4c1"
} else if val >= 60 {
return "#dfb317"
} else if val > 0 {
return "#fe7d37"
}
return "#e05d44"
case "race":
if value == "Clean" {
return "#4c1"
}
return "#e05d44"
case "vet":
if value == "OK" {
return "#4c1"
}
return "#e05d44"
}
return "#007acc"
}
func getModuleName(dir string) (string, error) {
f, err := os.Open(filepath.Join(dir, "go.mod"))
if err != nil {
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "module ") {
return strings.TrimPrefix(line, "module "), nil
}
}
return "", fmt.Errorf("module name not found in go.mod")
}
func checkFileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// UpdateBadges generates badge SVG and updates the README using the provided values.
func (h *Badges) UpdateBadges(readmeFile, licenseType, goVer, testStatus, coveragePercent, raceStatus, vetStatus string, quiet bool) error {
return h.updateBadges(readmeFile, licenseType, goVer, testStatus, coveragePercent, raceStatus, vetStatus, quiet)
}
func (h *Badges) updateBadges(readmeFile, licenseType, goVer, testStatus, coveragePercent, raceStatus, vetStatus string, quiet bool) error {
// Colors
licenseColor := GetBadgeColor("license", licenseType)
goColor := GetBadgeColor("go", goVer)
testColor := GetBadgeColor("tests", testStatus)
coverageColor := GetBadgeColor("coverage", coveragePercent)
raceColor := GetBadgeColor("race", raceStatus)
vetColor := GetBadgeColor("vet", vetStatus)
badgeArgs := []string{
"readmefile:" + readmeFile,
fmt.Sprintf("License:%s:%s", licenseType, licenseColor),
fmt.Sprintf("Go:%s:%s", goVer, goColor),
fmt.Sprintf("Tests:%s:%s", testStatus, testColor),
fmt.Sprintf("Coverage:%s%%:%s", coveragePercent, coverageColor),
fmt.Sprintf("Race:%s:%s", raceStatus, raceColor),
fmt.Sprintf("Vet:%s:%s", vetStatus, vetColor),
}
bh := NewBadges(badgeArgs...)
bh.SetLog(h.log)
if _, err := bh.BuildBadges(); err != nil {
return fmt.Errorf("error building badges: %w", err)
}
if !quiet {
h.log(fmt.Sprintf("Updated badges in %s", readmeFile))
}
return nil
}