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

199 lines
4.5 KiB
Go
Raw Normal View History

2016-12-29 09:58:22 -02:00
package brew
import (
"bytes"
"errors"
2016-12-30 09:48:06 -02:00
"log"
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
"github.com/google/go-github/github"
2017-01-14 20:01:32 -02:00
"github.com/goreleaser/goreleaser/clients"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/sha256sum"
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-01-19 10:04:41 +01:00
const formula = `class {{ .Name }} < Formula
2016-12-29 09:58:22 -02:00
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
2017-01-29 21:55:32 -02:00
version "{{ .Version }}"
2017-01-11 14:02:57 -02:00
sha256 "{{ .SHA256 }}"
2016-12-29 09:58:22 -02:00
2017-01-17 08:33:43 -02:00
{{- if .Dependencies }}
{{ range $index, $element := .Dependencies }}
depends_on "{{ . }}"
2017-01-17 08:33:43 -02:00
{{- end }}
{{- end }}
2017-02-01 15:40:27 -02:00
{{- if .Conflicts }}
{{ range $index, $element := .Conflicts }}
conflicts_with "{{ . }}"
{{- end }}
{{- end }}
2016-12-29 09:58:22 -02:00
def install
bin.install "{{ .BinaryName }}"
end
2016-12-31 17:13:49 -02:00
{{- if .Caveats }}
2016-12-29 13:14:52 -02:00
2016-12-31 17:02:25 -02:00
def caveats
2016-12-29 13:14:52 -02:00
"{{ .Caveats }}"
2016-12-31 17:02:25 -02:00
end
{{- end }}
2017-02-21 16:04:57 +01:00
{{- if .Plist }}
def plist; <<-EOS.undent
{{ .Plist }}
EOS
end
{{- end }}
2016-12-29 09:58:22 -02:00
end
`
type templateData struct {
2017-01-17 08:33:43 -02:00
Name string
Desc string
Homepage string
Repo string
Tag string
2017-01-29 21:55:32 -02:00
Version string
2017-01-17 08:33:43 -02:00
BinaryName string
Caveats string
File string
Format string
SHA256 string
2017-02-21 16:04:57 +01:00
Plist string
2017-01-17 08:33:43 -02:00
Dependencies []string
2017-02-01 15:40:27 -02:00
Conflicts []string
2016-12-29 09:58:22 -02:00
}
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 {
if ctx.Config.Brew.Repo == "" {
2016-12-30 09:27:35 -02:00
return nil
}
2017-02-23 16:01:54 -03:00
client := clients.GitHub(ctx)
path := filepath.Join(
ctx.Config.Brew.Folder, ctx.Config.Build.BinaryName+".rb",
)
2016-12-29 09:58:22 -02:00
2017-01-19 10:04:41 +01:00
log.Println("Pushing", path, "to", ctx.Config.Brew.Repo)
out, err := buildFormula(ctx, client)
2016-12-29 09:58:22 -02:00
if err != nil {
return err
}
2017-01-14 11:06:50 -02:00
options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String("goreleaserbot"),
Email: github.String("bot@goreleaser"),
2016-12-29 09:58:22 -02:00
},
2017-01-14 11:06:50 -02:00
Content: out.Bytes(),
2017-01-14 14:06:57 -02:00
Message: github.String(
ctx.Config.Build.BinaryName + " version " + ctx.Git.CurrentTag,
2017-01-14 14:06:57 -02:00
),
2017-01-14 11:06:50 -02:00
}
2017-01-06 11:07:41 -02:00
2017-01-14 14:50:46 -02:00
owner := ctx.BrewRepo.Owner
repo := ctx.BrewRepo.Name
2017-01-14 11:06:50 -02:00
file, _, res, err := client.Repositories.GetContents(
2017-02-23 16:01:54 -03:00
ctx, owner, repo, path, &github.RepositoryContentGetOptions{},
2016-12-30 09:48:06 -02:00
)
2017-01-14 11:06:50 -02:00
if err != nil && res.StatusCode == 404 {
2017-02-23 16:01:54 -03:00
_, _, err = client.Repositories.CreateFile(
ctx, owner, repo, path, options,
)
2017-01-14 11:06:50 -02:00
return err
2016-12-30 09:48:06 -02:00
}
2017-01-14 11:06:50 -02:00
options.SHA = file.SHA
2017-02-23 16:01:54 -03:00
_, _, err = client.Repositories.UpdateFile(ctx, owner, repo, path, options)
2017-01-14 11:06:50 -02:00
return err
2016-12-30 09:48:06 -02:00
}
2016-12-29 09:58:22 -02:00
2017-01-19 10:04:41 +01:00
func buildFormula(ctx *context.Context, client *github.Client) (bytes.Buffer, error) {
2017-01-14 14:06:57 -02:00
data, err := dataFor(ctx, client)
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-01-19 10:04:41 +01:00
func doBuildFormula(data templateData) (bytes.Buffer, error) {
2016-12-30 09:53:05 -02:00
var out bytes.Buffer
2017-01-19 10:04:41 +01:00
tmpl, err := template.New(data.BinaryName).Parse(formula)
2016-12-29 13:14:52 -02:00
if err != nil {
return out, err
}
err = tmpl.Execute(&out, data)
return out, err
}
2017-02-23 16:01:54 -03:00
func dataFor(
ctx *context.Context, client *github.Client,
) (result templateData, err error) {
2016-12-29 09:58:22 -02:00
var homepage string
var description string
2017-02-23 16:01:54 -03:00
rep, _, err := client.Repositories.Get(
ctx, ctx.ReleaseRepo.Owner, ctx.ReleaseRepo.Name,
)
2016-12-29 09:58:22 -02:00
if err != nil {
2017-01-11 14:02:57 -02:00
return
}
2017-01-14 15:08:10 -02:00
file := ctx.Archives["darwinamd64"]
if file == "" {
return result, ErrNoDarwin64Build
}
2017-01-14 14:06:57 -02:00
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
2017-01-11 14:02:57 -02:00
if err != nil {
return
2016-12-29 09:58:22 -02:00
}
2016-12-31 16:47:52 -02:00
if rep.Homepage != nil && *rep.Homepage != "" {
2016-12-29 09:58:22 -02:00
homepage = *rep.Homepage
2016-12-31 16:47:52 -02:00
} else {
homepage = *rep.HTMLURL
2016-12-29 09:58:22 -02:00
}
if rep.Description == nil {
description = "TODO"
} else {
description = *rep.Description
}
return templateData{
2017-01-17 08:33:43 -02:00
Name: formulaNameFor(ctx.Config.Build.BinaryName),
Desc: description,
Homepage: homepage,
Repo: ctx.Config.Release.Repo,
Tag: ctx.Git.CurrentTag,
2017-01-29 21:55:32 -02:00
Version: ctx.Version,
2017-01-17 08:33:43 -02:00
BinaryName: ctx.Config.Build.BinaryName,
Caveats: ctx.Config.Brew.Caveats,
File: file,
Format: ctx.Config.Archive.Format,
SHA256: sum,
Dependencies: ctx.Config.Brew.Dependencies,
2017-02-01 15:40:27 -02:00
Conflicts: ctx.Config.Brew.Conflicts,
2017-02-21 16:04:57 +01:00
Plist: ctx.Config.Brew.Plist,
2016-12-29 09:58:22 -02:00
}, err
}
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
}