Skip to content

Commit

Permalink
fix: brew tmpl (#1057)
Browse files Browse the repository at this point in the history
* fix: brew tmpl

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: compile

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: compile

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Jun 26, 2019
1 parent c9546ec commit ef6e13a
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 48 deletions.
3 changes: 1 addition & 2 deletions internal/client/client.go
Expand Up @@ -2,7 +2,6 @@
package client

import (
"bytes"
"os"

"github.com/goreleaser/goreleaser/pkg/config"
Expand All @@ -19,6 +18,6 @@ type Info struct {
// Client interface
type Client interface {
CreateRelease(ctx *context.Context, body string) (releaseID int64, err error)
CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, message string) (err error)
CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, message string) (err error)
Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error)
}
5 changes: 2 additions & 3 deletions internal/client/github.go
@@ -1,7 +1,6 @@
package client

import (
"bytes"
"crypto/tls"
"net/http"
"net/url"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (c *githubClient) CreateFile(
ctx *context.Context,
commitAuthor config.CommitAuthor,
repo config.Repo,
content bytes.Buffer,
content []byte,
path,
message string,
) error {
Expand All @@ -65,7 +64,7 @@ func (c *githubClient) CreateFile(
Name: github.String(commitAuthor.Name),
Email: github.String(commitAuthor.Email),
},
Content: content.Bytes(),
Content: content,
Message: github.String(message),
}

Expand Down
21 changes: 12 additions & 9 deletions internal/pipe/brew/brew.go
Expand Up @@ -141,7 +141,7 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
var filename = brew.Name + ".rb"
var path = filepath.Join(ctx.Config.Dist, filename)
log.WithField("formula", path).Info("writing")
if err := ioutil.WriteFile(path, content.Bytes(), 0644); err != nil {
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
return err
}

Expand All @@ -164,28 +164,31 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
Info("pushing")

var msg = fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
return client.CreateFile(ctx, brew.CommitAuthor, brew.GitHub, content, gpath, msg)
return client.CreateFile(ctx, brew.CommitAuthor, brew.GitHub, []byte(content), gpath, msg)
}

func ghFormulaPath(folder, filename string) string {
return path.Join(folder, filename)
}

