summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 4d790797204f57dff6038975ebc4a32033df2205 (plain)
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
//go:build mage

// Hexai mage targets: build, dev, test, lint, install, etc.
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/magefile/mage/mg"
	"github.com/magefile/mage/sh"
)

var (
	Default                   = Build // Default target: build all binaries.
	coverageThreshold float64 = 80
	coveragePrinted           = make(chan struct{}, 1)
)

// Build builds binaries.
func Build() error {
	mg.Deps(BuildHexaiLSP, BuildHexaiCLI, BuildHexaiTmuxAction, BuildHexaiTmuxEdit, BuildHexaiMCPServer)
	printCoverage()
	return nil
}

// BuildHexaiLSP builds the LSP server binary.
func BuildHexaiLSP() error {
	printCoverage()
	return sh.RunV("go", "build", "-o", "hexai-lsp-server", "cmd/hexai-lsp-server/main.go")
}

// BuildHexaiCLI builds the CLI binary.
func BuildHexaiCLI() error {
	printCoverage()
	return sh.RunV("go", "build", "-o", "hexai", "cmd/hexai/main.go")
}

// BuildHexaiTmuxAction builds the hexai-tmux-action TUI binary.
func BuildHexaiTmuxAction() error {
	printCoverage()
	return sh.RunV("go", "build", "-o", "hexai-tmux-action", "cmd/hexai-tmux-action/main.go")
}

// BuildHexaiTmuxEdit builds the hexai-tmux-edit popup editor binary.
func BuildHexaiTmuxEdit() error {
	printCoverage()
	return sh.RunV("go", "build", "-o", "hexai-tmux-edit", "cmd/hexai-tmux-edit/main.go")
}

// BuildHexaiMCPServer builds the MCP server binary (DEPRECATED - experimental, not actively maintained).
func BuildHexaiMCPServer() error {
	printCoverage()
	return sh.RunV("go", "build", "-o", "hexai-mcp-server", "cmd/hexai-mcp-server/main.go")
}

// Dev runs tests, vet, lint, then builds with race for all binaries.
func Dev() error {
	printCoverage()
	mg.Deps(Test, Vet, Lint)
	if err := sh.RunV("go", "build", "-race", "-o", "hexai-lsp-server", "cmd/hexai-lsp-server/main.go"); err != nil {
		return err
	}
	if err := sh.RunV("go", "build", "-race", "-o", "hexai", "cmd/hexai/main.go"); err != nil {
		return err
	}
	if err := sh.RunV("go", "build", "-race", "-o", "hexai-tmux-action", "cmd/hexai-tmux-action/main.go"); err != nil {
		return err
	}
	if err := sh.RunV("go", "build", "-race", "-o", "hexai-tmux-edit", "cmd/hexai-tmux-edit/main.go"); err != nil {
		return err
	}
	return sh.RunV("go", "build", "-race", "-o", "hexai-mcp-server", "cmd/hexai-mcp-server/main.go")
}

// Run launches the LSP server via go run (useful during development).
func Run() error {
	printCoverage()
	mg.Deps(Dev)
	return sh.RunV("go", "run", "cmd/hexai-lsp-server/main.go")
}

// RunCLI runs the CLI with a small test input.
func RunCLI() error {
	printCoverage()
	mg.Deps(Dev)
	cmd := "echo 'test' | go run cmd/hexai/main.go"
	return sh.RunV("bash", "-lc", cmd)
}

// Install copies built binaries to GOPATH/bin (defaults to ~/go/bin when GOPATH is unset).
func Install() error {
	printCoverage()
	mg.Deps(Build)
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		home, err := os.UserHomeDir()
		if err != nil {
			return fmt.Errorf("resolve home: %w", err)
		}
		gopath = filepath.Join(home, "go")
	}
	bin := filepath.Join(gopath, "bin")
	if err := os.MkdirAll(bin, 0o755); err != nil {
		return err
	}
	if err := sh.RunV("cp", "-v", "./hexai-lsp-server", bin+"/"); err != nil {
		return err
	}
	if err := sh.RunV("cp", "-v", "./hexai", bin+"/"); err != nil {
		return err
	}
	if err := sh.RunV("cp", "-v", "./hexai-tmux-action", bin+"/"); err != nil {
		return err
	}
	if err := sh.RunV("cp", "-v", "./hexai-tmux-edit", bin+"/"); err != nil {
		return err
	}
	return sh.RunV("cp", "-v", "./hexai-mcp-server", bin+"/")
}

