-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmagefile.go
More file actions
216 lines (186 loc) · 4.97 KB
/
Copy pathmagefile.go
File metadata and controls
216 lines (186 loc) · 4.97 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
//go:build mage
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"
"github.com/compozy/kb/internal/repohealth"
"github.com/magefile/mage/sh"
)
const (
golangciLintVersion = "v2.14.0"
gotestsumVersion = "v1.13.0"
goplsModernizeVersion = "v0.23.0"
binDir = "bin"
cliBinary = "kb"
versionPackage = "github.com/compozy/kb/internal/version"
)
var Default = Verify
func Deps() error {
return sh.RunV("go", "mod", "tidy")
}
func Fmt() error {
files, err := goFiles(".")
if err != nil {
return err
}
if len(files) == 0 {
return nil
}
args := append([]string{"-w"}, files...)
return sh.RunV("gofmt", args...)
}
func Lint() error {
if err := runGolangCILint(); err != nil {
return err
}
return Modernize()
}
func runGolangCILint() error {
args := []string{"run", "./..."}
if hasPinnedTool("golangci-lint", golangciLintVersion) {
return sh.RunV("golangci-lint", args...)
}
goRunArgs := append(
[]string{"run", "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@" + golangciLintVersion},
args...,
)
return sh.RunV("go", goRunArgs...)
}
// Modernize runs gopls' modernize analyzer to enforce idiomatic Go
// (any, min/max, range-over-int, slices/maps helpers, strings.Cut, ...).
// CGO stays enabled because the tree-sitter adapters require it to type-check.
func Modernize() error {
return sh.RunV(
"go",
"run",
"golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@"+goplsModernizeVersion,
"./...",
)
}
// hasPinnedTool reports whether an executable named name is on PATH and reports
// the pinned version. It runs `<name> version` and matches the output so a broken
// or mismatched shim (for example a mise shim with no version configured) is
// rejected in favor of `go run <module>@<version>`.
func hasPinnedTool(name string, wantVersion string) bool {
path, err := exec.LookPath(name)
if err != nil {
return false
}
output, err := exec.Command(path, "version").CombinedOutput()
if err != nil {
return false
}
versionToken := "version " + strings.TrimPrefix(wantVersion, "v")
return bytes.Contains(output, []byte(versionToken))
}
// Test runs unit tests only (no integration tag).
func Test() error {
return runGoTests("-race", "-parallel=4", "./...")
}
// TestIntegration runs all tests including integration tests.
func TestIntegration() error {
return runGoTests("-race", "-parallel=4", "-tags", "integration", "./...")
}
func Build() error {
return buildGo()
}
func buildGo() error {
ldflags := buildLDFlags()
if err := os.MkdirAll(binDir, 0o755); err != nil {
return err
}
if err := sh.RunV("go", "build", "-ldflags", ldflags, "./..."); err != nil {
return err
}
out := filepath.Join(binDir, cliBinary)
return sh.RunV("go", "build", "-ldflags", ldflags, "-o", out, "./cmd/"+cliBinary)
}
// Boundaries verifies that package import rules are not violated.
// Internal packages outside cli may not import the CLI layer.
func Boundaries() error {
violations, err := repohealth.FindPackageBoundaryViolations(".")
if err != nil {
return err
}
if len(violations) > 0 {
return fmt.Errorf("found %d boundary violations:\n%s", len(violations), repohealth.FormatPathViolations(violations))
}
fmt.Println("OK: all package boundaries respected")
return nil
}
func Verify() error {
steps := []func() error{
Fmt,
Lint,
Test,
buildGo,
Boundaries,
}
for _, step := range steps {
if err := step(); err != nil {
return err
}
}
return nil
}
func goFiles(root string) ([]string, error) {
var files []string
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
name := d.Name()
if path != root && (name == "vendor" || strings.HasPrefix(name, ".")) {
return filepath.SkipDir
}
return nil
}
if strings.HasSuffix(path, ".go") {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
sort.Strings(files)
return files, nil
}
func buildLDFlags() string {
version := gitOutput("describe", "--tags", "--always", "--dirty")
if version == "" {
version = "dev"
}
commit := gitOutput("rev-parse", "--short", "HEAD")
if commit == "" {
commit = "unknown"
}
buildDate := time.Now().UTC().Format(time.RFC3339)
return strings.Join([]string{
"-X " + versionPackage + ".Version=" + version,
"-X " + versionPackage + ".Commit=" + commit,
"-X " + versionPackage + ".Date=" + buildDate,
}, " ")
}
func gitOutput(args ...string) string {
cmd := exec.Command("git", args...)
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
func runGoTests(testArgs ...string) error {
// Always go run the pinned gotestsum so a broken or mismatched shim on PATH
// cannot break the test gate. The build is cached after the first run.
args := append([]string{"run", "gotest.tools/gotestsum@" + gotestsumVersion, "--format", "pkgname", "--"}, testArgs...)
return sh.RunV("go", args...)
}