This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmagefile.go
198 lines (182 loc) · 4.07 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
195
196
197
198
// +build mage
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"text/template"
"github.com/google/renameio"
"github.com/magefile/mage/sh"
)
const (
module = "github.com/gbrlsnchs/pilgo"
dstdir = ".bin"
sep = string(filepath.Separator)
)
var (
env = map[string]string{
"CGO": "0",
"GOARCH": "amd64",
}
exes = []string{"plg"}
platforms = []string{
"darwin",
"linux",
"windows",
}
)
var Default = Build
func Build() error {
tag, err := exec.Command("git", "tag", "--points-at=HEAD").Output() // git tag --points-at=HEAD
if err != nil {
return err
}
if string(tag) == "" {
return errors.New("aborting build: no tag found")
}
for _, ptf := range platforms {
env["GOOS"] = ptf
for _, x := range exes {
fmt.Printf("Building %q for %q...\n", x, ptf)
src := filepath.Join(module, "cmd", x)
dst := filepath.Join(dstdir, ptf, executable(x, ptf))
ldflags := ldflagsVar("internal.version", string(tag))
err := sh.RunWith(env, "go", "build", "-o", dst, "-ldflags", ldflags, src)
if err != nil {
return err
}
}
}
return nil
}
type linuxTxt string
const (
slash linuxTxt = "/"
fileNotFound linuxTxt = "no such file or directory"
readDirErr linuxTxt = "readdirent: not a directory"
)
type replacement struct {
old linuxTxt
new string
}
type testPatch struct {
reps replacement
buildData func([]byte) map[string]interface{}
}
// GenCLITests copies Linux E2E CLI tests and properly adapts it to other platforms.
func GenCLITests() error {
const linuxdir = "linux"
oses := map[string]map[string][]testPatch{
"darwin": {
".*": {
{
reps: replacement{
old: readDirErr,
new: "fdopendir: not a directory",
},
},
},
},
"windows": {
".*": {
{
reps: replacement{
old: slash,
new: "\\",
},
},
{
reps: replacement{
old: fileNotFound,
new: "The system cannot find the file specified.",
},
},
},
"scan.ct": {
{
reps: replacement{
old: readDirErr,
new: "Readdir {{.scandir}}: The system cannot find the path specified.",
},
buildData: func(b []byte) map[string]interface{} {
r := regexp.MustCompile(`plg scan (\w+) --> FAIL`)
subs := r.FindSubmatch(b)
t := make(map[string]interface{})
if len(subs) > 1 {
t["scandir"] = string(subs[1])
}
return t
},
},
},
},
}
for sys, regexes := range oses {
testdir := filepath.Join("cmd", "plg", "testdata", "TestCLI")
windir := filepath.Join(testdir, sys)
if err := os.RemoveAll(windir); err != nil {
return err
}
if err := os.Mkdir(windir, 0o755); err != nil {
return err
}
srcdir := filepath.Join(testdir, linuxdir)
files, err := ioutil.ReadDir(filepath.Join(testdir, linuxdir))
if err != nil {
return err
}
for _, fi := range files {
fname := fi.Name()
b, err := ioutil.ReadFile(filepath.Join(srcdir, fname))
if err != nil {
return err
}
for rgx, repls := range regexes {
match, err := regexp.MatchString(rgx, fname)
if err != nil {
return err
}
if !match {
continue
}
for _, p := range repls {
r := p.reps
b = bytes.ReplaceAll(b, []byte(r.old), []byte(r.new))
if p.buildData != nil {
data := p.buildData(b)
tmpl := template.Must(
template.New(fname).Parse(string(b)))
buf := bytes.NewBuffer(make([]byte, 0, len(b)))
if err = tmpl.Execute(buf, data); err != nil {
return err
}
b = buf.Bytes()
}
}
}
if err := renameio.WriteFile(filepath.Join(windir, fname),
b, fi.Mode().Perm()); err != nil {
return err
}
}
}
return nil
}
func Test() error {
ldflags := ldflagsVar("internal.version", "refs/tags/v0.0.0-test")
return sh.RunWith(env, "go", "test", "-race", "-ldflags", ldflags, "./...")
}
func executable(x string, platform string) string {
if platform == "windows" {
return x + ".exe"
}
return x
}
func ldflagsVar(name, value string) string {
return fmt.Sprintf("-X %s/cmd/%s=%s", module, name, value)
}