From ef6e13a61beaacac20a857d9ba8b62f6266b5092 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 26 Jun 2019 14:12:33 -0300 Subject: [PATCH] fix: brew tmpl (#1057) * fix: brew tmpl Signed-off-by: Carlos Alexandro Becker * fix: compile Signed-off-by: Carlos Alexandro Becker * fix: compile Signed-off-by: Carlos Alexandro Becker --- internal/client/client.go | 3 +-- internal/client/github.go | 5 ++-- internal/pipe/brew/brew.go | 21 +++++++++------- internal/pipe/brew/brew_test.go | 25 ++++++++++--------- .../pipe/brew/testdata/custom_block.rb.golden | 6 ++--- .../custom_download_strategy.rb.golden | 6 ++--- .../brew/testdata/custom_require.rb.golden | 6 ++--- internal/pipe/brew/testdata/default.rb.golden | 6 ++--- .../testdata/github_enterprise_url.rb.golden | 6 ++--- internal/pipe/release/release_test.go | 3 +-- internal/pipe/scoop/scoop.go | 2 +- internal/pipe/scoop/scoop_test.go | 6 ++--- 12 files changed, 47 insertions(+), 48 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 25984e9c1..c81075402 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -2,7 +2,6 @@ package client import ( - "bytes" "os" "github.com/goreleaser/goreleaser/pkg/config" @@ -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) } diff --git a/internal/client/github.go b/internal/client/github.go index e3a39a24e..ba22f41b7 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -1,7 +1,6 @@ package client import ( - "bytes" "crypto/tls" "net/http" "net/url" @@ -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 { @@ -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), } diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go index 1e6e5731f..9742a1b9b 100644 --- a/internal/pipe/brew/brew.go +++ b/internal/pipe/brew/brew.go @@ -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 } @@ -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) { diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go index 340b6fc57..6c13d05d3 100644 --- a/internal/pipe/brew/brew_test.go +++ b/internal/pipe/brew/brew_test.go @@ -1,7 +1,6 @@ package brew import ( - "bytes" "flag" "fmt" "io/ioutil" @@ -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 { @@ -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") @@ -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, @@ -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: `whatever`, Dependencies: []string{"zsh", "bash"}, Conflicts: []string{"gtk+", "qt"}, - Install: `bin.install "foo"`, + Install: `bin.install "{{ .ProjectName }}"`, }, }, }, @@ -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 } diff --git a/internal/pipe/brew/testdata/custom_block.rb.golden b/internal/pipe/brew/testdata/custom_block.rb.golden index bc3d8fb7e..802c60377 100644 --- a/internal/pipe/brew/testdata/custom_block.rb.golden +++ b/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" @@ -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 diff --git a/internal/pipe/brew/testdata/custom_download_strategy.rb.golden b/internal/pipe/brew/testdata/custom_download_strategy.rb.golden index eb4d2a28f..38a8c16cd 100644 --- a/internal/pipe/brew/testdata/custom_download_strategy.rb.golden +++ b/internal/pipe/brew/testdata/custom_download_strategy.rb.golden @@ -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" @@ -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 diff --git a/internal/pipe/brew/testdata/custom_require.rb.golden b/internal/pipe/brew/testdata/custom_require.rb.golden index f24610bab..156948951 100644 --- a/internal/pipe/brew/testdata/custom_require.rb.golden +++ b/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" @@ -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 diff --git a/internal/pipe/brew/testdata/default.rb.golden b/internal/pipe/brew/testdata/default.rb.golden index de7e8eb12..d2942fec5 100644 --- a/internal/pipe/brew/testdata/default.rb.golden +++ b/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" @@ -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 diff --git a/internal/pipe/brew/testdata/github_enterprise_url.rb.golden b/internal/pipe/brew/testdata/github_enterprise_url.rb.golden index b67e36b0a..605394ceb 100644 --- a/internal/pipe/brew/testdata/github_enterprise_url.rb.golden +++ b/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" @@ -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 diff --git a/internal/pipe/release/release_test.go b/internal/pipe/release/release_test.go index b81273e30..b3910ee71 100644 --- a/internal/pipe/release/release_test.go +++ b/internal/pipe/release/release_test.go @@ -1,7 +1,6 @@ package release import ( - "bytes" "io/ioutil" "os" "path/filepath" @@ -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 } diff --git a/internal/pipe/scoop/scoop.go b/internal/pipe/scoop/scoop.go index f91976a78..d58f78fbf 100644 --- a/internal/pipe/scoop/scoop.go +++ b/internal/pipe/scoop/scoop.go @@ -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), ) diff --git a/internal/pipe/scoop/scoop_test.go b/internal/pipe/scoop/scoop_test.go index 954dc9067..332cf3afe 100644 --- a/internal/pipe/scoop/scoop_test.go +++ b/internal/pipe/scoop/scoop_test.go @@ -1,7 +1,6 @@ package scoop import ( - "bytes" "flag" "io/ioutil" "os" @@ -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 }