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

200 lines
5.1 KiB
Go
Raw Normal View History

2016-12-29 13:58:22 +02:00
package brew
import (
"bytes"
"errors"
"fmt"
2018-01-10 01:31:18 +02:00
"io/ioutil"
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"
2017-12-17 20:31:06 +02:00
2017-07-14 01:22:10 +02:00
"github.com/goreleaser/goreleaser/checksum"
"github.com/goreleaser/goreleaser/config"
2017-01-15 00:01:32 +02:00
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
2017-05-13 23:06:15 +02:00
"github.com/goreleaser/goreleaser/internal/client"
2017-08-20 21:35:46 +02:00
"github.com/goreleaser/goreleaser/pipeline"
2016-12-29 13:58:22 +02:00
)
2017-12-17 20:31:06 +02:00
// ErrNoDarwin64Build when there is no build for darwin_amd64
var ErrNoDarwin64Build = errors.New("brew tap requires one darwin amd64 build")
// ErrTooManyDarwin64Builds when there are too many builds for darwin_amd64
var ErrTooManyDarwin64Builds = errors.New("brew tap requires at most one darwin amd64 build")
2016-12-30 16:41:59 +02:00
// Pipe for brew deployment
2016-12-30 13:27:35 +02:00
type Pipe struct{}
func (Pipe) String() string {
return "creating homebrew formula"
2016-12-30 13:27:35 +02:00
}
2016-12-30 16:41:59 +02:00
// Run the pipe
2017-01-14 18:06:57 +02:00
func (Pipe) Run(ctx *context.Context) error {
2017-09-26 23:52:37 +02:00
client, err := client.NewGitHub(ctx)
2017-09-22 14:42:36 +02:00
if err != nil {
return err
}
return doRun(ctx, client)
2017-03-26 20:30:21 +02:00
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Brew.Install == "" {
var installs []string
for _, build := range ctx.Config.Builds {
if !isBrewBuild(build) {
continue
}
installs = append(
installs,
fmt.Sprintf(`bin.install "%s"`, build.Binary),
)
}
ctx.Config.Brew.Install = strings.Join(installs, "\n")
}
if ctx.Config.Brew.CommitAuthor.Name == "" {
ctx.Config.Brew.CommitAuthor.Name = "goreleaserbot"
}
if ctx.Config.Brew.CommitAuthor.Email == "" {
ctx.Config.Brew.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}
2018-03-08 00:21:01 +02:00
if ctx.Config.Brew.Name == "" {
ctx.Config.Brew.Name = ctx.Config.ProjectName
}
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
}
2017-04-14 21:07:27 +02:00
func doRun(ctx *context.Context, client client.Client) error {
if ctx.Config.Brew.GitHub.Name == "" {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("brew section is not configured")
2016-12-30 13:27:35 +02:00
}
2017-07-16 20:23:29 +02:00
if ctx.Config.Archive.Format == "binary" {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("archive format is binary")
}
2017-07-16 20:23:29 +02:00
2017-12-17 20:31:06 +02:00
var archives = ctx.Artifacts.Filter(
2017-12-17 20:59:54 +02:00
artifact.And(
artifact.ByGoos("darwin"),
artifact.ByGoarch("amd64"),
artifact.ByGoarm(""),
2017-12-17 20:59:54 +02:00
artifact.ByType(artifact.UploadableArchive),
),
2017-12-17 20:31:06 +02:00
).List()
if len(archives) == 0 {
2017-07-14 01:22:10 +02:00
return ErrNoDarwin64Build
}
if len(archives) > 1 {
2017-12-17 20:31:06 +02:00
return ErrTooManyDarwin64Builds
2017-07-14 01:22:10 +02:00
}
2018-01-10 01:31:18 +02:00
2018-07-04 09:42:24 +02:00
content, err := buildFormula(ctx, archives[0])
2016-12-29 13:58:22 +02:00
if err != nil {
return err
}
2018-01-10 01:31:18 +02:00
2018-03-08 00:21:01 +02:00
var filename = ctx.Config.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, content.Bytes(), 0644); err != nil {
return err
}
2018-01-10 23:22:37 +02:00
if ctx.Config.Brew.SkipUpload {
return pipeline.Skip("brew.skip_upload is set")
}
if ctx.SkipPublish {
return pipeline.ErrSkipPublishEnabled
2018-01-10 23:22:37 +02:00
}
if ctx.Config.Release.Draft {
return pipeline.Skip("release is marked as draft")
}
2018-01-10 01:31:18 +02:00
path = filepath.Join(ctx.Config.Brew.Folder, filename)
log.WithField("formula", path).
WithField("repo", ctx.Config.Brew.GitHub.String()).
Info("pushing")
var msg = fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
return client.CreateFile(ctx, ctx.Config.Brew.CommitAuthor, ctx.Config.Brew.GitHub, content, path, msg)
2016-12-30 13:48:06 +02:00
}
2016-12-29 13:58:22 +02:00
2018-07-04 09:42:24 +02:00
func buildFormula(ctx *context.Context, artifact artifact.Artifact) (bytes.Buffer, error) {
data, err := dataFor(ctx, artifact)
2016-12-30 13:48:06 +02:00
if err != nil {
2016-12-30 13:53:05 +02:00
return bytes.Buffer{}, err
2016-12-30 13:48:06 +02:00
}
2017-01-19 11:04:41 +02:00
return doBuildFormula(data)
2016-12-30 13:53:05 +02:00
}
2017-07-16 20:06:32 +02:00
func doBuildFormula(data templateData) (out bytes.Buffer, err error) {
tmpl, err := template.New(data.Name).Parse(formulaTemplate)
2016-12-29 17:14:52 +02:00
if err != nil {
return out, err
}
err = tmpl.Execute(&out, data)
2017-07-16 20:06:32 +02:00
return
2016-12-29 17:14:52 +02:00
}
2018-07-04 09:42:24 +02:00
func dataFor(ctx *context.Context, artifact artifact.Artifact) (result templateData, err error) {
sum, err := checksum.SHA256(artifact.Path)
2017-07-14 01:22:10 +02:00
if err != nil {
return
}
var cfg = ctx.Config.Brew
2017-07-14 01:22:10 +02:00
return templateData{
Name: formulaNameFor(ctx.Config.Brew.Name),
DownloadURL: ctx.Config.GitHubURLs.Download,
Desc: cfg.Description,
Homepage: cfg.Homepage,
Repo: ctx.Config.Release.GitHub,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Caveats: split(cfg.Caveats),
File: artifact.Name,
SHA256: sum,
Dependencies: cfg.Dependencies,
Conflicts: cfg.Conflicts,
Plist: cfg.Plist,
Install: split(cfg.Install),
Tests: split(cfg.Test),
DownloadStrategy: cfg.DownloadStrategy,
2017-07-16 20:06:32 +02:00
}, 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.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
return strings.Replace(strings.Title(name), " ", "", -1)
2016-12-29 17:14:52 +02:00
}