-
Notifications
You must be signed in to change notification settings - Fork 1
/
magefile.go
174 lines (148 loc) · 3.55 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
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
// +build mage
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
appName = "cos"
cmdPkg = "github.com/dmolesUC3/cos/cmd"
)
var gocmd = mg.GoCmd()
var oses = []string{"darwin", "linux", "windows"}
var architectures = []string{"amd64"}
// ------------------------------------------------------------
// Targets
// Install installs in $GOPATH/bin.
func Install() error {
binName := appName
if runtime.GOOS == "windows" {
binName += ".exe"
}
gopath, err := sh.Output(gocmd, "env", "GOPATH")
if err != nil {
return fmt.Errorf("error determining GOPATH: %v", err)
}
binDir := filepath.Join(gopath, "bin")
binPath := filepath.Join(binDir, binName)
flags, err := ldFlags()
if err != nil {
return fmt.Errorf("error determining ldflags: %v", err)
}
return sh.RunV(gocmd, "build", "-o", binPath, "-ldflags", flags)
}
// Build builds a binary for the current platform.
func Build() error {
binName := appName
if runtime.GOOS == "windows" {
binName += ".exe"
}
flags, err := ldFlags()
if err != nil {
return fmt.Errorf("error determining ldflags: %v", err)
}
return sh.RunV(gocmd, "build", "-ldflags", flags)
}
// BuildLinux builds a linux-amd64 binary (the most common cross-compile case).
func BuildLinux() error {
return buildFor("linux", "amd64")
}
// BuildAll builds a binary for each target platform.
func BuildAll() error {
for _, os_ := range oses {
for _, arch := range architectures {
err := buildFor(os_, arch)
if err != nil {
return err
}
}
}
return nil
}
// Platforms lists target platforms for buildAll.
func Platforms() {
for _, os_ := range oses {
for _, arch := range architectures {
fmt.Printf("%s-%s\n", os_, arch)
}
}
}
// Clean removes compiled binaries from the current working directory.
func Clean() error {
var binRe = regexp.MustCompile("^" + appName + "(-[a-zA-Z0-9]+-[a-zA-Z0-9]+)?(.exe)?$")
rmcmd := "rm"
if runtime.GOOS == "windows" {
rmcmd = "del"
}
files, err := ioutil.ReadDir("./")
if err != nil {
return err
}
for _, f := range files {
mode := f.Mode()
isPlainFile := mode.IsRegular() && mode&os.ModeSymlink == 0
isExecutable := mode&0111 != 0
if isPlainFile && isExecutable {
name := f.Name()
if binRe.MatchString(name) {
err := sh.RunV(rmcmd, name)
if err != nil {
return err
}
}
}
}
return nil
}
// ------------------------------------------------------------
// Helper functions
func ldFlags() (string, error) {
commitHash, err := sh.Output("git", "rev-parse", "--short", "HEAD")
if err != nil {
return "", err
}
tag, err := sh.Output("git", "describe", "--tags")
if err != nil {
return "", err
}
timestamp := time.Now().Format(time.RFC3339)
flagVals := map[string]string{
"commitHash": commitHash,
"tag": tag,
"timestamp": timestamp,
}
var flags []string
for k, v := range flagVals {
flag := fmt.Sprintf("-X %s.%s=%s", cmdPkg, k, v)
flags = append(flags, flag)
}
return strings.Join(flags, " "), nil
}
func binNameFor(os_ string, arch string) string {
binName := appName
binName = fmt.Sprintf("%s-%s-%s", binName, os_, arch)
if os_ == "windows" {
binName += ".exe"
}
return binName
}
func buildFor(os_, arch string) error {
binName := binNameFor(os_, arch)
flags, err := ldFlags()
if err != nil {
return fmt.Errorf("error determining ldflags: %v", err)
}
env := map[string]string{
"GOOS": os_,
"GOARCH": arch,
}
return sh.RunWith(env, gocmd, "build", "-o", binName, "-ldflags", flags)
}