1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

126 lines
3.4 KiB
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// Package brew implements the Pipe, providing formula generation and
// uploading it to a configured repo.
2016-12-29 09:58:22 -02:00
package brew
import (
"bytes"
"errors"
2017-01-14 11:18:48 -02:00
"path/filepath"
2016-12-29 09:58:22 -02:00
"strings"
2016-12-29 13:14:52 -02:00
"text/template"
2016-12-29 09:58:22 -02:00
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-07-13 20:22:10 -03:00
"github.com/goreleaser/goreleaser/checksum"
2017-01-14 20:01:32 -02:00
"github.com/goreleaser/goreleaser/context"
2017-07-13 20:22:10 -03:00
"github.com/goreleaser/goreleaser/internal/archiveformat"
2017-05-13 18:06:15 -03:00
"github.com/goreleaser/goreleaser/internal/client"
2016-12-29 09:58:22 -02:00
)
// ErrNoDarwin64Build when there is no build for darwin_amd64 (goos doesn't
// contain darwin and/or goarch doesn't contain amd64)
var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build")
2017-07-01 22:11:50 -03:00
const platform = "darwinamd64"
2016-12-30 12:41:59 -02:00
// Pipe for brew deployment
2016-12-30 09:27:35 -02:00
type Pipe struct{}
2017-01-14 19:41:32 +01:00
// Description of the pipe
2017-01-14 15:14:35 -02:00
func (Pipe) Description() string {
2017-01-19 10:04:41 +01:00
return "Creating homebrew formula"
2016-12-30 09:27:35 -02:00
}
2016-12-30 12:41:59 -02:00
// Run the pipe
2017-01-14 14:06:57 -02:00
func (Pipe) Run(ctx *context.Context) error {
2017-04-14 16:07:27 -03:00
return doRun(ctx, client.NewGitHub(ctx))
2017-03-26 15:30:21 -03:00
}
2017-04-14 16:07:27 -03:00
func doRun(ctx *context.Context, client client.Client) error {
if !ctx.Publish {
2017-06-22 10:47:34 -03:00
log.Warn("skipped because --skip-publish is set")
return nil
}
if ctx.Config.Brew.GitHub.Name == "" {
2017-06-22 10:47:34 -03:00
log.Warn("skipped because brew section is not configured")
2016-12-30 09:27:35 -02:00
return nil
}
2017-04-21 19:50:09 -03:00
if ctx.Config.Release.Draft {
2017-06-22 10:47:34 -03:00
log.Warn("skipped because release is marked as draft")
2017-04-21 19:50:09 -03:00
return nil
}
2017-07-16 15:23:29 -03:00
if ctx.Config.Archive.Format == "binary" {
log.Warn("skipped because archive format is binary")
return nil
}
2017-07-16 15:23:29 -03:00
2017-07-13 20:22:10 -03:00
var group = ctx.Binaries["darwinamd64"]
if group == nil {
return ErrNoDarwin64Build
}
var folder string
for f := range group {
folder = f
break
}
2017-07-01 22:42:10 -03:00
var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.ProjectName+".rb")
2017-06-22 10:47:34 -03:00
log.WithField("formula", path).
WithField("repo", ctx.Config.Brew.GitHub.String()).
Info("pushing")
2017-07-13 20:22:10 -03:00
content, err := buildFormula(ctx, client, folder)
2016-12-29 09:58:22 -02:00
if err != nil {
return err
}
2017-07-16 15:13:30 -03:00
return client.CreateFile(ctx, content, path)
2016-12-30 09:48:06 -02:00
}
2016-12-29 09:58:22 -02:00
2017-07-13 20:22:10 -03:00
func buildFormula(ctx *context.Context, client client.Client, folder string) (bytes.Buffer, error) {
data, err := dataFor(ctx, client, folder)
2016-12-30 09:48:06 -02:00
if err != nil {
2016-12-30 09:53:05 -02:00
return bytes.Buffer{}, err
2016-12-30 09:48:06 -02:00
}
2017-01-19 10:04:41 +01:00
return doBuildFormula(data)
2016-12-30 09:53:05 -02:00
}
2017-07-16 15:06:32 -03:00
func doBuildFormula(data templateData) (out bytes.Buffer, err error) {
tmpl, err := template.New(data.Name).Parse(formulaTemplate)
2016-12-29 13:14:52 -02:00
if err != nil {
return out, err
}
err = tmpl.Execute(&out, data)
2017-07-16 15:06:32 -03:00
return
2016-12-29 13:14:52 -02:00
}
2017-07-13 20:22:10 -03:00
func dataFor(ctx *context.Context, client client.Client, folder string) (result templateData, err error) {
var file = folder + "." + archiveformat.For(ctx, platform)
sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file))
if err != nil {
return
}
return templateData{
Name: formulaNameFor(ctx.Config.ProjectName),
Desc: ctx.Config.Brew.Description,
Homepage: ctx.Config.Brew.Homepage,
Repo: ctx.Config.Release.GitHub,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Caveats: ctx.Config.Brew.Caveats,
File: file,
SHA256: sum,
Dependencies: ctx.Config.Brew.Dependencies,
Conflicts: ctx.Config.Brew.Conflicts,
Plist: ctx.Config.Brew.Plist,
2017-07-16 16:01:20 -03:00
Install: split(ctx.Config.Brew.Install),
Tests: split(ctx.Config.Brew.Test),
2017-07-16 15:06:32 -03:00
}, nil
2016-12-29 09:58:22 -02:00
}
2016-12-29 10:55:35 -02:00
2017-07-16 16:01:20 -03:00
func split(s string) []string {
return strings.Split(strings.TrimSpace(s), "\n")
}
2016-12-29 10: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 13:14:52 -02:00
}