-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
244 lines (217 loc) · 6.87 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//go:build mage
// +build mage
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"github.com/atombender/go-jsonschema/pkg/generator"
_ "github.com/go-bindata/go-bindata" // generator
"github.com/go-bindata/go-bindata/v3"
"github.com/invopop/jsonschema"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/pkg/errors"
"github.com/simple-container-com/welder/pkg/magebuild"
"github.com/simple-container-com/welder/pkg/welder/types"
"golang.org/x/sync/errgroup"
/**/)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
type (
Build mg.Namespace
Tests mg.Namespace
Generate mg.Namespace
Git mg.Namespace
Publish mg.Namespace
)
var (
Ctx = magebuild.InitBuild()
Generators = map[string]magebuild.Generator{
"templates": nsGenerate.Templates,
"gofmt": nsGenerate.Formatting,
"jsonschema": nsGenerate.JsonSchema,
//"pipelines-schema": nsGenerate.PipelinesSchema, // TODO
}
)
var (
nsGenerate = Generate{}
nsBuild = Build{}
nsTests = Tests{}
nsGit = Git{}
nsPublish = Publish{}
)
// --------------------------------------
// Git targets
// CheckStatus Checks current status and fails if there are changes
func (Git) CheckStatus() error {
return Ctx.GitCheckWorkTree()
}
// --------------------------------------
// Test targets
// Test Runs all types of tests
func Test() error {
Ctx.JUnitOutputFile = "bin/test-results/TEST-junit-report-unit.xml"
return Ctx.Test(Ctx.TestTarget, fmt.Sprintf("-tags='%s'", Ctx.TestTags))
}
// --------------------------------------
// Build targets
// All Run everything: install deps, generate clients, run all builds for all platforms
func (b Build) All() error {
if err := b.Build(); err != nil {
return err
}
if err := nsGit.CheckStatus(); err != nil {
return err
}
return b.Bundles()
}
// Build Builds all plugins at once
func (b Build) Build() error {
if err := b.Deps(); err != nil {
return err
}
if err := nsGenerate.All(); err != nil {
return err
}
if Ctx.BuildViaWelder {
return Ctx.Welder().Build()
}
if err := Ctx.ForAllTargets(func(target magebuild.Target) error {
return Ctx.ForAllPlatforms(func(platform magebuild.Platform) error {
var extraArgs []string
if platform.GOOS != "windows" {
extraArgs = []string{"-tags=osusergo"}
}
if err := Ctx.Build(target, platform, extraArgs...); err != nil {
return errors.Wrapf(err, "failed to build %s: check bin/test-results for test report", target)
}
return nil
})
}); err != nil {
return err
}
return Test()
}
// Deps Cleans up output directory
func (Build) Deps() error {
fmt.Println("Installing dependencies...")
return Ctx.RunCmd(magebuild.Cmd{Command: "go mod download", Env: Ctx.CurrentPlatform().GoEnv()})
}
// Clean Cleans up output directory
func (Build) Clean() error {
fmt.Println("Cleaning...")
return os.RemoveAll(Ctx.OutDir)
}
// Bundles Build tarballs out of binaries
func (b Build) Bundles() error {
fmt.Println("Bundling tarballs...")
return Ctx.ForAllTargets(func(target magebuild.Target) error {
return Ctx.ForAllPlatforms(func(platform magebuild.Platform) error {
return Ctx.BuildBundle(Ctx.Bundle(target, platform))
})
})
}
// --------------------------------------
// Generators
// Invoke all generators and generate all necessary clients
func (Generate) All() error {
var eg errgroup.Group
for _, generator := range Generators {
g := generator
if Ctx.IsParallel() {
eg.Go(g)
} else if err := g(); err != nil {
fmt.Println(err.Error())
return errors.Wrapf(err, "Failed to execute generator")
}
}
return eg.Wait()
}
// Templates Generate templates.tpl.go file from /templates folder
func (Generate) Templates() error {
fmt.Println("Regenerating templates.tpl.go...")
templatesDir := Ctx.Path("templates")
if paths, err := Ctx.ListAllSubDirs(templatesDir); err != nil {
return err
} else {
fmt.Println(fmt.Sprintf("Generating for paths: %s", strings.Join(paths, ",")))
config := bindata.NewConfig()
config.Package = "rendered"
config.Prefix = fmt.Sprintf("%s/", templatesDir)
config.Output = Ctx.Path("pkg/render/rendered/templates.tpl.go")
config.Input = []bindata.InputConfig{}
config.NoMetadata = true
config.Ignore = []*regexp.Regexp{regexp.MustCompile("\\.DS_Store")}
for _, path := range paths {
config.Input = append(config.Input, bindata.InputConfig{Path: fmt.Sprintf("%s/%s", templatesDir, path)})
}
return bindata.Translate(config)
}
}
// Formatting runs gofmt
func (Generate) Formatting() error {
fmt.Println("Reformat code")
err := Ctx.RunCmd(magebuild.Cmd{Command: "go fmt $(go list ./... | grep -v render/rendered)", Env: Ctx.CurrentPlatform().GoEnv()})
if err != nil {
return err
}
return Ctx.RunCmd(magebuild.Cmd{Command: "go run mvdan.cc/gofumpt -l -w .", Env: Ctx.CurrentPlatform().GoEnv()})
}
// JsonSchema generate JSON schema to use within the IntelliJ IDEA plugin
func (Generate) JsonSchema() error {
schema := jsonschema.Reflect(&types.RootBuildDefinition{})
contBytes, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return errors.Wrapf(err, "failed to marshal JSONSchema")
}
if err := os.MkdirAll(Ctx.Path("bin"), os.ModePerm); err != nil {
return errors.Wrapf(err, "failed to create bin output directory")
}
path := Ctx.Path("bin/welder.schema.json")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return errors.Wrapf(err, "failed to open %s for writing", path)
}
_, err = f.WriteString(string(contBytes))
return err
}
// PipelinesSchema generate structures based on the bitbucket-pipelines.schema.json file
func (Generate) PipelinesSchema() error {
cfg := generator.Config{
Warner: func(message string) {
fmt.Println(fmt.Sprintf("Warning: %s", message))
},
DefaultPackageName: "schema",
DefaultOutputName: Ctx.Path("pkg/pipelines/schema/structs.go"),
SchemaMappings: []generator.SchemaMapping{
{RootType: "BitbucketPipelines", PackageName: "schema", SchemaID: "https://bitbucket.org/pipelines.json"},
},
}
gen, err := generator.New(cfg)
if err != nil {
return errors.Wrapf(err, "failed to create generator")
}
pipelinesJsonSchemaFile := Ctx.Path("pkg/pipelines/schema/bitbucket-pipelines.schema.json")
err = gen.DoFile(pipelinesJsonSchemaFile)
if err != nil {
return errors.Wrapf(err, "failed to read file %s", pipelinesJsonSchemaFile)
}
for path, contBytes := range gen.Sources() {
fmt.Println(fmt.Sprintf("Generating %s...", path))
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return errors.Wrapf(err, "failed to open %s for writing", path)
}
if err != nil {
return errors.Wrapf(err, "failed to create output file for pipelines schema")
}
_, err = f.WriteString(string(contBytes))
if err != nil {
return errors.Wrapf(err, "failed to write to file %s", path)
}
}
return nil
}