forked from goreleaser/goreleaser
/
brew.go
349 lines (312 loc) · 9.25 KB
/
brew.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package brew
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
// ErrNoArchivesFound happens when 0 archives are found.
var ErrNoArchivesFound = errors.New("no linux/macos archives found")
// ErrMultipleArchivesSameOS happens when the config yields multiple archives
// for linux or windows.
var ErrMultipleArchivesSameOS = errors.New("one tap can handle only archive of an OS/Arch combination. Consider using ids in the brew section")
// ErrTokenTypeNotImplementedForBrew indicates that a new token type was not implemented for this pipe.
type ErrTokenTypeNotImplementedForBrew struct {
TokenType context.TokenType
}
func (e ErrTokenTypeNotImplementedForBrew) Error() string {
if e.TokenType != "" {
return fmt.Sprintf("token type %q not implemented for brew pipe", e.TokenType)
}
return "token type not implemented for brew pipe"
}
// Pipe for brew deployment.
type Pipe struct{}
func (Pipe) String() string {
return "homebrew tap formula"
}
// Publish brew formula.
func (Pipe) Publish(ctx *context.Context) error {
// we keep GitHub as default for now, in line with releases
if string(ctx.TokenType) == "" {
ctx.TokenType = context.TokenTypeGitHub
}
cli, err := client.New(ctx)
if err != nil {
return err
}
return publishAll(ctx, cli)
}
func publishAll(ctx *context.Context, cli client.Client) error {
// even if one of them skips, we run them all, and then show return the skips all at once.
// this is needed so we actually create the `dist/foo.rb` file, which is useful for debugging.
var skips = pipe.SkipMemento{}
for _, brew := range ctx.Config.Brews {
var err = doRun(ctx, brew, cli)
if err != nil && pipe.IsSkip(err) {
skips.Remember(err)
continue
}
if err != nil {
return err
}
}
return skips.Evaluate()
}
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
for i := range ctx.Config.Brews {
var brew = &ctx.Config.Brews[i]
if brew.Install == "" {
// TODO: maybe replace this with a simpler also optimistic
// approach of just doing `bin.install "project_name"`?
var installs []string
for _, build := range ctx.Config.Builds {
if !isBrewBuild(build) {
continue
}
install := fmt.Sprintf(`bin.install "%s"`, build.Binary)
// Do not add duplicate "bin.install" statements when binary names overlap.
var found bool
for _, instruction := range installs {
if instruction == install {
found = true
break
}
}
if !found {
installs = append(installs, install)
}
}
brew.Install = strings.Join(installs, "\n")
log.Warnf("optimistically guessing `brew[%d].install`, double check", i)
}
//nolint: staticcheck
if brew.GitHub.String() != "" {
deprecate.Notice(ctx, "brews.github")
brew.Tap.Owner = brew.GitHub.Owner
brew.Tap.Name = brew.GitHub.Name
}
if brew.GitLab.String() != "" {
deprecate.Notice(ctx, "brews.gitlab")
brew.Tap.Owner = brew.GitLab.Owner
brew.Tap.Name = brew.GitLab.Name
}
if brew.CommitAuthor.Name == "" {
brew.CommitAuthor.Name = "goreleaserbot"
}
if brew.CommitAuthor.Email == "" {
brew.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}
if brew.Name == "" {
brew.Name = ctx.Config.ProjectName
}
if brew.Goarm == "" {
brew.Goarm = "6"
}
}
return nil
}
func isBrewBuild(build config.Build) bool {
for _, ignore := range build.Ignore {
if ignore.Goos == "darwin" && ignore.Goarch == "amd64" {
return false
}
}
return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64")
}
func contains(ss []string, s string) bool {
for _, zs := range ss {
if zs == s {
return true
}
}
return false
}
func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
if brew.Tap.Name == "" {
return pipe.Skip("brew section is not configured")
}
if brew.Tap.Token != "" {
token, err := tmpl.New(ctx).ApplySingleEnvOnly(brew.Tap.Token)
if err != nil {
return err
}
log.Debug("using custom token to publish homebrew formula")
c, err := client.NewWithToken(ctx, token)
if err != nil {
return err
}
cl = c
}
// TODO: properly cover this with tests
var filters = []artifact.Filter{
artifact.Or(
artifact.ByGoos("darwin"),
artifact.ByGoos("linux"),
),
artifact.ByFormats("zip", "tar.gz"),
artifact.Or(
artifact.ByGoarch("amd64"),
artifact.ByGoarch("arm64"),
artifact.And(
artifact.ByGoarch("arm"),
artifact.ByGoarm(brew.Goarm),
),
),
artifact.ByType(artifact.UploadableArchive),
}
if len(brew.IDs) > 0 {
filters = append(filters, artifact.ByIDs(brew.IDs...))
}
var archives = ctx.Artifacts.Filter(artifact.And(filters...)).List()
if len(archives) == 0 {
return ErrNoArchivesFound
}
name, err := tmpl.New(ctx).Apply(brew.Name)
if err != nil {
return err
}
brew.Name = name
content, err := buildFormula(ctx, brew, cl, archives)
if err != nil {
return err
}
var filename = brew.Name + ".rb"
var path = filepath.Join(ctx.Config.Dist, filename)
log.WithField("formula", path).Info("writing")
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil { //nolint: gosec
return fmt.Errorf("failed to write brew formula: %w", err)
}
if strings.TrimSpace(brew.SkipUpload) == "true" {
return pipe.Skip("brew.skip_upload is set")
}
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
if strings.TrimSpace(brew.SkipUpload) == "auto" && ctx.Semver.Prerelease != "" {
return pipe.Skip("prerelease detected with 'auto' upload, skipping homebrew publish")
}
repo := client.RepoFromRef(brew.Tap)
var gpath = buildFormulaPath(brew.Folder, filename)
log.WithField("formula", gpath).
WithField("repo", repo.String()).
Info("pushing")
var msg = fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
return cl.CreateFile(ctx, brew.CommitAuthor, repo, []byte(content), gpath, msg)
}
func buildFormulaPath(folder, filename string) string {
return path.Join(folder, filename)
}
func buildFormula(ctx *context.Context, brew config.Homebrew, client client.Client, artifacts []*artifact.Artifact) (string, error) {
data, err := dataFor(ctx, brew, client, artifacts)
if err != nil {
return "", err
}
return doBuildFormula(ctx, data)
}
func doBuildFormula(ctx *context.Context, data templateData) (string, error) {
t, err := template.New(data.Name).Parse(formulaTemplate)
if err != nil {
return "", err
}
var out bytes.Buffer
if err := t.Execute(&out, data); err != nil {
return "", err
}
return tmpl.New(ctx).Apply(out.String())
}
func dataFor(ctx *context.Context, cfg config.Homebrew, cl client.Client, artifacts []*artifact.Artifact) (templateData, error) {
var result = templateData{
Name: formulaNameFor(cfg.Name),
Desc: cfg.Description,
Homepage: cfg.Homepage,
Version: ctx.Version,
License: cfg.License,
Caveats: split(cfg.Caveats),
Dependencies: cfg.Dependencies,
Conflicts: cfg.Conflicts,
Plist: cfg.Plist,
Install: split(cfg.Install),
PostInstall: cfg.PostInstall,
Tests: split(cfg.Test),
DownloadStrategy: cfg.DownloadStrategy,
CustomRequire: cfg.CustomRequire,
CustomBlock: split(cfg.CustomBlock),
}
for _, artifact := range artifacts {
sum, err := artifact.Checksum("sha256")
if err != nil {
return result, err
}
if cfg.URLTemplate == "" {
url, err := cl.ReleaseURLTemplate(ctx)
if err != nil {
if client.IsNotImplementedErr(err) {
return result, ErrTokenTypeNotImplementedForBrew{ctx.TokenType}
}
return result, err
}
cfg.URLTemplate = url
}
url, err := tmpl.New(ctx).WithArtifact(artifact, map[string]string{}).Apply(cfg.URLTemplate)
if err != nil {
return result, err
}
var down = downloadable{
DownloadURL: url,
SHA256: sum,
}
// TODO: refactor
if artifact.Goos == "darwin" { // nolint: nestif
if result.MacOS.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.MacOS = down
} else if artifact.Goos == "linux" {
switch artifact.Goarch {
case "amd64":
if result.LinuxAmd64.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.LinuxAmd64 = down
case "arm":
if result.LinuxArm.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.LinuxArm = down
case "arm64":
if result.LinuxArm64.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.LinuxArm64 = down
}
}
}
return result, nil
}
func split(s string) []string {
strings := strings.Split(strings.TrimSpace(s), "\n")
if len(strings) == 1 && strings[0] == "" {
return []string{}
}
return strings
}
func formulaNameFor(name string) string {
name = strings.ReplaceAll(name, "-", " ")
name = strings.ReplaceAll(name, "_", " ")
name = strings.ReplaceAll(name, "@", "AT")
return strings.ReplaceAll(strings.Title(name), " ", "")
}