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
|
//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"
)
// Default target: build both binaries.
var Default = Build
// Build builds the Hexai LSP and CLI binaries.
func Build() error {
mg.Deps(BuildHexaiLSP, BuildHexaiCLI)
warnIfLowCoverage(80.0)
return nil
}
// BuildHexaiLSP builds the LSP server binary.
func BuildHexaiLSP() error {
return sh.RunV("go", "build", "-o", "hexai-lsp", "cmd/hexai-lsp/main.go")
}
// BuildHexaiCLI builds the CLI binary.
func BuildHexaiCLI() error {
return sh.RunV("go", "build", "-o", "hexai", "cmd/hexai/main.go")
}
// Dev runs tests, vet, lint, then builds with race for both binaries.
func Dev() error {
mg.Deps(Test, Vet, Lint)
if err := sh.RunV("go", "build", "-race", "-o", "hexai-lsp", "cmd/hexai-lsp/main.go"); err != nil {
return err
}
return sh.RunV("go", "build", "-race", "-o", "hexai", "cmd/hexai/main.go")
}
// Run launches the LSP server via go run (useful during development).
func Run() error {
mg.Deps(Dev)
return sh.RunV("go", "run", "cmd/hexai-lsp/main.go")
}
// RunCLI runs the CLI with a small test input.
func RunCLI() error {
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 {
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", bin+"/"); err != nil {
return err
}
return sh.RunV("cp", "-v", "./hexai", bin+"/")
}
// warnIfLowCoverage prints a warning if an existing coverage profile shows total < threshold.
func warnIfLowCoverage(threshold float64) {
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 {
fmt.Println("[coverage] Could not parse total coverage from", profile)
return
}
if pct < threshold {
fmt.Printf("WARNING: total test coverage is %.1f%% (< %.1f%%)\n", pct, threshold)
} else {
fmt.Printf("[coverage] total test coverage: %.1f%% (>= %.1f%%)\n", pct, threshold)
}
}
// 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.
// Default threshold is 80.0; override with HEXAI_COVER_THRESH.
// Exceptions: any package whose import path contains "/cmd/" and any substring
// provided via HEXAI_COVER_EXCEPT (comma-separated).
func CoverCheck() error {
threshold := 80.0
if v := strings.TrimSpace(os.Getenv("HEXAI_COVER_THRESH")); v != "" {
if f, err := strconv.ParseFloat(v, 64); err == nil { threshold = f }
}
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 < threshold { bad = append(bad, res{pkg, total}) }
time.Sleep(10 * time.Millisecond)
}
fmt.Printf("Per-package coverage (threshold %.1f%%)\n", threshold)
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), threshold)
}
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 ""
}
|