func buildFormula(ctx *context.Context, brew config.Homebrew, artifacts []artifact.Artifact) (bytes.Buffer, error) {
func buildFormula(ctx *context.Context, brew config.Homebrew, artifacts []artifact.Artifact) (string, error) {
data, err := dataFor(ctx, brew, artifacts)
if err != nil {
return bytes.Buffer{}, err
return "", err
}
return doBuildFormula(data)
return doBuildFormula(ctx, data)
}

func doBuildFormula(data templateData) (out bytes.Buffer, err error) {
func doBuildFormula(ctx *context.Context, data templateData) (string, error) {
t, err := template.New(data.Name).Parse(formulaTemplate)
if err != nil {
return out, err
return "", err
}
err = t.Execute(&out, data)
return
var out bytes.Buffer
if err := t.Execute(&out, data); err != nil {
return "", err
}
return tmpl.New(ctx).Apply(out.String())
}

func dataFor(ctx *context.Context, cfg config.Homebrew, artifacts []artifact.Artifact) (templateData, error) {
Expand Down
25 changes: 13 additions & 12 deletions internal/pipe/brew/brew_test.go
@@ -1,7 +1,6 @@
package brew

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -66,10 +65,11 @@ func TestFullFormulae(t *testing.T) {
data.Plist = "it works"
data.CustomBlock = []string{"devel do", ` url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`, ` sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`, "end"}
data.Install = []string{"custom install script", "another install script"}
data.Tests = []string{`system "#{bin}/foo -version"`}
out, err := doBuildFormula(data)
data.Tests = []string{`system "#{bin}/{{.ProjectName}} -version"`}
formulae, err := doBuildFormula(context.New(config.Project{
ProjectName: "foo",
}), data)
assert.NoError(t, err)
formulae := out.String()

var golden = "testdata/test.rb.golden"
if *update {
Expand All @@ -82,9 +82,8 @@ func TestFullFormulae(t *testing.T) {
}

func TestFormulaeSimple(t *testing.T) {
out, err := doBuildFormula(defaultTemplateData)
formulae, err := doBuildFormula(context.New(config.Project{}), defaultTemplateData)
assert.NoError(t, err)
formulae := out.String()
assertDefaultTemplateData(t, formulae)
assert.NotContains(t, formulae, "def caveats")
assert.NotContains(t, formulae, "depends_on")
Expand Down Expand Up @@ -126,6 +125,9 @@ func TestRunPipe(t *testing.T) {
},
Version: "1.0.1",
Artifacts: artifact.New(),
Env: map[string]string{
"FOO": "foo_is_bar",
},
Config: config.Project{
Dist: folder,
ProjectName: name,
Expand All @@ -148,14 +150,14 @@ func TestRunPipe(t *testing.T) {
IDs: []string{
"foo",
},
Description: "A run pipe test formula",
Description: "A run pipe test formula and FOO={{ .Env.FOO }}",
Homepage: "https://github.com/goreleaser",
Caveats: "don't do this",
Caveats: "don't do this {{ .ProjectName }}",
Test: "system \"true\"\nsystem \"#{bin}/foo -h\"",
Plist: `<xml>whatever</xml>`,
Dependencies: []string{"zsh", "bash"},
Conflicts: []string{"gtk+", "qt"},
Install: `bin.install "foo"`,
Install: `bin.install "{{ .ProjectName }}"`,
},
},
},
Expand Down Expand Up @@ -423,10 +425,9 @@ func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (rel
return
}

func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
client.CreatedFile = true
bts, _ := ioutil.ReadAll(&content)
client.Content = string(bts)
client.Content = string(content)
return
}

Expand Down
6 changes: 3 additions & 3 deletions internal/pipe/brew/testdata/custom_block.rb.golden
@@ -1,6 +1,6 @@
# This file was generated by GoReleaser. DO NOT EDIT.
class CustomBlock < Formula
desc "A run pipe test formula"
desc "A run pipe test formula and FOO=foo_is_bar"
homepage "https://github.com/goreleaser"
version "1.0.1"

Expand All @@ -19,11 +19,11 @@ class CustomBlock < Formula
conflicts_with "qt"

def install
bin.install "foo"
bin.install "custom_block"
end

def caveats; <<~EOS
don't do this
don't do this custom_block
EOS
end

Expand Down
@@ -1,6 +1,6 @@
# This file was generated by GoReleaser. DO NOT EDIT.
class CustomDownloadStrategy < Formula
desc "A run pipe test formula"
desc "A run pipe test formula and FOO=foo_is_bar"
homepage "https://github.com/goreleaser"
version "1.0.1"

Expand All @@ -17,11 +17,11 @@ class CustomDownloadStrategy < Formula
conflicts_with "qt"

def install
bin.install "foo"
bin.install "custom_download_strategy"
end

def caveats; <<~EOS
don't do this
don't do this custom_download_strategy
EOS
end

Expand Down
6 changes: 3 additions & 3 deletions internal/pipe/brew/testdata/custom_require.rb.golden
@@ -1,7 +1,7 @@
# This file was generated by GoReleaser. DO NOT EDIT.
require_relative "custom_download_strategy"
class CustomRequire < Formula
desc "A run pipe test formula"
desc "A run pipe test formula and FOO=foo_is_bar"
homepage "https://github.com/goreleaser"
version "1.0.1"

Expand All @@ -18,11 +18,11 @@ class CustomRequire < Formula
conflicts_with "qt"

def install
bin.install "foo"
bin.install "custom_require"
end

def caveats; <<~EOS
don't do this
don't do this custom_require
EOS
end

Expand Down
6 changes: 3 additions & 3 deletions internal/pipe/brew/testdata/default.rb.golden
@@ -1,6 +1,6 @@
# This file was generated by GoReleaser. DO NOT EDIT.
class Default < Formula
desc "A run pipe test formula"
desc "A run pipe test formula and FOO=foo_is_bar"
homepage "https://github.com/goreleaser"
version "1.0.1"

Expand All @@ -17,11 +17,11 @@ class Default < Formula
conflicts_with "qt"

def install
bin.install "foo"
bin.install "default"
end

def caveats; <<~EOS
don't do this
don't do this default
EOS
end

Expand Down
6 changes: 3 additions & 3 deletions internal/pipe/brew/testdata/github_enterprise_url.rb.golden
@@ -1,6 +1,6 @@
# This file was generated by GoReleaser. DO NOT EDIT.
class GithubEnterpriseUrl < Formula
desc "A run pipe test formula"
desc "A run pipe test formula and FOO=foo_is_bar"
homepage "https://github.com/goreleaser"
version "1.0.1"

Expand All @@ -17,11 +17,11 @@ class GithubEnterpriseUrl < Formula
conflicts_with "qt"

def install
bin.install "foo"
bin.install "github_enterprise_url"
end

def caveats; <<~EOS
don't do this
don't do this github_enterprise_url
EOS
end

Expand Down
3 changes: 1 addition & 2 deletions internal/pipe/release/release_test.go
@@ -1,7 +1,6 @@
package release

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -332,7 +331,7 @@ func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (rel
return
}

func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/scoop/scoop.go
Expand Up @@ -90,7 +90,7 @@ func doRun(ctx *context.Context, client client.Client) error {
ctx,
ctx.Config.Scoop.CommitAuthor,
ctx.Config.Scoop.Bucket,
content,
content.Bytes(),
path,
fmt.Sprintf("Scoop update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag),
)
Expand Down
6 changes: 2 additions & 4 deletions internal/pipe/scoop/scoop_test.go
@@ -1,7 +1,6 @@
package scoop

import (
"bytes"
"flag"
"io/ioutil"
"os"
Expand Down Expand Up @@ -535,10 +534,9 @@ func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (rel
return
}

func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
client.CreatedFile = true
bts, _ := ioutil.ReadAll(&content)
client.Content = string(bts)
client.Content = string(content)
return
}

Expand Down

0 comments on commit ef6e13a

Please sign in to comment.