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

262 lines
6.9 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"
2018-11-07 18:15:07 +02:00
"path"
2017-01-14 15:18:48 +02:00
"path/filepath"
"reflect"
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"
"github.com/goreleaser/goreleaser/internal/deprecate"
2018-09-12 19:18:01 +02:00
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"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("brew tap: no archives found matching criteria")
2017-12-17 20:31:06 +02:00
// ErrMultipleArchivesSameOS happens when the config yields multiple archives
// for linux or windows.
// TODO: improve this confusing error message
var ErrMultipleArchivesSameOS = errors.New("brew tap: one tap can handle only 1 linux and 1 macos archive")
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 {
2018-11-03 20:25:01 +02:00
return "homebrew tap formula"
2016-12-30 13:27:35 +02:00
}
2018-10-10 17:47:31 +02:00
// Publish brew formula
func (Pipe) Publish(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
}
var g = semerrgroup.New(ctx.Parallelism)
for _, brew := range ctx.Config.Brews {
brew := brew
g.Go(func() error {
return doRun(ctx, brew, client)
})
}
return g.Wait()
2017-03-26 20:30:21 +02:00
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if len(ctx.Config.Brews) == 0 {
ctx.Config.Brews = append(ctx.Config.Brews, ctx.Config.Brew)
if !reflect.DeepEqual(ctx.Config.Brew, config.Homebrew{}) {
deprecate.Notice("brew")
}
}
for i := range ctx.Config.Brews {
var brew = &ctx.Config.Brews[i]
if brew.Install == "" {
// TODO: maybe replace this with a simplear also optimistic
// approach of just doing `bin.install "project_name"`?
var installs []string
for _, build := range ctx.Config.Builds {
if !isBrewBuild(build) {
continue
}
installs = append(
installs,
fmt.Sprintf(`bin.install "%s"`, build.Binary),
)
}
brew.Install = strings.Join(installs, "\n")
log.Warnf("optimistically guessing `brew[%d].installs`, double check", i)
}
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
}
}
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, client client.Client) error {
if brew.GitHub.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
}
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"),
artifact.ByGoarch("amd64"),
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
content, err := buildFormula(ctx, brew, 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 {
2018-01-10 01:31:18 +02:00
return err
}
if strings.TrimSpace(brew.SkipUpload) == "true" {
2018-09-12 19:18:01 +02:00
return pipe.Skip("brew.skip_upload is set")
2018-01-10 23:22:37 +02:00
}
if ctx.SkipPublish {
2018-09-12 19:18:01 +02:00
return pipe.ErrSkipPublishEnabled
2018-01-10 23:22:37 +02:00
}
if ctx.Config.Release.Draft {
2018-09-12 19:18:01 +02:00
return pipe.Skip("release is marked as draft")
2018-01-10 23:22:37 +02:00
}
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
var gpath = ghFormulaPath(brew.Folder, filename)
2018-11-06 14:46:00 +02:00
log.WithField("formula", gpath).
WithField("repo", brew.GitHub.String()).
2018-01-10 01:31:18 +02:00
Info("pushing")
var msg = fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
return client.CreateFile(ctx, brew.CommitAuthor, brew.GitHub, []byte(content), gpath, msg)
2018-11-06 14:46:00 +02:00
}
func ghFormulaPath(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, artifacts []artifact.Artifact) (string, error) {
data, err := dataFor(ctx, brew, 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
}
return tmpl.New(ctx).Apply(out.String())
2016-12-29 17:14:52 +02:00
}
func dataFor(ctx *context.Context, cfg config.Homebrew, artifacts []artifact.Artifact) (templateData, error) {
var result = templateData{
Name: formulaNameFor(cfg.Name),
Desc: cfg.Description,
Homepage: cfg.Homepage,
Version: ctx.Version,
Caveats: split(cfg.Caveats),
Dependencies: cfg.Dependencies,
Conflicts: cfg.Conflicts,
Plist: cfg.Plist,
Install: split(cfg.Install),
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 == "" {
cfg.URLTemplate = fmt.Sprintf(
"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
ctx.Config.GitHubURLs.Download,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
)
}
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,
}
if artifact.Goos == "darwin" {
if result.MacOS.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.MacOS = down
} else if artifact.Goos == "linux" {
if result.Linux.DownloadURL != "" {
return result, ErrMultipleArchivesSameOS
}
result.Linux = 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.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
return strings.Replace(strings.Title(name), " ", "", -1)
2016-12-29 17:14:52 +02:00
}