// RunTmuxAction runs the hexai-tmux-action TUI via go run (reads stdin).
func RunTmuxAction() error {
	printCoverage()
	mg.Deps(Dev)
	return sh.RunV("go", "run", "cmd/hexai-tmux-action/main.go")
}

// printCoverage prints a warning if an existing coverage profile shows total < coverateThreshold.
func printCoverage() {
	// Ensure the top-level coverage profile is refreshed at least once per day.
	ensureDailyCoverage(24 * time.Hour)
	select {
	case coveragePrinted <- struct{}{}:
	default:
		// Coverage already printed
		return
	}

	profile := ""
	if _, err := os.Stat("docs/coverage.out"); err == nil {
		profile = "docs/coverage.out"
	}
	if profile == "" {
		fmt.Println("[coverage] No coverage profile found (run 'mage cover' or 'mage coverall').")
		return
	}
	pct, ok := totalCoveragePercent(profile)
	if !ok {
		// Attempt a one-time regen if the profile is malformed
		if err := Coverage(); err == nil {
			if p2, ok2 := totalCoveragePercent(profile); ok2 {
				pct = p2
				ok = true
			}
		}
	}
	if !ok {
		fmt.Println("[coverage] Could not parse total coverage from", profile)
		return
	}
	if pct < coverageThreshold {
		fmt.Printf("[coverage] WARNING: total test coverage is %.1f%% (< %.1f%%)\n", pct, coverageThreshold)
	} else {
		fmt.Printf("[coverage] total test coverage: %.1f%% (>= %.1f%%)\n", pct, coverageThreshold)
	}
}

// ensureDailyCoverage regenerates the main coverage profile when it's missing
// or older than maxAge. It writes to docs/coverage.out via the Coverage target.
func ensureDailyCoverage(maxAge time.Duration) {
	const prof = "docs/coverage.out"
	st, err := os.Stat(prof)
	if err == nil {
		age := time.Since(st.ModTime())
		if age <= maxAge {
			return // fresh enough
		}
	}
	// Missing or stale; attempt to refresh. Do not hard-fail builds if coverage fails.
	if err := Coverage(); err != nil {
		fmt.Println("[coverage] refresh skipped due to error:", err)
	}
}

// totalCoveragePercent returns the parsed total percentage from a coverage profile using `go tool cover -func`.
func totalCoveragePercent(profile string) (float64, bool) {
	out, err := sh.Output("go", "tool", "cover", "-func="+profile)
	if err != nil {
		return 0, false
	}
	// Find a line like: "total:\t(statements)\t75.3%"
	re := regexp.MustCompile(`(?m)^total:\s*\(statements\)\s*([0-9]+\.[0-9]+|[0-9]+)%\s*$`)
	m := re.FindStringSubmatch(out)
	if len(m) != 2 {
		return 0, false
	}
	f, err := strconv.ParseFloat(m[1], 64)
	if err != nil {
		return 0, false
	}
	return f, true
}

// Test runs the test suite.
func Test() error {
	if err := sh.RunV("go", "clean", "-testcache"); err != nil {
		return err
	}
	return sh.RunV("go", "test", "-v", "./...")
}

