-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagefile.go
194 lines (165 loc) · 4.14 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//go:build mage
// +build mage
package main
import (
"bufio"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
// tidy code
//func Fmt() error {
// packages := strings.Split("cmd", " ")
// files, _ := filepath.Glob("*.go")
// packages = append(packages, files...)
// return sh.Run("gofmt", append([]string{"-s", "-l", "-w"}, packages...)...)
//}
// for local machine build
func Build() error {
return buildTarget(runtime.GOOS, runtime.GOARCH, nil)
}
// build all platform
func Pack() error {
buildTarget("darwin", "amd64", nil)
buildTarget("darwin", "arm64", nil)
buildTarget("freebsd", "amd64", nil)
buildTarget("linux", "amd64", nil)
buildTarget("linux", "arm64", nil)
buildTarget("windows", "amd64", nil)
return genCheckSum()
}
// Test all packages
func Test() error {
err := sh.RunV("go", "test", "-v", "-coverprofile", ".cover.out", "./...")
if err != nil {
return err
}
err = sh.RunV("go", "tool", "cover", "-func=.cover.out")
if err != nil {
return err
}
err = sh.Run("go", "tool", "cover", "-html=.cover.out", "-o", ".cover.html")
if err != nil {
return err
}
return sh.Rm(".cover.out")
}
// build to target (cross build)
func buildTarget(OS, arch string, envs map[string]string) error {
tag := tag()
name := fmt.Sprintf("mysshw-%s-%s-%s", OS, arch, tag)
dir := fmt.Sprintf("dist/%s", name)
target_unix := fmt.Sprintf("%s/mysshw", dir)
target_win := fmt.Sprintf("%s/mysshw.exe", dir)
target := ""
if OS == "windows" {
target = target_win
}else{
target = target_unix
}
args := make([]string, 0, 10)
args = append(args, "build", "-o", target)
args = append(args, "-ldflags", flags(), "main.go")
fmt.Println("build", target)
env := make(map[string]string)
env["GOOS"] = OS
env["GOARCH"] = arch
env["CGO_ENABLED"] = "0"
if envs != nil {
for k, v := range envs {
env[k] = v
}
}
if err := sh.RunWith(env, mg.GoCmd(), args...); err != nil {
return err
}
// cp -a ./example/mysshw.toml ./dist/{name}/mysshw.toml
sh.Run("cp", "-a", "example/mysshw.toml", fmt.Sprintf("%s/mysshw.toml", dir))
sh.Run("tar", "-czf", fmt.Sprintf("%s.tar.gz", dir), "-C", "dist", name)
return nil
}
func flags() string {
hash := hash()
tag := tag()
buildTime := buildTime()
verStr := versionStr()
return fmt.Sprintf(`-s -w -X "main.Version=%s" -X "main.Build=%s-%s" -X "main.BuildTime=%s" -extldflags "-static"`, verStr, tag, hash, buildTime)
}
// tag returns the git tag for the current branch or "" if none.
func tag() string {
s, _ := sh.Output("bash", "-c", "git branch --show-current> /dev/null")
if s == "" {
return "main"
}
return s
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
func mod() string {
f, err := os.Open("go.mod")
if err == nil {
reader := bufio.NewReader(f)
line, _, _ := reader.ReadLine()
return strings.Replace(string(line), "module ", "", 1)
}
return ""
}
// cleanup all build files
func Clean() {
sh.Rm("dist")
sh.Rm(".cover.html")
}
func genCheckSum() error {
fmt.Println("generate checksum.txt file")
fs, err := ioutil.ReadDir("dist")
if err != nil {
return err
}
file, err := os.OpenFile("dist/checksum.txt", os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
for _, f := range fs {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), ".tar.gz") {
sum, _ := fileHash(fmt.Sprintf("dist/%s", f.Name()))
fmt.Println(sum, f.Name())
file.WriteString(fmt.Sprintf("%s %s\n", sum, f.Name()))
}
}
return nil
}
func fileHash(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
hash := sha1.New()
io.Copy(hash, file)
ret := hash.Sum(nil)
return hex.EncodeToString(ret[:]), nil
}
func buildTime() string {
s, _ := sh.Output("date", "+%Y-%m-%d %H:%M:%S")
return s
}
func versionStr() string {
s, _ := sh.Output("date", "+%y.%m.%d")
return fmt.Sprintf("v%s", s)
}