forked from johannesboyne/gofakes3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile.go
203 lines (173 loc) · 4.01 KB
/
makefile.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
199
200
201
202
203
//go:build tools
// +build tools
// Run this script like so:
//
// go run makefile.go <cmd> <args>...
//
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/shabbyrobe/gocovmerge"
"golang.org/x/tools/cover"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
var commandNames = []string{
"cover",
}
if len(os.Args) < 2 {
return fmt.Errorf("command missing: expected %s", commandNames)
}
command, args := os.Args[1], os.Args[2:]
switch command {
case "cover":
return runCover(args)
case "builddocker":
return runBuildDocker()
case "buildrelease":
return runBuildRelease()
case "release":
return runRelease(args)
default:
return fmt.Errorf("unknown command %v: expected %s", command, commandNames)
}
return nil
}
func runBuildDocker() error {
cmd := command(
"go", "build", "-a",
"-installsuffix", "cgo",
"-o", "./build/main",
"./cmd/gofakes3",
)
cmd.Env = append(cmd.Env, "GO111MODULE=on", "CGO_ENABLED=0", "GOOS=linux")
if err := cmd.Run(); err != nil {
return err
}
cmd = command("docker", "build", "-t", "JankariTech/gofakes3", ".")
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func runRelease(args []string) error {
if len(args) != 1 {
return fmt.Errorf("missing <version> argument")
}
version := args[0]
fmt.Printf("[+] releasing %s\n", version)
fmt.Println("[+] re-generating")
fmt.Println("[+] building")
if err := runBuildRelease(); err != nil {
return err
}
fmt.Println("[+] comitting")
if err := command("git", "tag", version).Run(); err != nil {
return err
}
fmt.Println("[+] complete")
return nil
}
func runBuildRelease() error {
var builds = map[string][]string{
"linux_amd64": {"GOOS=linux", "GOARCH=amd64"},
"linux_arm7": {"GOOS=linux", "GOARCH=arm", "GOARM=7"},
"darwin_amd64": {"GOOS=darwin", "GOARCH=amd64"},
"windows_amd64.exe": {"GOOS=windows", "GOARCH=amd64"},
}
for suffix, build := range builds {
cmd := command(
"go", "build",
"-o", fmt.Sprintf("./build/gofakes3_%s", suffix),
"./cmd/gofakes3",
)
cmd.Env = append(cmd.Env, "GO111MODULE=on")
cmd.Env = append(cmd.Env, build...)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
// runCover collects true code coverage for all packages in gofakes3.
// It does so by running 'go test' for each child package (enumerated by
// 'go list ./...') with the '-coverpkg' flag, populated with the same
// 'go list'.
func runCover(args []string) error {
pkgs := goList()
var files []string
for _, pkg := range pkgs {
covFile, err := ioutil.TempFile("", "")
if err != nil {
return err
}
covFile.Close()
defer os.Remove(covFile.Name())
files = append(files, covFile.Name())
cmd := exec.Command("go", "test",
"-covermode=atomic",
fmt.Sprintf("-coverprofile=%s", covFile.Name()),
fmt.Sprintf("-coverpkg=%s", strings.Join(pkgs, ",")),
pkg,
)
if err := cmd.Run(); err != nil {
return err
}
}
var merged []*cover.Profile
for _, file := range files {
profiles, err := cover.ParseProfiles(file)
if err != nil {
return fmt.Errorf("failed to parse profiles: %v", err)
}
for _, p := range profiles {
merged = gocovmerge.AddProfile(merged, p)
}
}
var out io.WriteCloser = os.Stdout
if len(args) > 0 {
var err error
out, err = os.Create(args[0])
if err != nil {
return err
}
}
defer out.Close()
return gocovmerge.DumpProfiles(merged, out)
}
func command(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append([]string{}, os.Environ()...)
return cmd
}
func goList() (pkgs []string) {
cmd := exec.Command("go", "list", "./...")
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
panic(err)
}
scanner := bufio.NewScanner(&stdout)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
pkgs = append(pkgs, line)
}
return pkgs
}