/
build.go
148 lines (121 loc) · 3.11 KB
/
build.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
package targets
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
"github.com/mholt/archiver/v4"
)
var verboseFlag = fmt.Sprintf("-v=%t", mg.Verbose())
type Build mg.Namespace
// Builds the opni binary and plugins
func (Build) All(ctx context.Context) {
ctx, tr := Tracer.Start(ctx, "target.build")
defer tr.End()
mg.CtxDeps(ctx, Build.Opni, Build.Plugins)
}
// Compiles all go packages except those named 'main'
func (Build) Archives(ctx context.Context) error {
_, tr := Tracer.Start(ctx, "target.build.archives")
defer tr.End()
return buildArchive("./...")
}
// Builds the custom linter plugin
func (Build) Linter(ctx context.Context) error {
_, tr := Tracer.Start(ctx, "target.build.linter")
defer tr.End()
return buildMainPackage(buildOpts{
Path: "./internal/cmd/lint",
Output: "bin/lint",
})
}
// Build the typescript service generator plugin
func (Build) TypescriptServiceGenerator() error {
if shouldGenerate, _ := target.Dir("web/service-generator/dist", "web/service-generator/src"); !shouldGenerate {
return nil
}
cwd, err := os.Getwd()
if err != nil {
return err
}
webPath := path.Join(cwd, "web")
command := exec.Command("yarn", "build:service-generator")
command.Dir = webPath
command.Stderr = os.Stderr
command.Stdout = os.Stdout
command.Stdin = os.Stdin
return command.Run()
}
type buildOpts struct {
Path string
Output string
Tags []string
Debug bool
Compress bool
}
func buildMainPackage(opts buildOpts) error {
tag, _ := os.LookupEnv("BUILD_VERSION")
tag = strings.TrimSpace(tag)
version := "unversioned"
if tag != "" {
version = tag
}
args := []string{
"go", "build", "-v",
}
if !opts.Debug {
args = append(args,
"-ldflags", fmt.Sprintf("-w -s -X github.com/rancher/opni/pkg/versions.Version=%s", version),
"-trimpath",
)
}
args = append(args, "-o", opts.Output)
if len(opts.Tags) > 0 {
args = append(args, fmt.Sprintf("-tags=%s", strings.Join(opts.Tags, ",")))
}
// disable vcs stamping inside git worktrees if the linked git directory doesn't exist
dotGit, err := os.Stat(".git")
if err == nil && !dotGit.IsDir() {
fmt.Println("disabling vcs stamping inside worktree")
args = append(args, "-buildvcs=false")
}
args = append(args, opts.Path)
err = sh.RunWith(map[string]string{"CGO_ENABLED": "0"}, args[0], args[1:]...)
if err != nil {
return err
}
if !opts.Compress {
return nil
}
files, err := archiver.FilesFromDisk(nil, map[string]string{
opts.Output: "opni",
})
if err != nil {
return err
}
out, err := os.Create(opts.Output + ".tar.gz")
if err != nil {
return err
}
defer out.Close()
format := archiver.CompressedArchive{
Compression: archiver.Gz{},
Archival: archiver.Tar{},
}
// create the archive
err = format.Archive(context.Background(), out, files)
if err != nil {
return err
}
return err
}
func buildArchive(path string) error {
return sh.RunWith(map[string]string{
"CGO_ENABLED": "0",
}, mg.GoCmd(), "build", verboseFlag, "-buildmode=archive", "-trimpath", "-tags=nomsgpack", path)
}