-
Notifications
You must be signed in to change notification settings - Fork 46
/
magefile.go
130 lines (110 loc) · 2.68 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
// +build mage
// magefile inspired/copied from Hugo's: https://github.com/gohugoio/hugo/blob/master/magefile.go
package main
import (
"fmt"
"os"
"os/exec"
"path"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
packageName = "github.com/ropnop/go-windapsearch"
)
var (
curDir string
binDir string
distDir string
hash string
buildDate string
buildNum string
version string
)
var ldflags = `-w -s` +
` -X $PKG/pkg/buildinfo.GitSHA=$GIT_SHA` +
` -X $PKG/pkg/buildinfo.BuildDate=$DATE` +
` -X $PKG/pkg/buildinfo.Version=$VERSION` +
` -X $PKG/pkg/buildinfo.BuildNumber=$BUILDNUM`
var targets = "linux/amd64 darwin/amd64 windows/amd64"
var goexe = "go"
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
curDir, err := os.Getwd()
if err != nil {
curDir = "." //hack
}
binDir = curDir
distDir = path.Join(curDir, "dist")
}
// Build Compile windapsearch for current OS and ARCH
func Build() error {
err := sh.RunWith(flagEnv(), goexe, "install", "-ldflags", ldflags, "$PKG/cmd/...")
if err != nil {
return err
}
fmt.Printf("[+] Compiled binary to %s/\n", binDir)
return nil
}
var gox = sh.RunCmd("gox")
// Dist Cross-compile for Windows, Linux, Mac x64 and put in ./dist
func Dist() error {
ldflags += " -extldflags \"-static\""
mg.Deps(checkGox)
fmt.Printf("[+] Cross compiling for: %q\n", targets)
err := sh.RunWith(
flagEnv(),
"gox",
"-parallel=3",
"-output",
"$DISTDIR/{{.Dir}}-{{.OS}}-{{.Arch}}",
"--osarch=$TARGETS",
"-ldflags",
ldflags,
"$PKG/cmd/...")
if err != nil {
return err
}
fmt.Printf("[+] Cross compiled binaries in: %s\n", distDir)
return nil
}
func checkGox() error {
_, err := exec.LookPath("gox")
if err != nil {
return sh.Run(goexe, "get", "-u", "github.com/mitchellh/gox")
}
return nil
}
// Clean Delete bin and dist dirs
func Clean() {
fmt.Println("[+] Removing bin and dist...")
os.RemoveAll(binDir)
os.RemoveAll(distDir)
}
// set up environment variables
func flagEnv() map[string]string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
if version = os.Getenv("VERSION"); version == "" {
version = "dev"
}
if buildNum = os.Getenv("BUILDNUM"); buildNum == "" {
buildNum = "local"
}
return map[string]string{
"PKG": packageName,
"GOBIN": binDir,
"GIT_SHA": hash,
"DATE": time.Now().Format("01/02/06"),
"VERSION": version,
"BUILDNUM": buildNum,
"DISTDIR": distDir,
"CGO_ENABLED": "1", //bug: when this is disabled, DNS gets wonky
"TARGETS": targets,
}
}