1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/brew/brew.go

329 lines
8.6 KiB
Go
Raw Normal View History

2016-12-29 13:58:22 +02:00
package brew
import (
"bufio"
2016-12-29 13:58:22 +02:00
"bytes"
"errors"
"fmt"
2018-01-10 01:31:18 +02:00
"io/ioutil"
2018-11-07 18:15:07 +02:00
"path"
2017-01-14 15:18:48 +02:00
"path/filepath"
2016-12-29 13:58:22 +02:00
"strings"
2016-12-29 17:14:52 +02:00
"text/template"
2016-12-29 13:58:22 +02:00
2017-06-22 05:09:14 +02:00
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
2017-05-13 23:06:15 +02:00
"github.com/goreleaser/goreleaser/internal/client"
2018-09-12 19:18:01 +02:00
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2016-12-29 13:58:22 +02:00
)
// ErrNoArchivesFound happens when 0 archives are found.
var ErrNoArchivesFound = errors.New("no linux/macos archives found")
2017-12-17 20:31:06 +02:00
// 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.
2016-12-30 13:27:35 +02:00
type Pipe struct{}
func (Pipe) String() string {
2018-11-03 20:25:01 +02:00
return "homebrew tap formula"
2016-12-30 13:27:35 +02:00
}
// Publish brew formula.
2018-10-10 17:47:31 +02:00
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)
2017-09-22 14:42:36 +02:00
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()
2017-03-26 20:30:21 +02:00
}
// 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 == "" {
brew.Install = fmt.Sprintf(`bin.install "%s"`, ctx.Config.ProjectName)
log.Warnf("optimistically guessing `brew[%d].install` to be `%s`", i, brew.Install)
}
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 doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
if brew.Tap.Name == "" {
2018-09-12 19:18:01 +02:00
return pipe.Skip("brew section is not configured")
2016-12-30 13:27:35 +02:00
}
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
}
2019-09-09 14:42:11 +02:00
// TODO: properly cover this with tests
var filters = []artifact.Filter{
artifact.Or(
2017-12-17 20:59:54 +02:00
artifact.ByGoos("darwin"),
artifact.ByGoos("linux"),
2017-12-17 20:59:54 +02:00
),
artifact.ByFormats("zip", "tar.gz"),
2019-09-09 14:34:52 +02:00
artifact.Or(
artifact.ByGoarch("amd64"),
artifact.ByGoarch("arm64"),
artifact.And(
artifact.ByGoarch("arm"),
artifact.ByGoarm(brew.Goarm),
),
2019-09-09 14:34:52 +02:00
),
artifact.ByType(artifact.UploadableArchive),
2017-07-14 01:22:10 +02:00
}
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
2017-07-14 01:22:10 +02:00
}
2018-01-10 01:31:18 +02:00
name, err := tmpl.New(ctx).Apply(brew.Name)
if err != nil {
return err
}
brew.Name = name
content, err := buildFormula(ctx, brew, cl, archives)
2016-12-29 13:58:22 +02:00
if err != nil {
return err
}
2018-01-10 01:31:18 +02:00
var filename = brew.Name + ".rb"
2018-01-10 01:31:18 +02:00
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)
2018-01-10 01:31:18 +02:00
}
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")
}
2018-01-10 23:22:37 +02:00
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)
2018-11-06 14:46:00 +02:00
}
func buildFormulaPath(folder, filename string) string {
2018-11-07 18:15:07 +02:00
return path.Join(folder, filename)
2016-12-30 13:48:06 +02:00
}
2016-12-29 13:58:22 +02:00
func buildFormula(ctx *context.Context, brew config.Homebrew, client client.Client, artifacts []*artifact.Artifact) (string, error) {
data, err := dataFor(ctx, brew, client, artifacts)
2016-12-30 13:48:06 +02:00
if err != nil {
return "", err
2016-12-30 13:48:06 +02:00
}
return doBuildFormula(ctx, data)
2016-12-30 13:53:05 +02:00
}
func doBuildFormula(ctx *context.Context, data templateData) (string, error) {
t, err := template.New(data.Name).Parse(formulaTemplate)
2016-12-29 17:14:52 +02:00
if err != nil {
return "", err
2016-12-29 17:14:52 +02:00
}
var out bytes.Buffer
if err := t.Execute(&out, data); err != nil {
return "", err
}
content, err := tmpl.New(ctx).Apply(out.String())
if err != nil {
return "", err
}
out.Reset()
// Sanitize the template output and get rid of trailing whitespace.
var (
r = strings.NewReader(content)
s = bufio.NewScanner(r)
)
for s.Scan() {
l := strings.TrimRight(s.Text(), " ")
_, _ = out.WriteString(l)
_ = out.WriteByte('\n')
}
if err := s.Err(); err != nil {
return "", err
}
return out.String(), nil
2016-12-29 17:14:52 +02:00
}
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
switch artifact.Goarch {
case "amd64":
if result.MacOSAmd64.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.MacOSAmd64 = down
case "arm64":
if result.MacOSArm64.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.MacOSArm64 = 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
2016-12-29 13:58:22 +02:00
}
2016-12-29 14:55:35 +02:00
2017-07-16 21:01:20 +02:00
func split(s string) []string {
strings := strings.Split(strings.TrimSpace(s), "\n")
if len(strings) == 1 && strings[0] == "" {
return []string{}
}
return strings
2017-07-16 21:01:20 +02:00
}
2016-12-29 14:55:35 +02:00
func formulaNameFor(name string) string {
name = strings.ReplaceAll(name, "-", " ")
name = strings.ReplaceAll(name, "_", " ")
name = strings.ReplaceAll(name, "@", "AT")
return strings.ReplaceAll(strings.Title(name), " ", "")
2016-12-29 17:14:52 +02:00
}