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

126 lines
3.0 KiB
Go
Raw Normal View History

2016-12-29 13:58:22 +02:00
package brew
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"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"
"github.com/goreleaser/releaser/config"
"golang.org/x/oauth2"
)
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
2016-12-29 16:51:20 +02:00
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .BinaryName }}_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}.tar.gz"
2016-12-29 13:58:22 +02:00
head "https://github.com/{{ .Repo }}.git"
2016-12-29 16:51:37 +02:00
version "{{ .Tag }}"
2016-12-29 13:58:22 +02:00
def install
bin.install "{{ .BinaryName }}"
end
2016-12-29 17:14:52 +02:00
2016-12-29 17:19:03 +02:00
{{ if .Caveats }}def caveats
2016-12-29 17:14:52 +02:00
"{{ .Caveats }}"
2016-12-29 17:19:03 +02:00
end{{ end }}
2016-12-29 13:58:22 +02:00
end
`
type templateData struct {
2016-12-29 17:14:52 +02:00
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats string
2016-12-29 13:58:22 +02:00
}
func Brew(version string, config config.ProjectConfig) error {
fmt.Println("Updating brew formulae...")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
parts := strings.Split(config.Brew.Repo, "/")
2016-12-29 17:14:52 +02:00
data, err := dataFor(version, config, client)
2016-12-29 13:58:22 +02:00
if err != nil {
return err
}
2016-12-29 17:14:52 +02:00
out, err := buildFormulae(data)
2016-12-29 13:58:22 +02:00
if err != nil {
return err
}
2016-12-29 14:15:08 +02:00
var sha *string
file, _, _, err := client.Repositories.GetContents(
parts[0], parts[1], config.BinaryName+".rb", &github.RepositoryContentGetOptions{},
)
if err == nil {
sha = file.SHA
} else {
sha = github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes())))
}
2016-12-29 13:58:22 +02:00
_, _, err = client.Repositories.UpdateFile(
parts[0],
parts[1],
config.BinaryName+".rb",
&github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String("goreleaserbot"),
Email: github.String("bot@goreleaser"),
},
Content: out.Bytes(),
Message: github.String(config.BinaryName + " version " + version),
2016-12-29 14:15:08 +02:00
SHA: sha,
2016-12-29 13:58:22 +02:00
},
)
return err
}
2016-12-29 17:14:52 +02:00
func buildFormulae(data templateData) (bytes.Buffer, error) {
var out bytes.Buffer
tmpl, err := template.New(data.BinaryName).Parse(formulae)
if err != nil {
return out, err
}
err = tmpl.Execute(&out, data)
return out, err
}
2016-12-29 13:58:22 +02:00
func dataFor(version string, config config.ProjectConfig, client *github.Client) (result templateData, err error) {
var homepage string
var description string
parts := strings.Split(config.Repo, "/")
rep, _, err := client.Repositories.Get(parts[0], parts[1])
if err != nil {
return result, err
}
if rep.Homepage == nil {
homepage = *rep.HTMLURL
} else {
homepage = *rep.Homepage
}
if rep.Description == nil {
description = "TODO"
} else {
description = *rep.Description
}
return templateData{
2016-12-29 14:55:35 +02:00
Name: formulaNameFor(config.BinaryName),
2016-12-29 13:58:22 +02:00
Desc: description,
Homepage: homepage,
Repo: config.Repo,
Tag: version,
BinaryName: config.BinaryName,
2016-12-29 17:14:52 +02:00
Caveats: config.Brew.Caveats,
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
}