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

171 lines
4.0 KiB
Go
Raw Normal View History

2016-12-29 09:58:22 -02:00
package brew
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
2016-12-30 09:48:06 -02:00
"log"
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"
"github.com/goreleaser/releaser/config"
2017-01-06 11:13:17 -02:00
"github.com/goreleaser/releaser/name"
2017-01-11 14:02:57 -02:00
"github.com/goreleaser/releaser/sha256sum"
2016-12-30 09:48:06 -02:00
"github.com/goreleaser/releaser/split"
2016-12-29 09: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 09:58:22 -02:00
head "https://github.com/{{ .Repo }}.git"
2016-12-29 12:51:37 -02:00
version "{{ .Tag }}"
2017-01-11 14:02:57 -02:00
sha256 "{{ .SHA256 }}"
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 }}
2016-12-29 09:58:22 -02:00
end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats, File, Format, SHA256 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{}
2016-12-30 12:41:59 -02:00
// Name of the pipe
2016-12-30 09:27:35 -02:00
func (Pipe) Name() string {
return "Homebrew"
}
2016-12-30 12:41:59 -02:00
// Run the pipe
func (Pipe) Run(config config.ProjectConfig) error {
2016-12-30 09:27:35 -02:00
if config.Brew.Repo == "" {
return nil
}
2016-12-29 09:58:22 -02:00
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
2016-12-30 09:48:06 -02:00
owner, repo := split.OnSlash(config.Brew.Repo)
name := config.BinaryName + ".rb"
2016-12-30 10:06:09 -02:00
log.Println("Updating", name, "on", config.Brew.Repo, "...")
2016-12-30 09:48:06 -02:00
out, err := buildFormulae(config, client)
2016-12-29 09:58:22 -02:00
if err != nil {
return err
}
2017-01-11 14:08:29 -02:00
sha, err := formulaeSHA(client, owner, repo, name, out)
2016-12-29 09:58:22 -02:00
if err != nil {
return err
}
_, _, err = client.Repositories.UpdateFile(
2016-12-30 09:48:06 -02:00
owner, repo, name, &github.RepositoryContentFileOptions{
2016-12-29 09:58:22 -02:00
Committer: &github.CommitAuthor{
Name: github.String("goreleaserbot"),
Email: github.String("bot@goreleaser"),
},
Content: out.Bytes(),
2016-12-30 09:27:35 -02:00
Message: github.String(config.BinaryName + " version " + config.Git.CurrentTag),
2016-12-29 10:15:08 -02:00
SHA: sha,
2016-12-29 09:58:22 -02:00
},
)
return err
}
2017-01-06 11:07:41 -02:00
2017-01-11 14:08:29 -02:00
func formulaeSHA(client *github.Client, owner, repo, name string, out bytes.Buffer) (*string, error) {
2016-12-30 09:48:06 -02:00
file, _, _, err := client.Repositories.GetContents(
owner, repo, name, &github.RepositoryContentGetOptions{},
)
if err == nil {
2017-01-02 11:49:30 -02:00
return file.SHA, err
2016-12-30 09:48:06 -02:00
}
2017-01-07 17:51:23 -02:00
return github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes()))), nil
2016-12-30 09:48:06 -02:00
}
2016-12-29 09:58:22 -02:00
2016-12-30 09:48:06 -02:00
func buildFormulae(config config.ProjectConfig, client *github.Client) (bytes.Buffer, error) {
data, err := dataFor(config, client)
if err != nil {
2016-12-30 09:53:05 -02:00
return bytes.Buffer{}, err
2016-12-30 09:48:06 -02:00
}
2016-12-30 09:53:05 -02:00
return doBuildFormulae(data)
}
func doBuildFormulae(data templateData) (bytes.Buffer, error) {
var out bytes.Buffer
2016-12-29 13: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
}
2016-12-30 09:27:35 -02:00
func dataFor(config config.ProjectConfig, client *github.Client) (result templateData, err error) {
2016-12-29 09:58:22 -02:00
var homepage string
var description string
2016-12-30 09:48:06 -02:00
owner, repo := split.OnSlash(config.Repo)
rep, _, err := client.Repositories.Get(owner, repo)
2016-12-29 09:58:22 -02:00
if err != nil {
2017-01-11 14:02:57 -02:00
return
}
sum, err := sha256sum.For("dist/" + config.BinaryName + "_Darwin_x86_64." + config.Archive.Format)
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
}
2017-01-06 11:07:41 -02:00
file, err := fileName(config)
if err != nil {
return result, err
}
2016-12-29 09:58:22 -02:00
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,
2016-12-30 09:27:35 -02:00
Tag: config.Git.CurrentTag,
2016-12-29 09:58:22 -02:00
BinaryName: config.BinaryName,
2016-12-29 13:14:52 -02:00
Caveats: config.Brew.Caveats,
2017-01-06 11:07:41 -02:00
File: file,
2017-01-06 13:48:25 -02:00
Format: config.Archive.Format,
2017-01-11 14:08:29 -02:00
SHA256: sum,
2016-12-29 09:58:22 -02:00
}, err
}
2016-12-29 10:55:35 -02:00
2017-01-06 11:07:41 -02:00
func fileName(config config.ProjectConfig) (string, error) {
return name.For(
config,
"#{%x(uname -s).gsub(/\n/, '')}",
"#{%x(uname -m).gsub(/\n/, '')}",
)
}
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
}