forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
161 lines (132 loc) · 4.09 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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// +build mage
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)
var (
// GoImportsImportPath controls the import path used to install goimports.
GoImportsImportPath = "golang.org/x/tools/cmd/goimports"
// GoImportsLocalPrefix is a string prefix matching imports that should be
// grouped after third-party packages.
GoImportsLocalPrefix = "github.com/elastic"
// GoLicenserImportPath controls the import path used to install go-licenser.
GoLicenserImportPath = "github.com/elastic/go-licenser"
buildDir = "./build"
storageRepoDir = filepath.Join(buildDir, "package-storage")
packagePaths = []string{filepath.Join(storageRepoDir, "packages"), "./testdata/package/"}
)
func Build() error {
err := FetchPackageStorage()
if err != nil {
return err
}
return sh.Run("go", "build", ".")
}
func FetchPackageStorage() error {
// Remove old storage directory
if err := os.RemoveAll(storageRepoDir); err != nil {
return errors.Wrapf(err, "failed to remove existing storage directory: %s", storageRepoDir)
}
packageStorageRevision := os.Getenv("PACKAGE_STORAGE_REVISION")
if packageStorageRevision == "" {
packageStorageRevision = "production"
}
// Check out fresh storage directory
return sh.Run("git", "clone", "--depth=1", "--single-branch",
"--branch", packageStorageRevision, "https://github.com/elastic/package-storage.git", storageRepoDir)
}
func Check() error {
Format()
// Setup the variables for the tests and not create tarGz files
packagePaths = []string{"testdata/package"}
err := Build()
if err != nil {
return err
}
err = ModTidy()
if err != nil {
return err
}
// Check if no changes are shown
err = sh.RunV("git", "update-index", "--refresh")
if err != nil {
return err
}
return sh.RunV("git", "diff-index", "--exit-code", "HEAD", "--")
}
func Test() error {
return sh.RunV("go", "test", "./...", "-v")
}
// Format adds license headers, formats .go files with goimports, and formats
// .py files with autopep8.
func Format() {
// Don't run AddLicenseHeaders and GoImports concurrently because they
// both can modify the same files.
mg.Deps(AddLicenseHeaders)
mg.Deps(GoImports)
}
// GoImports executes goimports against all .go files in and below the CWD. It
// ignores vendor/ directories.
func GoImports() error {
goFiles, err := FindFilesRecursive(func(path string, _ os.FileInfo) bool {
return filepath.Ext(path) == ".go" && !strings.Contains(path, "vendor/")
})
if err != nil {
return err
}
if len(goFiles) == 0 {
return nil
}
fmt.Println(">> fmt - goimports: Formatting Go code")
args := append(
[]string{"-local", GoImportsLocalPrefix, "-l", "-w"},
goFiles...,
)
return sh.RunV("goimports", args...)
}
// AddLicenseHeaders adds license headers to .go files. It applies the
// appropriate license header based on the value of mage.BeatLicense.
func AddLicenseHeaders() error {
fmt.Println(">> fmt - go-licenser: Adding missing headers")
return sh.RunV("go-licenser", "-license", "Elastic")
}
// FindFilesRecursive recursively traverses from the CWD and invokes the given
// match function on each regular file to determine if the given path should be
// returned as a match.
func FindFilesRecursive(match func(path string, info os.FileInfo) bool) ([]string, error) {
var matches []string
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
// continue
return nil
}
if match(filepath.ToSlash(path), info) {
matches = append(matches, path)
}
return nil
})
return matches, err
}
func Clean() error {
err := os.RemoveAll(buildDir)
if err != nil {
return err
}
return os.RemoveAll("package-registry")
}
// ModTidy cleans unused dependencies.
func ModTidy() error {
return sh.RunV("go", "mod", "tidy")
}