-
Notifications
You must be signed in to change notification settings - Fork 12
/
goxz.go
277 lines (254 loc) · 5.88 KB
/
goxz.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package goxz
import (
"context"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// Run the goxz
func Run(ctx context.Context, args []string, outStream, errStream io.Writer) error {
return (&cli{outStream: outStream, errStream: errStream}).run(args)
}
type goxz struct {
os, arch string
name, version string
dest string
include string
output string
buildLdFlags, buildTags, buildInstallSuffix string
zipAlways bool
pkgs []string
static bool
work bool
trimpath bool
platforms []*platform
projDir string
workDir string
resources []string
}
func (gx *goxz) run() error {
err := gx.init()
if err != nil {
return err
}
gx.workDir, err = os.MkdirTemp(gx.dest, ".goxz-")
if err != nil {
return err
}
defer func() {
if !gx.work {
os.RemoveAll(gx.workDir)
}
}()
if gx.work {
log.Printf("working dir: %s\n", gx.workDir)
}
wd, err := filepath.Abs(".")
if err != nil {
return err
}
if wd != gx.projDir {
if err := os.Chdir(gx.projDir); err != nil {
return err
}
defer os.Chdir(wd)
}
err = gx.buildAll()
if err == nil {
log.Println("Success!")
}
return err
}
func (gx *goxz) init() error {
log.Println("Initializing...")
if len(gx.pkgs) == 0 {
gx.pkgs = append(gx.pkgs, ".")
}
if len(gx.pkgs) > 1 && gx.output != "" {
return errors.New("When building multiple packages, output(`-o`) doesn't work")
}
if gx.projDir == "" {
var err error
gx.projDir, err = filepath.Abs(".")
if err != nil {
return err
}
} else if !filepath.IsAbs(gx.projDir) {
p, err := filepath.Abs(gx.projDir)
if err != nil {
return err
}
gx.projDir = p
}
if gx.name == "" {
gx.name = filepath.Base(gx.projDir)
}
if err := gx.initDest(); err != nil {
return err
}
err := os.MkdirAll(gx.dest, 0755)
if err != nil {
return err
}
// fill the defaults
if gx.os == "" {
gx.os = "linux darwin windows"
}
if gx.arch == "" {
gx.arch = "amd64 arm64"
}
gx.platforms, err = resolvePlatforms(gx.os, gx.arch)
if err != nil {
return err
}
gx.resources, err = gx.gatherResources()
if err != nil {
return err
}
rBaseNames := make([]string, len(gx.resources))
for i, r := range gx.resources {
rBaseNames[i], _ = filepath.Rel(gx.projDir, r)
}
log.Printf("Resources to include: [%s]\n", strings.Join(rBaseNames, " "))
return nil
}
var separateReg = regexp.MustCompile(`\s*(?:\s+|,)\s*`)
func resolvePlatforms(os, arch string) ([]*platform, error) {
platforms := []*platform{}
osTargets := separateReg.Split(os, -1)
archTargets := separateReg.Split(arch, -1)
for _, os := range osTargets {
if strings.TrimSpace(os) == "" {
continue
}
for _, arch := range archTargets {
if strings.TrimSpace(arch) == "" {
continue
}
platforms = append(platforms, &platform{os: os, arch: arch})
}
}
uniqPlatforms := []*platform{}
seen := make(map[string]struct{})
for _, pf := range platforms {
key := pf.os + ":" + pf.arch
_, ok := seen[key]
if !ok {
seen[key] = struct{}{}
uniqPlatforms = append(uniqPlatforms, pf)
}
}
return uniqPlatforms, nil
}
func (gx *goxz) initDest() error {
if gx.dest == "" {
gx.dest = "goxz"
}
if !filepath.IsAbs(gx.dest) {
var err error
gx.dest, err = filepath.Abs(gx.dest)
if err != nil {
return err
}
}
return nil
}
var (
resourceReg = regexp.MustCompile(`(?i)^(?:readme|licen[sc]e|credits?|install|changelog)(?:\.|$)`)
execExtReg = regexp.MustCompile(`(?i)\.(?:[a-z]*sh|p[ly]|rb|exe|go)$`)
)
func (gx *goxz) gatherResources() ([]string, error) {
dir := gx.projDir
var ret []string
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, f := range files {
if !f.Type().IsRegular() {
continue
}
n := f.Name()
if resourceReg.MatchString(n) && !execExtReg.MatchString(n) {
ret = append(ret, filepath.Join(dir, n))
}
}
if gx.include != "" {
for _, inc := range separateReg.Split(gx.include, -1) {
if !filepath.IsAbs(inc) {
inc = filepath.Join(dir, inc)
}
files, err := filepath.Glob(inc)
if err != nil {
return nil, err
}
for _, f := range files {
if !filepath.IsAbs(f) {
var err error
f, err = filepath.Abs(f)
if err != nil {
return nil, err
}
}
ret = append(ret, f)
}
}
}
seen := make(map[string]struct{})
ret2 := make([]string, 0, len(ret))
for _, p := range ret {
_, ok := seen[p]
if !ok {
seen[p] = struct{}{}
ret2 = append(ret2, p)
}
}
return ret2, nil
}
func (gx *goxz) buildAll() error {
eg := errgroup.Group{}
for _, bdr := range gx.builders() {
bdr := bdr
eg.Go(func() error {
archivePath, err := bdr.build()
if err != nil {
return err
}
installPath := filepath.Join(gx.dest, filepath.Base(archivePath))
err = os.Rename(archivePath, installPath)
if err != nil {
return err
}
log.Printf("Artifact archived to %s\n", installPath)
return nil
})
}
return eg.Wait()
}
func (gx *goxz) builders() []*builder {
builders := make([]*builder, len(gx.platforms))
for i, pf := range gx.platforms {
builders[i] = &builder{
platform: pf,
name: gx.name,
version: gx.version,
output: gx.output,
buildLdFlags: gx.buildLdFlags,
buildTags: gx.buildTags,
buildInstallSuffix: gx.buildInstallSuffix,
pkgs: gx.pkgs,
zipAlways: gx.zipAlways,
static: gx.static,
workDirBase: gx.workDir,
trimpath: gx.trimpath,
resources: gx.resources,
projDir: gx.projDir,
}
}
return builders
}