forked from goreleaser/goreleaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brew.go
135 lines (120 loc) · 3.09 KB
/
brew.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package brew
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"strings"
"text/template"
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"golang.org/x/oauth2"
"log"
)
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .BinaryName }}_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}.tar.gz"
head "https://github.com/{{ .Repo }}.git"
version "{{ .Tag }}"
def install
bin.install "{{ .BinaryName }}"
end
{{ if .Caveats }}def caveats
"{{ .Caveats }}"
end{{ end }}
end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats string
}
type Pipe struct{}
func (Pipe) Name() string {
return "Homebrew"
}
func (Pipe) Work(config config.ProjectConfig) error {
if config.Brew.Repo == "" {
return nil
}
log.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, "/")
data, err := dataFor(config, client)
if err != nil {
return err
}
out, err := buildFormulae(data)
if err != nil {
return err
}
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())))
}
_, _, 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 " + config.Git.CurrentTag),
SHA: sha,
},
)
return err
}
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
}
func dataFor(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: formulaNameFor(config.BinaryName),
Desc: description,
Homepage: homepage,
Repo: config.Repo,
Tag: config.Git.CurrentTag,
BinaryName: config.BinaryName,
Caveats: config.Brew.Caveats,
}, err
}
func formulaNameFor(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
return strings.Replace(strings.Title(name), " ", "", -1)
}