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

113 lines
2.7 KiB
Go
Raw Normal View History

2016-12-29 09:58:22 -02:00
package brew
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
2016-12-29 10:15:08 -02:00
"text/template"
2016-12-29 09:58:22 -02:00
"strings"
"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 12: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 09:58:22 -02:00
head "https://github.com/{{ .Repo }}.git"
def install
bin.install "{{ .BinaryName }}"
end
end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName string
}
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, "/")
tmpl, err := template.New(config.BinaryName).Parse(formulae)
if err != nil {
return err
}
data, err := dataFor(version, config, client)
if err != nil {
return err
}
var out bytes.Buffer
tmpl.Execute(&out, data)
2016-12-29 10: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 09: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 10:15:08 -02:00
SHA: sha,
2016-12-29 09:58:22 -02:00
},
)
return err
}
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 10:55:35 -02:00
Name: formulaNameFor(config.BinaryName),
2016-12-29 09:58:22 -02:00
Desc: description,
Homepage: homepage,
Repo: config.Repo,
Tag: version,
BinaryName: config.BinaryName,
}, 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)
}