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

added caveats support

This commit is contained in:
Carlos Alexandro Becker 2016-12-29 13:14:52 -02:00
parent 8892860dc4
commit 2dd8bfa93d
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 65 additions and 13 deletions

View File

@ -5,8 +5,8 @@ import (
"context"
"crypto/sha256"
"fmt"
"text/template"
"strings"
"text/template"
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
@ -23,11 +23,17 @@ const formulae = `class {{ .Name }} < Formula
def install
bin.install "{{ .BinaryName }}"
end
{{ if .Caveats }}
def caveats
"{{ .Caveats }}"
end
{{ end }}
end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName string
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats string
}
func Brew(version string, config config.ProjectConfig) error {
@ -39,19 +45,15 @@ func Brew(version string, config config.ProjectConfig) error {
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)
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{},
@ -79,6 +81,16 @@ func Brew(version string, config config.ProjectConfig) error {
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(version string, config config.ProjectConfig, client *github.Client) (result templateData, err error) {
var homepage string
var description string
@ -104,6 +116,7 @@ func dataFor(version string, config config.ProjectConfig, client *github.Client)
Repo: config.Repo,
Tag: version,
BinaryName: config.BinaryName,
Caveats: config.Brew.Caveats,
}, err
}
@ -111,4 +124,4 @@ func formulaNameFor(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
return strings.Replace(strings.Title(name), " ", "", -1)
}
}

View File

@ -4,6 +4,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"os"
"io/ioutil"
)
func TestNameWithDash(t *testing.T) {
@ -17,3 +19,22 @@ func TestNameWithUnderline(t *testing.T) {
func TestSimpleName(t *testing.T) {
assert.Equal(t, formulaNameFor("binary"), "Binary")
}
func TestFormulae(t *testing.T) {
assert := assert.New(t)
out, err := buildFormulae(templateData{
BinaryName: "test",
Desc: "Some desc",
Homepage: "https://google.com",
Name: "Test",
Repo: "caarlos0/test",
Tag: "v0.1.3",
Caveats: "Here are some caveats",
})
assert.NoError(err)
f, err := os.Open("./brew/test_files/test.txt")
bts, _ := ioutil.ReadAll(f)
assert.NoError(err)
assert.Equal(string(bts), out.String())
}

17
brew/test_files/test.txt Normal file
View File

@ -0,0 +1,17 @@
class Test < Formula
desc "Some desc"
homepage "https://google.com"
url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}.tar.gz"
head "https://github.com/caarlos0/test.git"
version "v0.1.3"
def install
bin.install "test"
end
def caveats
"Here are some caveats"
end
end

View File

@ -11,8 +11,9 @@ import (
var emptyBrew = HomebrewDeploy{}
type HomebrewDeploy struct {
Repo string
Token string
Repo string
Token string
Caveats string
}
type BuildConfig struct {