-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagefile.go
More file actions
283 lines (245 loc) · 6.73 KB
/
magefile.go
File metadata and controls
283 lines (245 loc) · 6.73 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
//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
binaryName = "snirect"
buildDir = "dist"
cmdPath = "./cmd/snirect"
rulesURL = "https://github.com/SpaceTimee/Cealing-Host/raw/refs/heads/main/Cealing-Host.json"
)
var (
// Default target
Default = Build
)
// getVersion returns the version in format TAGVERSION[-N][-dirty]
// - TAGVERSION: latest git tag (e.g., v1.0.0)
// - N (optional): distance (commits ahead) from the tagged commit, only included if > 0
// - dirty (optional): append "-dirty" if there are unstaged changes
func getVersion() string {
// Get the latest tag reachable from HEAD
tag, err := sh.Output("git", "describe", "--tags", "--abbrev=0")
if err != nil {
// No tags found, fallback to commit hash
hash, hashErr := sh.Output("git", "rev-parse", "--short", "HEAD")
if hashErr != nil {
return "0.0.0-dev"
}
// Check dirty status
if isDirty() {
return hash + "-dirty"
}
return hash
}
// Get distance from the tag to HEAD (number of commits)
dist, err := sh.Output("git", "rev-list", "--count", tag+"..HEAD")
if err != nil || dist == "" {
dist = "0"
}
// Build version string: tag only if distance is 0, otherwise tag-distance
version := tag
if dist != "0" {
version = fmt.Sprintf("%s-%s", tag, dist)
}
// Append dirty suffix if there are unstaged changes
if isDirty() {
version += "-dirty"
}
return version
}
// isDirty checks if there are any unstaged changes in the working directory
func isDirty() bool {
// Check modified but unstaged files
out, err := sh.Output("git", "status", "--porcelain")
return err == nil && strings.TrimSpace(out) != ""
}
// getLDFLAGS returns the linker flags
func getLDFLAGS() string {
return fmt.Sprintf("-s -w -X 'snirect/internal/cmd.Version=%s'", getVersion())
}
// Generate runs code generation
func Generate() error {
fmt.Println("Running code generation...")
return sh.RunV("go", "generate", "./...")
}
// GenerateCompletions generates shell completion scripts
func GenerateCompletions() error {
fmt.Println("Generating completions...")
compDir := filepath.Join("internal", "cmd", "completions")
os.RemoveAll(compDir)
if err := os.MkdirAll(compDir, 0755); err != nil {
return err
}
shells := []string{"bash", "zsh", "fish", "powershell"}
for _, shell := range shells {
out, _ := sh.Output("go", "run", cmdPath, "completion", shell)
if out != "" {
err := os.WriteFile(filepath.Join(compDir, shell), []byte(out), 0644)
if err != nil {
fmt.Printf("Warning: failed to write %s completion: %v\n", shell, err)
}
}
}
return nil
}
// Build compiles the binary
func Build() error {
mg.Deps(Generate)
mg.Deps(GenerateCompletions)
if err := os.MkdirAll(buildDir, 0755); err != nil {
return err
}
tags := os.Getenv("TAGS")
return sh.RunV("go", "build", "-tags", tags, "-ldflags", getLDFLAGS(), "-o", filepath.Join(buildDir, binaryName), cmdPath)
}
// Release compiles the binary (currently identical to build)
func Release() error {
return Build()
}
// Full builds with all features (includes QUIC)
func Full() error {
os.Setenv("TAGS", "quic")
return Release()
}
// Clean removes build artifacts
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(buildDir)
os.Remove("log.txt")
os.RemoveAll(filepath.Join("internal", "cmd", "completions"))
}
// UpdateRules downloads and converts rules from upstream to shared library
func UpdateRules() error {
fmt.Printf("Updating rules from %s...\n", rulesURL)
rawPath := filepath.Join("internal", "config", "rules.raw.json")
// Convert and write to shared library
targetPath := filepath.Join("..", "snirect-shared", "rules", "fetched.toml")
if err := sh.RunV("curl", "-sSL", rulesURL, "-o", rawPath); err != nil {
return err
}
defer os.Remove(rawPath)
// Call convert_rules from shared library
return sh.RunV("go", "run", "github.com/xihale/snirect-shared/tools/convert_rules", rawPath, targetPath)
}
// CrossAll performs cross-platform builds
func CrossAll() error {
mg.Deps(Clean)
mg.Deps(Generate, GenerateCompletions)
if err := os.MkdirAll(buildDir, 0755); err != nil {
return err
}
type target struct {
os string
arch string
}
targets := []target{
{"linux", "amd64"},
{"linux", "arm64"},
{"darwin", "amd64"},
{"darwin", "arm64"},
{"windows", "amd64"},
{"windows", "arm64"},
}
var wg sync.WaitGroup
errs := make(chan error, len(targets))
for _, t := range targets {
wg.Add(1)
go func(t target) {
defer wg.Done()
output := filepath.Join(buildDir, fmt.Sprintf("%s-%s-%s", binaryName, t.os, t.arch))
if t.os == "windows" {
output += ".exe"
}
fmt.Printf("Building for %s/%s...\n", t.os, t.arch)
env := map[string]string{
"GOOS": t.os,
"GOARCH": t.arch,
}
err := sh.RunWithV(env, "go", "build", "-ldflags", getLDFLAGS(), "-o", output, cmdPath)
if err != nil {
errs <- fmt.Errorf("failed build for %s/%s: %v", t.os, t.arch, err)
}
}(t)
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
return err
}
}
return nil
}
// Checksum generates sha256 checksums for files in dist
func Checksum() error {
files, err := filepath.Glob(filepath.Join(buildDir, "*"))
if err != nil {
return err
}
var checksums string
for _, f := range files {
if filepath.Base(f) == "checksums.txt" {
continue
}
sum, err := sh.Output("sha256sum", f)
if err != nil {
// Try shasum -a 256 for macOS
sum, err = sh.Output("shasum", "-a", "256", f)
if err != nil {
continue
}
}
checksums += sum + "\n"
}
return os.WriteFile(filepath.Join(buildDir, "checksums.txt"), []byte(checksums), 0644)
}
// Install builds and runs the internal install logic
func Install() error {
mg.Deps(Build)
bin := filepath.Join(buildDir, binaryName)
if runtime.GOOS == "windows" {
bin += ".exe"
}
return sh.RunV(bin, "install")
}
// Uninstall builds and runs the internal uninstall logic
func Uninstall() error {
mg.Deps(Build)
bin := filepath.Join(buildDir, binaryName)
if runtime.GOOS == "windows" {
bin += ".exe"
}
return sh.RunV(bin, "uninstall")
}
// Upx compresses binaries in dist with UPX
func Upx() error {
files, err := filepath.Glob(filepath.Join(buildDir, binaryName+"-*"))
if err != nil {
return err
}
for _, f := range files {
if strings.Contains(f, "windows-arm64") {
continue // UPX usually doesn't support windows/arm64 well or at all
}
sh.Run("upx", f)
}
return nil
}
// Lint runs golangci-lint on the codebase
func Lint() error {
fmt.Println("Running golangci-lint...")
// Ensure golangci-lint is installed (v1.61+)
if err := sh.Run("golangci-lint", "run", "--timeout=5m"); err != nil {
return fmt.Errorf("linting failed: %w", err)
}
fmt.Println("Linting passed")
return nil
}