2016-12-29 09:58:22 -02:00
|
|
|
package brew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-09-21 14:47:51 -03:00
|
|
|
"errors"
|
2017-12-02 19:53:19 -02:00
|
|
|
"fmt"
|
2018-01-09 21:31:18 -02:00
|
|
|
"io/ioutil"
|
2018-11-07 14:15:07 -02:00
|
|
|
"path"
|
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-12-17 20:14:41 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-05-13 18:06:15 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/client"
|
2020-07-06 14:48:17 +01:00
|
|
|
"github.com/goreleaser/goreleaser/internal/deprecate"
|
2018-09-12 14:18:01 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-07-26 16:03:28 +03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2016-12-29 09:58:22 -02:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// ErrNoArchivesFound happens when 0 archives are found.
|
2019-08-31 10:29:24 -03:00
|
|
|
var ErrNoArchivesFound = errors.New("no linux/macos archives found")
|
2017-12-17 16:31:06 -02:00
|
|
|
|
2019-06-10 10:35:19 -03:00
|
|
|
// ErrMultipleArchivesSameOS happens when the config yields multiple archives
|
|
|
|
// for linux or windows.
|
2019-09-27 02:46:05 +02:00
|
|
|
var ErrMultipleArchivesSameOS = errors.New("one tap can handle only archive of an OS/Arch combination. Consider using ids in the brew section")
|
2017-01-16 14:52:27 -02:00
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// ErrTokenTypeNotImplementedForBrew indicates that a new token type was not implemented for this pipe.
|
2020-05-03 18:22:25 +01:00
|
|
|
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"
|
|
|
|
}
|
2019-08-13 20:28:03 +02:00
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for brew deployment.
|
2016-12-30 09:27:35 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
2018-11-03 15:25:01 -03:00
|
|
|
return "homebrew tap formula"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Publish brew formula.
|
2018-10-10 12:47:31 -03:00
|
|
|
func (Pipe) Publish(ctx *context.Context) error {
|
2020-07-06 14:48:17 +01:00
|
|
|
// we keep GitHub as default for now, in line with releases
|
|
|
|
if string(ctx.TokenType) == "" {
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:29:56 -03:00
|
|
|
cli, err := client.New(ctx)
|
2017-09-22 09:42:36 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-16 11:29:56 -03:00
|
|
|
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{}
|
2020-04-29 15:09:00 -03:00
|
|
|
for _, brew := range ctx.Config.Brews {
|
2020-08-16 11:29:56 -03:00
|
|
|
var err = doRun(ctx, brew, cli)
|
|
|
|
if err != nil && pipe.IsSkip(err) {
|
|
|
|
skips.Remember(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-08-31 10:27:04 -03:00
|
|
|
return err
|
|
|
|
}
|
2019-06-10 10:35:19 -03:00
|
|
|
}
|
2020-08-16 11:29:56 -03:00
|
|
|
return skips.Evaluate()
|
2017-03-26 15:30:21 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Default sets the pipe defaults.
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2019-06-10 10:35:19 -03:00
|
|
|
for i := range ctx.Config.Brews {
|
|
|
|
var brew = &ctx.Config.Brews[i]
|
2020-07-06 14:48:17 +01:00
|
|
|
|
2019-06-10 10:35:19 -03:00
|
|
|
if brew.Install == "" {
|
2020-10-28 14:35:45 +01:00
|
|
|
// TODO: maybe replace this with a simpler also optimistic
|
2019-06-10 10:35:19 -03:00
|
|
|
// approach of just doing `bin.install "project_name"`?
|
|
|
|
var installs []string
|
|
|
|
for _, build := range ctx.Config.Builds {
|
|
|
|
if !isBrewBuild(build) {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-28 14:35:45 +01:00
|
|
|
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)
|
|
|
|
}
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
2019-06-10 10:35:19 -03:00
|
|
|
brew.Install = strings.Join(installs, "\n")
|
2020-09-16 20:43:37 +02:00
|
|
|
log.Warnf("optimistically guessing `brew[%d].install`, double check", i)
|
2019-06-10 10:35:19 -03:00
|
|
|
}
|
2020-09-21 10:13:03 -03:00
|
|
|
|
|
|
|
//nolint: staticcheck
|
2020-07-06 14:48:17 +01:00
|
|
|
if brew.GitHub.String() != "" {
|
|
|
|
deprecate.Notice(ctx, "brews.github")
|
2020-07-06 21:12:41 +01:00
|
|
|
brew.Tap.Owner = brew.GitHub.Owner
|
|
|
|
brew.Tap.Name = brew.GitHub.Name
|
2020-07-06 14:48:17 +01:00
|
|
|
}
|
|
|
|
if brew.GitLab.String() != "" {
|
|
|
|
deprecate.Notice(ctx, "brews.gitlab")
|
2020-07-06 21:12:41 +01:00
|
|
|
brew.Tap.Owner = brew.GitLab.Owner
|
|
|
|
brew.Tap.Name = brew.GitLab.Name
|
2020-07-06 14:48:17 +01:00
|
|
|
}
|
2019-06-10 10:35:19 -03:00
|
|
|
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
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
2019-09-27 02:46:05 +02:00
|
|
|
if brew.Goarm == "" {
|
|
|
|
brew.Goarm = "6"
|
|
|
|
}
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:12:41 +01:00
|
|
|
func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
|
2020-07-06 14:48:17 +01:00
|
|
|
if brew.Tap.Name == "" {
|
2018-09-12 14:18:01 -03:00
|
|
|
return pipe.Skip("brew section is not configured")
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
2019-08-13 20:28:03 +02:00
|
|
|
|
2020-07-06 21:12:41 +01: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 09:42:11 -03:00
|
|
|
// TODO: properly cover this with tests
|
2019-06-10 10:35:19 -03:00
|
|
|
var filters = []artifact.Filter{
|
|
|
|
artifact.Or(
|
2017-12-17 16:59:54 -02:00
|
|
|
artifact.ByGoos("darwin"),
|
2019-06-10 10:35:19 -03:00
|
|
|
artifact.ByGoos("linux"),
|
2017-12-17 16:59:54 -02:00
|
|
|
),
|
2019-06-10 10:35:19 -03:00
|
|
|
artifact.ByFormats("zip", "tar.gz"),
|
2019-09-09 14:34:52 +02:00
|
|
|
artifact.Or(
|
|
|
|
artifact.ByGoarch("amd64"),
|
|
|
|
artifact.ByGoarch("arm64"),
|
2019-09-27 02:46:05 +02:00
|
|
|
artifact.And(
|
|
|
|
artifact.ByGoarch("arm"),
|
|
|
|
artifact.ByGoarm(brew.Goarm),
|
|
|
|
),
|
2019-09-09 14:34:52 +02:00
|
|
|
),
|
2019-06-10 10:35:19 -03:00
|
|
|
artifact.ByType(artifact.UploadableArchive),
|
2017-07-13 20:22:10 -03:00
|
|
|
}
|
2019-06-10 10:35:19 -03: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-13 20:22:10 -03:00
|
|
|
}
|
2018-01-09 21:31:18 -02:00
|
|
|
|
2020-08-15 17:16:48 -03:00
|
|
|
name, err := tmpl.New(ctx).Apply(brew.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
brew.Name = name
|
|
|
|
|
2020-07-06 21:12:41 +01:00
|
|
|
content, err := buildFormula(ctx, brew, cl, archives)
|
2016-12-29 09:58:22 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-09 21:31:18 -02:00
|
|
|
|
2019-06-10 10:35:19 -03:00
|
|
|
var filename = brew.Name + ".rb"
|
2018-01-09 21:31:18 -02:00
|
|
|
var path = filepath.Join(ctx.Config.Dist, filename)
|
|
|
|
log.WithField("formula", path).Info("writing")
|
2020-05-26 00:48:10 -03:00
|
|
|
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil { //nolint: gosec
|
2020-09-21 14:47:51 -03:00
|
|
|
return fmt.Errorf("failed to write brew formula: %w", err)
|
2018-01-09 21:31:18 -02:00
|
|
|
}
|
|
|
|
|
2020-04-29 15:09:00 -03: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 19:22:37 -02:00
|
|
|
|
2020-07-06 21:12:41 +01:00
|
|
|
repo := client.RepoFromRef(brew.Tap)
|
2020-04-29 15:09:00 -03:00
|
|
|
|
|
|
|
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)
|
2020-07-06 21:12:41 +01:00
|
|
|
return cl.CreateFile(ctx, brew.CommitAuthor, repo, []byte(content), gpath, msg)
|
2018-11-06 10:46:00 -02:00
|
|
|
}
|
|
|
|
|
2019-08-13 20:28:03 +02:00
|
|
|
func buildFormulaPath(folder, filename string) string {
|
2018-11-07 14:15:07 -02:00
|
|
|
return path.Join(folder, filename)
|
2016-12-30 09:48:06 -02:00
|
|
|
}
|
2016-12-29 09:58:22 -02:00
|
|
|
|
2020-07-06 14:48:17 +01: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 09:48:06 -02:00
|
|
|
if err != nil {
|
2019-06-26 14:12:33 -03:00
|
|
|
return "", err
|
2016-12-30 09:48:06 -02:00
|
|
|
}
|
2019-06-26 14:12:33 -03:00
|
|
|
return doBuildFormula(ctx, data)
|
2016-12-30 09:53:05 -02:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:12:33 -03:00
|
|
|
func doBuildFormula(ctx *context.Context, data templateData) (string, error) {
|
2018-07-26 16:03:28 +03:00
|
|
|
t, err := template.New(data.Name).Parse(formulaTemplate)
|
2016-12-29 13:14:52 -02:00
|
|
|
if err != nil {
|
2019-06-26 14:12:33 -03:00
|
|
|
return "", err
|
2016-12-29 13:14:52 -02:00
|
|
|
}
|
2019-06-26 14:12:33 -03: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 13:14:52 -02:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:48:17 +01:00
|
|
|
func dataFor(ctx *context.Context, cfg config.Homebrew, cl client.Client, artifacts []*artifact.Artifact) (templateData, error) {
|
2019-06-10 10:35:19 -03:00
|
|
|
var result = templateData{
|
|
|
|
Name: formulaNameFor(cfg.Name),
|
2018-06-20 09:46:42 -03:00
|
|
|
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),
|
2020-09-18 10:42:52 -03:00
|
|
|
PostInstall: cfg.PostInstall,
|
2018-06-20 09:46:42 -03:00
|
|
|
Tests: split(cfg.Test),
|
|
|
|
DownloadStrategy: cfg.DownloadStrategy,
|
2018-11-21 15:57:44 +00:00
|
|
|
CustomRequire: cfg.CustomRequire,
|
2018-12-30 02:06:54 +00:00
|
|
|
CustomBlock: split(cfg.CustomBlock),
|
2019-06-10 10:35:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, artifact := range artifacts {
|
|
|
|
sum, err := artifact.Checksum("sha256")
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.URLTemplate == "" {
|
2020-07-06 14:48:17 +01:00
|
|
|
url, err := cl.ReleaseURLTemplate(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if client.IsNotImplementedErr(err) {
|
|
|
|
return result, ErrTokenTypeNotImplementedForBrew{ctx.TokenType}
|
|
|
|
}
|
|
|
|
return result, err
|
2019-08-13 20:28:03 +02:00
|
|
|
}
|
2020-07-06 14:48:17 +01:00
|
|
|
cfg.URLTemplate = url
|
2019-06-10 10:35:19 -03:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
2020-05-26 00:48:10 -03:00
|
|
|
// TODO: refactor
|
|
|
|
if artifact.Goos == "darwin" { // nolint: nestif
|
2019-06-10 10:35:19 -03:00
|
|
|
if result.MacOS.DownloadURL != "" {
|
|
|
|
return result, ErrMultipleArchivesSameOS
|
|
|
|
}
|
|
|
|
result.MacOS = down
|
|
|
|
} else if artifact.Goos == "linux" {
|
2019-08-13 19:37:11 +03:00
|
|
|
switch artifact.Goarch {
|
|
|
|
case "386", "amd64":
|
|
|
|
if result.Linux.DownloadURL != "" {
|
|
|
|
return result, ErrMultipleArchivesSameOS
|
|
|
|
}
|
|
|
|
result.Linux = down
|
|
|
|
case "arm":
|
|
|
|
if result.Arm.DownloadURL != "" {
|
|
|
|
return result, ErrMultipleArchivesSameOS
|
|
|
|
}
|
|
|
|
result.Arm = down
|
|
|
|
case "arm64":
|
|
|
|
if result.Arm64.DownloadURL != "" {
|
|
|
|
return result, ErrMultipleArchivesSameOS
|
|
|
|
}
|
|
|
|
result.Arm64 = down
|
2019-06-10 10:35:19 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, 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 {
|
2018-04-05 14:11:31 -07:00
|
|
|
strings := strings.Split(strings.TrimSpace(s), "\n")
|
|
|
|
if len(strings) == 1 && strings[0] == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return strings
|
2017-07-16 16:01:20 -03:00
|
|
|
}
|
|
|
|
|
2016-12-29 10:55:35 -02:00
|
|
|
func formulaNameFor(name string) string {
|
2020-09-21 10:13:03 -03:00
|
|
|
name = strings.ReplaceAll(name, "-", " ")
|
|
|
|
name = strings.ReplaceAll(name, "_", " ")
|
|
|
|
name = strings.ReplaceAll(name, "@", "AT")
|
|
|
|
return strings.ReplaceAll(strings.Title(name), " ", "")
|
2016-12-29 13:14:52 -02:00
|
|
|
}
|