1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

brew release

This commit is contained in:
Carlos Alexandro Becker 2016-12-29 09:58:22 -02:00
parent 9b0a96f8b5
commit 943a5ac42e
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
2 changed files with 103 additions and 3 deletions

97
brew/brew.go Normal file
View File

@ -0,0 +1,97 @@
package brew
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"strings"
"github.com/alecthomas/template"
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"golang.org/x/oauth2"
)
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .BinaryName }}_Darwin_x86_64.tar.gz"
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)
_, _, 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),
SHA: github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes()))),
},
)
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{
Name: strings.Title(config.BinaryName),
Desc: description,
Homepage: homepage,
Repo: config.Repo,
Tag: version,
BinaryName: config.BinaryName,
}, err
}

View File

@ -4,10 +4,11 @@ import (
"fmt"
"log"
"github.com/goreleaser/releaser/brew"
"github.com/goreleaser/releaser/build"
"github.com/goreleaser/releaser/compress"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/git"
"github.com/goreleaser/releaser/compress"
"github.com/goreleaser/releaser/release"
)
@ -41,9 +42,11 @@ func main() {
}
err = release.Release(tag, diff, config)
if err != nil {
log.Fatalln("Failed to create the GitHub release", err.Error())
log.Fatalln("Failed to create GitHub release", err.Error())
}
if config.Brew.Repo != "" {
// release to brew
if err := brew.Brew(tag, config); err != nil {
log.Fatalln("Failed to create homebrew formula", err.Error())
}
}
}