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

157 lines
3.6 KiB
Go
Raw Normal View History

2016-12-29 13:58:22 +02:00
package brew
import (
"bytes"
2017-01-14 18:06:57 +02:00
goctx "context"
2016-12-30 13:48:06 +02:00
"log"
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
"github.com/google/go-github/github"
2017-01-14 16:34:22 +02:00
"github.com/goreleaser/releaser/context"
2017-01-11 18:02:57 +02:00
"github.com/goreleaser/releaser/sha256sum"
2016-12-29 13:58:22 +02:00
"golang.org/x/oauth2"
)
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
2016-12-29 16:51:37 +02:00
version "{{ .Tag }}"
2017-01-11 18:02:57 +02:00
sha256 "{{ .SHA256 }}"
2016-12-29 13:58:22 +02:00
def install
bin.install "{{ .BinaryName }}"
end
2016-12-31 21:13:49 +02:00
{{- if .Caveats }}
2016-12-29 17:14:52 +02:00
2016-12-31 21:02:25 +02:00
def caveats
2016-12-29 17:14:52 +02:00
"{{ .Caveats }}"
2016-12-31 21:02:25 +02:00
end
{{- end }}
2016-12-29 13:58:22 +02:00
end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats, File, Format, SHA256 string
2016-12-29 13:58:22 +02:00
}
2016-12-30 16:41:59 +02:00
// Pipe for brew deployment
2016-12-30 13:27:35 +02:00
type Pipe struct{}
2016-12-30 16:41:59 +02:00
// Name of the pipe
2016-12-30 13:27:35 +02:00
func (Pipe) Name() string {
return "Homebrew"
}
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 {
if ctx.Config.Brew.Repo == "" {
2016-12-30 13:27:35 +02:00
return nil
}
2016-12-29 13:58:22 +02:00
ts := oauth2.StaticTokenSource(
2017-01-14 18:06:57 +02:00
&oauth2.Token{AccessToken: *ctx.Token},
2016-12-29 13:58:22 +02:00
)
2017-01-14 18:06:57 +02:00
tc := oauth2.NewClient(goctx.Background(), ts)
2016-12-29 13:58:22 +02:00
client := github.NewClient(tc)
2017-01-14 18:06:57 +02:00
owner := ctx.BrewRepo.Owner
repo := ctx.BrewRepo.Name
path := filepath.Join(
ctx.Config.Brew.Folder,
ctx.Config.BinaryName+".rb",
)
log.Println("Updating", path, "on", ctx.Config.Brew.Repo, "...")
out, err := buildFormulae(ctx, client)
2016-12-29 13:58:22 +02:00
if err != nil {
return err
}
2017-01-14 15:06:50 +02:00
options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String("goreleaserbot"),
Email: github.String("bot@goreleaser"),
2016-12-29 13:58:22 +02:00
},
2017-01-14 15:06:50 +02:00
Content: out.Bytes(),
2017-01-14 18:06:57 +02:00
Message: github.String(
ctx.Config.BinaryName + " version " + ctx.Git.CurrentTag,
),
2017-01-14 15:06:50 +02:00
}
2017-01-06 15:07:41 +02:00
2017-01-14 15:06:50 +02:00
file, _, res, err := client.Repositories.GetContents(
owner, repo, path, &github.RepositoryContentGetOptions{},
2016-12-30 13:48:06 +02:00
)
2017-01-14 15:06:50 +02:00
if err != nil && res.StatusCode == 404 {
_, _, err = client.Repositories.CreateFile(owner, repo, path, options)
return err
2016-12-30 13:48:06 +02:00
}
2017-01-14 15:06:50 +02:00
options.SHA = file.SHA
_, _, err = client.Repositories.UpdateFile(owner, repo, path, options)
return err
2016-12-30 13:48:06 +02:00
}
2016-12-29 13:58:22 +02:00
2017-01-14 18:06:57 +02:00
func buildFormulae(ctx *context.Context, client *github.Client) (bytes.Buffer, error) {
data, err := dataFor(ctx, client)
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
}
2016-12-30 13:53:05 +02:00
return doBuildFormulae(data)
}
func doBuildFormulae(data templateData) (bytes.Buffer, error) {
var out bytes.Buffer
2016-12-29 17:14:52 +02:00
tmpl, err := template.New(data.BinaryName).Parse(formulae)
if err != nil {
return out, err
}
err = tmpl.Execute(&out, data)
return out, err
}
2017-01-14 18:06:57 +02:00
func dataFor(ctx *context.Context, client *github.Client) (result templateData, err error) {
2016-12-29 13:58:22 +02:00
var homepage string
var description string
2017-01-14 18:06:57 +02:00
rep, _, err := client.Repositories.Get(ctx.Repo.Owner, ctx.Repo.Name)
2016-12-29 13:58:22 +02:00
if err != nil {
2017-01-11 18:02:57 +02:00
return
}
2017-01-14 18:06:57 +02:00
file, err := ctx.ArchiveName("darwin", "amd64")
2017-01-11 18:28:12 +02:00
if err != nil {
return
}
2017-01-14 18:06:57 +02:00
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
2017-01-11 18:02:57 +02:00
if err != nil {
return
2016-12-29 13:58:22 +02:00
}
2016-12-31 20:47:52 +02:00
if rep.Homepage != nil && *rep.Homepage != "" {
2016-12-29 13:58:22 +02:00
homepage = *rep.Homepage
2016-12-31 20:47:52 +02:00
} else {
homepage = *rep.HTMLURL
2016-12-29 13:58:22 +02:00
}
if rep.Description == nil {
description = "TODO"
} else {
description = *rep.Description
}
return templateData{
2017-01-14 18:06:57 +02:00
Name: formulaNameFor(ctx.Config.BinaryName),
2016-12-29 13:58:22 +02:00
Desc: description,
Homepage: homepage,
2017-01-14 18:06:57 +02:00
Repo: ctx.Config.Repo,
Tag: ctx.Git.CurrentTag,
BinaryName: ctx.Config.BinaryName,
Caveats: ctx.Config.Brew.Caveats,
2017-01-06 15:07:41 +02:00
File: file,
2017-01-14 18:06:57 +02:00
Format: ctx.Config.Archive.Format,
2017-01-11 18:08:29 +02:00
SHA256: sum,
2016-12-29 13:58:22 +02:00
}, err
}
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
}