This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
goreleaser.go
313 lines (282 loc) · 7.46 KB
/
goreleaser.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"bufio"
"log"
"os"
"os/exec"
"reflect"
"strings"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type GoReleaserConfig struct {
Path string
Project config.Project
}
func getConfig(cli Cli, target Target, compilers Compilers) (grc GoReleaserConfig, _ error) {
var cfg config.Project
var err error
if len(cli.Config) > 0 {
b, err := os.ReadFile(cli.Config)
if err != nil {
return grc, err
}
if err := yaml.Unmarshal(b, &cfg); err != nil {
return grc, err
}
}
if len(cfg.Dist) > 0 {
log.Printf("WARN: dist specified in your config file is overrided")
}
cfg.Dist, err = os.MkdirTemp(os.TempDir(), "dist")
if err != nil {
return grc, err
}
if len(cli.Name) > 0 {
cfg.ProjectName = cli.Name
}
var build config.Build
if len(cfg.Builds) == 1 {
build = cfg.Builds[0]
} else if len(cfg.Builds) > 1 {
return grc, errors.New("multiple builds found, please specify one")
}
cgoEnabled := false
if ok := hasCgoEnabled(os.Environ()); ok {
cgoEnabled = true
}
if ok := hasCgoEnabled(cli.Envs); ok {
cgoEnabled = true
}
if ok := hasCgoEnabled(build.Env); ok {
cgoEnabled = true
}
if cgoEnabled {
if len(compilers.Ar) > 0 {
if _, err = exec.LookPath(compilers.Ar); err == nil {
cfg.Env = append(cfg.Env, "AR="+compilers.Ar)
}
}
if len(compilers.Cc) > 0 {
if _, err = exec.LookPath(compilers.Cc); err == nil {
cfg.Env = append(cfg.Env, "CC="+compilers.Cc)
}
}
if len(compilers.Cxx) > 0 {
if _, err = exec.LookPath(compilers.Cxx); err == nil {
cfg.Env = append(cfg.Env, "CXX="+compilers.Cxx)
}
}
if len(compilers.PkgConfig) > 0 {
if _, err = exec.LookPath(compilers.PkgConfig); err == nil {
cfg.Env = append(cfg.Env, "PKG_CONFIG="+compilers.PkgConfig)
}
}
if len(compilers.CgoCflags) > 0 {
cfg.Env = append(cfg.Env, "CGO_CFLAGS="+compilers.CgoCflags)
}
if len(compilers.CgoCppflags) > 0 {
cfg.Env = append(cfg.Env, "CGO_CPPFLAGS="+compilers.CgoCppflags)
}
if len(compilers.CgoCxxflags) > 0 {
cfg.Env = append(cfg.Env, "CGO_CXXFLAGS="+compilers.CgoCxxflags)
}
if len(compilers.CgoFflags) > 0 {
cfg.Env = append(cfg.Env, "CGO_FFLAGS="+compilers.CgoFflags)
}
if len(compilers.CgoLdflags) > 0 {
cfg.Env = append(cfg.Env, "CGO_LDFLAGS="+compilers.CgoLdflags)
}
}
if len(cli.Envs) > 0 {
cfg.Env = append(cfg.Env, cli.Envs...)
}
if len(build.Env) > 0 {
cfg.Env = append(cfg.Env, build.Env...)
build.Env = nil
}
if len(build.Goos) > 0 {
log.Printf("WARN: goos specified in your config file is overrided")
}
if len(build.Goarch) > 0 {
log.Printf("WARN: goarch specified in your config file is overrided")
}
if len(build.Goarm) > 0 {
log.Printf("WARN: goarm specified in your config file is overrided")
}
if len(build.Gomips) > 0 {
log.Printf("WARN: gomips specified in your config file is overrided")
}
if len(build.Goamd64) > 0 {
log.Printf("WARN: goarm specified in your config file is overrided")
}
build.Goos = []string{target.Os}
build.Goarch = []string{target.Arch}
if len(target.Arch) > 0 {
build.Goarm = []string{target.Arm}
}
if len(target.Amd64) > 0 {
build.Goamd64 = []string{target.Amd64}
}
if strings.HasPrefix(target.Arch, "mips64") {
build.Gomips = []string{target.Mips64}
} else if strings.HasPrefix(target.Arch, "mips") {
build.Gomips = []string{target.Mips}
}
if len(cli.Main) > 0 {
build.Main = cli.Main
}
if len(cli.Flags) > 0 {
build.Flags = append(build.Flags, cli.Flags)
}
if len(cli.Asmflags) > 0 {
build.Asmflags = append(build.Asmflags, cli.Asmflags)
}
if len(cli.Gcflags) > 0 {
build.Gcflags = append(build.Gcflags, cli.Gcflags)
}
if len(cli.Ldflags) > 0 {
build.Ldflags = append(build.Ldflags, cli.Ldflags)
}
if len(cli.Tags) > 0 {
build.Tags = append(build.Tags, cli.Tags)
}
if len(cli.GoBinary) > 0 {
build.GoBinary = cli.GoBinary
}
if len(cfg.Before.Hooks) > 0 {
for _, cmd := range cfg.Before.Hooks {
build.Hooks.Pre = append(build.Hooks.Pre, config.Hook{
Cmd: cmd,
})
}
cfg.Before.Hooks = nil
}
for _, cmd := range cli.PreHooks {
build.Hooks.Pre = append(build.Hooks.Pre, config.Hook{
Cmd: cmd,
})
}
build.Hooks.Post = append(build.Hooks.Post, config.Hook{
Cmd: `cp "{{ .Path }}" "/usr/local/bin/{{ .ProjectName }}{{ .Ext }}"`,
})
for _, cmd := range cli.PostHooks {
build.Hooks.Post = append(build.Hooks.Post, config.Hook{
Cmd: cmd,
})
}
archive := config.Archive{ID: target.Pair}
if len(cfg.Archives) == 1 {
archive = cfg.Archives[0]
} else if len(cfg.Archives) > 1 {
return grc, errors.New("multiple archives found, please specify one")
}
for _, f := range cli.Files {
archive.Files = append(archive.Files, config.File{
Source: f,
})
}
if archive.Replacements == nil {
archive.Replacements = make(map[string]string)
}
for k, v := range cli.Replacements {
archive.Replacements[k] = v
}
winOverride := false
for _, o := range archive.FormatOverrides {
if o.Goos == "windows" {
winOverride = true
break
}
}
if !winOverride {
archive.FormatOverrides = append(archive.FormatOverrides, config.FormatOverride{
Goos: "windows",
Format: "zip",
})
}
cfg.Builds = []config.Build{build}
cfg.Archives = []config.Archive{archive}
if !reflect.DeepEqual(cfg.Checksum, config.Checksum{}) {
log.Printf("WARN: checksum section specified in your config file is disabled")
}
cfg.Checksum = config.Checksum{Disable: true}
if !reflect.DeepEqual(cfg.Release, config.Release{}) {
log.Printf("WARN: release section specified in your config file disabled")
}
cfg.Release = config.Release{Disable: true}
if !reflect.DeepEqual(cfg.Changelog, config.Changelog{}) {
log.Printf("WARN: changelog section specified in your config file skipped")
}
cfg.Changelog = config.Changelog{Skip: true}
if len(cfg.NFPMs) > 0 && !allowNfpms(target) {
log.Printf("WARN: nfpms section specified in your config file disabled for %s", formatTarget(target))
cfg.NFPMs = nil
}
if len(cfg.Brews) > 0 && !allowBrews(target) {
log.Printf("WARN: brews section specified in your config file disabled for %s", formatTarget(target))
cfg.Brews = nil
}
if len(cfg.Snapcrafts) > 0 && !allowSnaps(target) {
log.Printf("WARN: snapcrafts section specified in your config file disabled for %s", formatTarget(target))
cfg.Snapcrafts = nil
}
b, err := yaml.Marshal(cfg)
if err != nil {
return grc, err
}
f, err := os.CreateTemp(os.TempDir(), ".goreleaser.yml")
if err != nil {
return grc, err
}
defer f.Close()
if err := os.WriteFile(f.Name(), b, 0644); err != nil {
defer os.Remove(f.Name())
return grc, err
}
if cli.Debug {
log.Println("DBG: goreleaser config:")
scanner := bufio.NewScanner(strings.NewReader(string(b)))
for scanner.Scan() {
log.Printf(" %s", scanner.Text())
}
}
return GoReleaserConfig{
Path: f.Name(),
Project: cfg,
}, nil
}
func allowNfpms(target Target) bool {
return target.Os == "linux"
}
func allowBrews(target Target) bool {
if target.Os != "linux" && target.Os != "darwin" {
return false
}
for _, a := range []string{"arm64", "arm", "amd64"} {
if target.Arch == a {
return true
}
}
return false
}
func allowSnaps(target Target) bool {
if target.Os != "linux" {
return false
}
for _, a := range []string{"s390x", "ppc64le", "arm64", "arm", "amd64", "386"} {
if target.Arch == a {
return true
}
}
return false
}
func hasCgoEnabled(envs []string) bool {
for _, e := range envs {
if e == "CGO_ENABLED=1" {
return true
}
}
return false
}