2016-12-29 13:58:22 +02:00
|
|
|
package brew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-01-16 18:52:27 +02:00
|
|
|
"errors"
|
2016-12-30 13:48:06 +02:00
|
|
|
"log"
|
2017-01-14 15:18:48 +02:00
|
|
|
"path/filepath"
|
2016-12-29 13:58:22 +02:00
|
|
|
"strings"
|
2016-12-29 17:14:52 +02:00
|
|
|
"text/template"
|
2016-12-29 13:58:22 +02:00
|
|
|
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/clients"
|
2017-03-23 02:01:29 +02:00
|
|
|
"github.com/goreleaser/goreleaser/config"
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/goreleaser/goreleaser/sha256sum"
|
2016-12-29 13:58:22 +02:00
|
|
|
)
|
|
|
|
|
2017-01-16 18:52:27 +02:00
|
|
|
// ErrNoDarwin64Build when there is no build for darwin_amd64 (goos doesn't
|
|
|
|
// contain darwin and/or goarch doesn't contain amd64)
|
|
|
|
var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build")
|
|
|
|
|
2017-01-19 11:04:41 +02:00
|
|
|
const formula = `class {{ .Name }} < Formula
|
2016-12-29 13:58:22 +02:00
|
|
|
desc "{{ .Desc }}"
|
|
|
|
homepage "{{ .Homepage }}"
|
2017-03-23 02:01:29 +02:00
|
|
|
url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
|
2017-01-30 01:55:32 +02:00
|
|
|
version "{{ .Version }}"
|
2017-01-11 18:02:57 +02:00
|
|
|
sha256 "{{ .SHA256 }}"
|
2016-12-29 13:58:22 +02:00
|
|
|
|
2017-01-17 12:33:43 +02:00
|
|
|
{{- if .Dependencies }}
|
|
|
|
{{ range $index, $element := .Dependencies }}
|
2017-01-17 19:44:32 +02:00
|
|
|
depends_on "{{ . }}"
|
2017-01-17 12:33:43 +02:00
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
|
2017-02-01 19:40:27 +02:00
|
|
|
{{- if .Conflicts }}
|
|
|
|
{{ range $index, $element := .Conflicts }}
|
|
|
|
conflicts_with "{{ . }}"
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
|
2017-03-09 19:24:49 +02:00
|
|
|
def install
|
|
|
|
{{- range $index, $element := .Install }}
|
|
|
|
{{ . -}}
|
2017-03-09 20:33:45 +02:00
|
|
|
{{- end }}
|
2016-12-29 13:58:22 +02:00
|
|
|
end
|
2016-12-31 21:13:49 +02:00
|
|
|
|
2016-12-31 21:11:12 +02:00
|
|
|
{{- if .Caveats }}
|
2016-12-29 17:14:52 +02:00
|
|
|
|
2016-12-31 21:02:25 +02:00
|
|
|
def caveats
|
2016-12-29 17:14:52 +02:00
|
|
|
"{{ .Caveats }}"
|
2016-12-31 21:02:25 +02:00
|
|
|
end
|
2016-12-31 21:11:12 +02:00
|
|
|
{{- end }}
|
2017-02-21 17:04:57 +02:00
|
|
|
|
|
|
|
{{- if .Plist }}
|
|
|
|
|
|
|
|
def plist; <<-EOS.undent
|
|
|
|
{{ .Plist }}
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
{{- end }}
|
2016-12-29 13:58:22 +02:00
|
|
|
end
|
|
|
|
`
|
|
|
|
|
|
|
|
type templateData struct {
|
2017-01-17 12:33:43 +02:00
|
|
|
Name string
|
|
|
|
Desc string
|
|
|
|
Homepage string
|
2017-03-23 02:01:29 +02:00
|
|
|
Repo config.Repo // FIXME: will not work for anything but github right now.
|
2017-01-17 12:33:43 +02:00
|
|
|
Tag string
|
2017-01-30 01:55:32 +02:00
|
|
|
Version string
|
2017-03-23 02:06:37 +02:00
|
|
|
Binary string
|
2017-01-17 12:33:43 +02:00
|
|
|
Caveats string
|
|
|
|
File string
|
|
|
|
Format string
|
|
|
|
SHA256 string
|
2017-02-21 17:04:57 +02:00
|
|
|
Plist string
|
2017-03-09 19:24:49 +02:00
|
|
|
Install []string
|
2017-01-17 12:33:43 +02:00
|
|
|
Dependencies []string
|
2017-02-01 19:40:27 +02:00
|
|
|
Conflicts []string
|
2016-12-29 13:58:22 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Pipe for brew deployment
|
2016-12-30 13:27:35 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-01-14 20:41:32 +02:00
|
|
|
// Description of the pipe
|
2017-01-14 19:14:35 +02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 11:04:41 +02:00
|
|
|
return "Creating homebrew formula"
|
2016-12-30 13:27:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Run the pipe
|
2017-01-14 18:06:57 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-03-23 02:01:29 +02:00
|
|
|
// TODO: remove this block in next release cycle
|
|
|
|
if ctx.Config.Brew.Repo != "" {
|
|
|
|
log.Println("The `brew.repo` syntax is deprecated and will soon be removed. Please check the README for more info.")
|
|
|
|
var ss = strings.Split(ctx.Config.Brew.Repo, "/")
|
|
|
|
ctx.Config.Brew.GitHub = config.Repo{
|
|
|
|
Owner: ss[0],
|
|
|
|
Name: ss[1],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ctx.Config.Brew.GitHub.Name == "" {
|
2016-12-30 13:27:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-26 19:46:25 +02:00
|
|
|
client := clients.NewGitHubClient(ctx)
|
|
|
|
path := filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Build.Binary+".rb")
|
2016-12-29 13:58:22 +02:00
|
|
|
|
2017-03-23 02:01:29 +02:00
|
|
|
log.Println("Pushing", path, "to", ctx.Config.Brew.GitHub.String())
|
2017-03-26 19:46:25 +02:00
|
|
|
content, err := buildFormula(ctx, client)
|
2016-12-29 13:58:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-14 15:06:50 +02:00
|
|
|
|
2017-03-26 19:46:25 +02:00
|
|
|
return client.CreateFile(ctx, content, path)
|
2016-12-30 13:48:06 +02:00
|
|
|
}
|
2016-12-29 13:58:22 +02:00
|
|
|
|
2017-03-26 19:46:25 +02:00
|
|
|
func buildFormula(ctx *context.Context, client clients.Client) (bytes.Buffer, error) {
|
2017-01-14 18:06:57 +02:00
|
|
|
data, err := dataFor(ctx, client)
|
2016-12-30 13:48:06 +02:00
|
|
|
if err != nil {
|
2016-12-30 13:53:05 +02:00
|
|
|
return bytes.Buffer{}, err
|
2016-12-30 13:48:06 +02:00
|
|
|
}
|
2017-01-19 11:04:41 +02:00
|
|
|
return doBuildFormula(data)
|
2016-12-30 13:53:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 11:04:41 +02:00
|
|
|
func doBuildFormula(data templateData) (bytes.Buffer, error) {
|
2016-12-30 13:53:05 +02:00
|
|
|
var out bytes.Buffer
|
2017-03-23 02:06:37 +02:00
|
|
|
tmpl, err := template.New(data.Binary).Parse(formula)
|
2016-12-29 17:14:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&out, data)
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:46:25 +02:00
|
|
|
func dataFor(ctx *context.Context, client clients.Client) (result templateData, err error) {
|
2017-01-14 19:08:10 +02:00
|
|
|
file := ctx.Archives["darwinamd64"]
|
2017-01-16 18:52:27 +02:00
|
|
|
if file == "" {
|
|
|
|
return result, ErrNoDarwin64Build
|
|
|
|
}
|
2017-03-26 02:29:38 +02:00
|
|
|
sum, err := sha256sum.For(
|
|
|
|
filepath.Join(
|
2017-03-26 02:31:16 +02:00
|
|
|
ctx.Config.Dist,
|
2017-03-26 02:29:38 +02:00
|
|
|
file+"."+ctx.Config.Archive.Format,
|
|
|
|
),
|
|
|
|
)
|
2017-01-11 18:02:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2016-12-29 13:58:22 +02:00
|
|
|
}
|
2017-03-26 19:46:25 +02:00
|
|
|
homepage, description, err := getInfo(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2016-12-29 13:58:22 +02:00
|
|
|
}
|
|
|
|
return templateData{
|
2017-03-23 02:06:37 +02:00
|
|
|
Name: formulaNameFor(ctx.Config.Build.Binary),
|
2017-01-17 12:33:43 +02:00
|
|
|
Desc: description,
|
|
|
|
Homepage: homepage,
|
2017-03-23 02:01:29 +02:00
|
|
|
Repo: ctx.Config.Release.GitHub,
|
2017-01-17 12:33:43 +02:00
|
|
|
Tag: ctx.Git.CurrentTag,
|
2017-01-30 01:55:32 +02:00
|
|
|
Version: ctx.Version,
|
2017-03-23 02:06:37 +02:00
|
|
|
Binary: ctx.Config.Build.Binary,
|
2017-01-17 12:33:43 +02:00
|
|
|
Caveats: ctx.Config.Brew.Caveats,
|
|
|
|
File: file,
|
|
|
|
Format: ctx.Config.Archive.Format,
|
|
|
|
SHA256: sum,
|
|
|
|
Dependencies: ctx.Config.Brew.Dependencies,
|
2017-02-01 19:40:27 +02:00
|
|
|
Conflicts: ctx.Config.Brew.Conflicts,
|
2017-02-21 17:04:57 +02:00
|
|
|
Plist: ctx.Config.Brew.Plist,
|
2017-03-09 21:33:17 +02:00
|
|
|
Install: strings.Split(ctx.Config.Brew.Install, "\n"),
|
2016-12-29 13:58:22 +02:00
|
|
|
}, err
|
|
|
|
}
|
2016-12-29 14:55:35 +02:00
|
|
|
|
2017-03-26 19:46:25 +02:00
|
|
|
func getInfo(
|
|
|
|
ctx *context.Context,
|
|
|
|
client clients.Client,
|
|
|
|
) (homepage string, description string, err error) {
|
|
|
|
info, err := client.GetInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if info.Homepage != "" {
|
|
|
|
homepage = info.Homepage
|
|
|
|
} else {
|
|
|
|
homepage = info.URL
|
|
|
|
}
|
|
|
|
if info.Description == "" {
|
|
|
|
description = "TODO"
|
|
|
|
} else {
|
|
|
|
description = info.Description
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|