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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// +build mage
package main
import (
"bytes"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
packageName = "github.com/gohugoio/hugo"
noGitLdflags = "-X $PACKAGE/hugolib.BuildDate=$BUILD_DATE"
)
var ldflags = "-X $PACKAGE/hugolib.CommitHash=$COMMIT_HASH -X $PACKAGE/hugolib.BuildDate=$BUILD_DATE"
// allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems
var goexe = "go"
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
}
func govendor() error {
return sh.Run(goexe, "get", "github.com/kardianos/govendor")
}
// Install govendor and sync Hugo's vendored dependencies
func Vendor() error {
mg.Deps(govendor)
return sh.Run("govendor", "sync", packageName)
}
// Build hugo binary
func Hugo() error {
mg.Deps(Vendor)
return sh.RunWith(flagEnv(), goexe, "build", "-ldflags", ldflags, packageName)
}
// Build hugo binary with race detector enabled
func HugoRace() error {
mg.Deps(Vendor)
return sh.RunWith(flagEnv(), goexe, "build", "-race", "-ldflags", ldflags, packageName)
}
// Install hugo binary
func Install() error {
mg.Deps(Vendor)
return sh.RunWith(flagEnv(), goexe, "install", "-ldflags", ldflags, packageName)
}
func flagEnv() map[string]string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return map[string]string{
"PACKAGE": packageName,
"COMMIT_HASH": hash,
"BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"),
}
}
// Build hugo without git info
func HugoNoGitInfo() {
ldflags = noGitLdflags
mg.Deps(Vendor, Hugo)
}
var docker = sh.RunCmd("docker")
// Build hugo Docker container
func Docker() error {
if err := docker("build", "-t", "hugo", "."); err != nil {
return err
}
// yes ignore errors here
docker("rm", "-f", "hugo-build")
if err := docker("run", "--name", "hugo-build", "hugo ls /go/bin"); err != nil {
return err
}
if err := docker("cp", "hugo-build:/go/bin/hugo", "."); err != nil {
return err
}
return docker("rm", "hugo-build")
}
// Run tests and linters
func Check() {
mg.Deps(TestRace, Test386, Fmt, Vet)
}
// Run tests in 32-bit mode
func Test386() error {
return sh.RunWith(map[string]string{"GOARCH": "386"}, "govendor", "test", "+local")
}
// Run tests
func Test() error {
mg.Deps(govendor)
return sh.Run("govendor", "test", "+local")
}
// Run tests with race detector
func TestRace() error {
mg.Deps(govendor)
return sh.Run("govendor", "test", "-race", "+local")
}
// Run gofmt linter
func Fmt() error {
pkgs, err := hugoPackages()
if err != nil {
return err
}
failed := false
for _, pkg := range pkgs {
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
if err != nil {
return nil
}
for _, f := range files {
if err := sh.Run("gofmt", "-l", f); err != nil {
failed = false
}
}
}
if failed {
return errors.New("improperly formatted go files")
}
return nil
}
var pkgPrefixLen = len("github.com/gohugoio/hugo")
func hugoPackages() ([]string, error) {
mg.Deps(govendor)
s, err := sh.Output("govendor", "list", "-no-status", "+local")
if err != nil {
return nil, err
}
pkgs := strings.Split(s, "\n")
for i := range pkgs {
pkgs[i] = "." + pkgs[i][pkgPrefixLen:]
}
return pkgs, nil
}
// Run golint linter
func Lint() error {
pkgs, err := hugoPackages()
if err != nil {
return err
}
failed := false
for _, pkg := range pkgs {
if _, err := sh.Exec(nil, os.Stderr, os.Stderr, "golint", "-set_exit_status", pkg); err != nil {
failed = true
}
}
if failed {
return errors.New("golint errors!")
}
return nil
}
// Run go vet linter
func Vet() error {
mg.Deps(govendor)
if err := sh.Run("govendor", "vet", "+local"); err != nil {
return errors.New("go vet errors!")
}
return nil
}
// Generate test coverage report
func TestCoverHTML() error {
mg.Deps(govendor)
const (
coverAll = "coverage-all.out"
cover = "coverage.out"
)
f, err := os.Create(coverAll)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write([]byte("mode: count")); err != nil {
return err
}
pkgs, err := hugoPackages()
if err != nil {
return err
}
for _, pkg := range pkgs {
if err := sh.Run("govendor", "test", "-coverprofile="+cover, "-covermode=count", pkg); err != nil {
return err
}
b, err := ioutil.ReadFile(cover)
if err != nil {
return err
}
idx := bytes.Index(b, []byte{'\n'})
b = b[idx+1:]
if _, err := f.Write(b); err != nil {
return err
}
}
if err := f.Close(); err != nil {
return err
}
return sh.Run(goexe, "tool"
|