// Coverage generates a combined coverage profile across all packages (cross-package coverage).
// Instruments all packages during each test run using -coverpkg=./... so that
// coverage collected from one package's tests include code executed in others.
func Coverage() error {
	const prof = "docs/coverage.out"
	const html = "docs/coverage.html"
	_ = os.Remove(prof)
	_ = os.Remove(html)
	if err := sh.RunV("go", "clean", "-testcache"); err != nil {
		return err
	}
	if err := sh.RunV("go", "test", "-covermode=atomic", "-coverpkg=./...", "-coverprofile="+prof, "./..."); err != nil {
		return err
	}
	if out, err := sh.Output("go", "tool", "cover", "-func="+prof); err == nil {
		fmt.Print(out)
		lines := strings.Split(strings.TrimSpace(out), "\n")
		for i := len(lines) - 1; i >= 0; i-- {
			if strings.HasPrefix(strings.TrimSpace(lines[i]), "total:") {
				fmt.Println("\nTotal coverage (cross-package):", strings.TrimSpace(lines[i]))
				break
			}
		}
	} else {
		return err
	}
	if err := sh.RunV("go", "tool", "cover", "-html="+prof, "-o", html); err != nil {
		return err
	}
	fmt.Println("HTML coverage report written to " + html + " (cross-package)")
	return nil
}

// Vet runs go vet.
func Vet() error {
	return sh.RunV("go", "vet", "./...")
}

// Lint runs golangci-lint.
func Lint() error {
	return sh.RunV("golangci-lint", "run")
}

// DevInstall installs helpful developer tools.
func DevInstall() error {
	if err := sh.RunV("go", "install", "golang.org/x/tools/gopls@latest"); err != nil {
		return err
	}
	return sh.RunV("go", "install", "github.com/golangci/golangci-lint/cmd/golangci-lint@latest")
}

// CoverCheck enforces minimum per-package coverage.
// Exceptions: any package whose import path contains "/cmd/" and any substring
// provided via HEXAI_COVER_EXCEPT (comma-separated).
func CoverCheck() error {
	except := []string{"/cmd/"}
	if v := strings.TrimSpace(os.Getenv("HEXAI_COVER_EXCEPT")); v != "" {
		parts := strings.Split(v, ",")
		for _, p := range parts {
			if s := strings.TrimSpace(p); s != "" {
				except = append(except, s)
			}
		}
	}
	list, err := sh.Output("go", "list", "./...")
	if err != nil {
		return err
	}
	pkgs := strings.Split(strings.TrimSpace(list), "\n")
	mod := modulePathGuess()
	_ = os.MkdirAll("docs/coverage", 0o755)
	type res struct {
		pkg   string
		total float64
	}
	var all, bad []res
	for _, pkg := range pkgs {
		if pkg == "" {
			continue
		}
		skip := false
		for _, ex := range except {
			if strings.Contains(pkg, ex) {
				skip = true
				break
			}
		}
		if skip {
			continue
		}
		safe := strings.ReplaceAll(strings.TrimPrefix(pkg, mod), "/", "_")
		if safe == pkg {
			if i := strings.LastIndex(pkg, "/"); i >= 0 {
				safe = pkg[i+1:]
			}
		}
		prof := filepath.Join("docs", "coverage", safe+".out")
		// Per-package run; ignore errors to allow packages without tests
		_, _ = sh.Output("go", "test", "-covermode=count", "-coverprofile", prof, pkg)
		// Read total
		total, ok := totalCoveragePercent(prof)
		if !ok {
			total = 0
		}
		all = append(all, res{pkg, total})
		if total < coverageThreshold {
			bad = append(bad, res{pkg, total})
		}
		time.Sleep(10 * time.Millisecond)
	}
	fmt.Printf("Per-package coverage (threshold %.1f%%)\n", coverageThreshold)
	for _, r := range all {
		fmt.Printf("- %s: %.1f%%\n", r.pkg, r.total)
	}
	if len(bad) > 0 {
		fmt.Println("\nPackages below threshold:")
		for _, r := range bad {
			fmt.Printf("- %s: %.1f%%\n", r.pkg, r.total)
		}
		return fmt.Errorf("coverage check failed (%d package(s) < %.1f%%)", len(bad), coverageThreshold)
	}
	fmt.Println("All packages meet coverage threshold.")
	return nil
}

func modulePathGuess() string {
	if out, err := sh.Output("go", "list", "-m"); err == nil {
		return strings.TrimSpace(out)
	}
	return ""
}