-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
103 lines (83 loc) · 1.63 KB
/
magefile.go
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
//go:build mage
// +build mage
package main
import (
"fmt"
"path"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const name string = "ambilight"
const buildDir string = "build"
type (
Build mg.Namespace
Test mg.Namespace
Docker mg.Namespace
CI mg.Namespace
Install mg.Namespace
)
type BuildOptions struct {
Static bool
Extension string
}
func build(os string, arch string, opts BuildOptions) error {
env := map[string]string{
"GOOS": os,
"GOARCH": arch,
}
// Build static binary
if opts.Static {
env["CGO_ENABLED"] = "0"
}
outputName := fmt.Sprintf(
"%s-%s-%s", name, os, arch,
)
outputPath := path.Join(
buildDir,
outputName,
)
if opts.Extension != "" {
outputPath += fmt.Sprintf(".%s", opts.Extension)
}
args := []string{
"build",
"-o",
outputPath,
"-ldflags",
"-s -w",
".",
}
return sh.RunWith(
env, "go", args...,
)
}
// Builds all binaries
func (Build) All() {
mg.Deps(
Build.DarwinAmd64,
Build.LinuxAmd64,
Build.WindowsAmd64,
)
}
// Builds the binary for Linux (amd64)
func (Build) LinuxAmd64() error {
return build("linux", "amd64", BuildOptions{})
}
// Builds the binary for Windows (amd64)
func (Build) WindowsAmd64() error {
return build("windows", "amd64", BuildOptions{
Extension: "exe",
})
}
// Builds the binary for Darwin/macOS (amd64)
func (Build) DarwinAmd64() error {
return build("darwin", "amd64", BuildOptions{})
}
// Compresses all binaries into a single tarball
func (CI) CompressBinaries() error {
return sh.Run("tar", "-cvzf", "binaries.tar.gz", "build")
}
// Cleans up build directories
func Clean() {
sh.Rm("build")
}