From 9e58a6551972d7a204dbf75b647b7410c8849d32 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jan 2018 18:57:41 -0200 Subject: [PATCH] feat: support download strategy on brew closes #507 --- config/config.go | 25 +++++++------- pipeline/brew/brew.go | 32 +++++++++--------- pipeline/brew/brew_test.go | 17 ++++++++++ pipeline/brew/template.go | 32 +++++++++--------- .../run_pipe_download_strategy.rb.golden | 33 +++++++++++++++++++ 5 files changed, 97 insertions(+), 42 deletions(-) create mode 100644 pipeline/brew/testdata/run_pipe_download_strategy.rb.golden diff --git a/config/config.go b/config/config.go index 4b9b4bbd2..b68d2fc92 100644 --- a/config/config.go +++ b/config/config.go @@ -39,18 +39,19 @@ func (r Repo) String() string { // Homebrew contains the brew section type Homebrew struct { - GitHub Repo `yaml:",omitempty"` - CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"` - Folder string `yaml:",omitempty"` - Caveats string `yaml:",omitempty"` - Plist string `yaml:",omitempty"` - Install string `yaml:",omitempty"` - Dependencies []string `yaml:",omitempty"` - Test string `yaml:",omitempty"` - Conflicts []string `yaml:",omitempty"` - Description string `yaml:",omitempty"` - Homepage string `yaml:",omitempty"` - SkipUpload bool `yaml:"skip_upload,omitempty"` + GitHub Repo `yaml:",omitempty"` + CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"` + Folder string `yaml:",omitempty"` + Caveats string `yaml:",omitempty"` + Plist string `yaml:",omitempty"` + Install string `yaml:",omitempty"` + Dependencies []string `yaml:",omitempty"` + Test string `yaml:",omitempty"` + Conflicts []string `yaml:",omitempty"` + Description string `yaml:",omitempty"` + Homepage string `yaml:",omitempty"` + SkipUpload bool `yaml:"skip_upload,omitempty"` + DownloadStrategy string `yaml:"download_strategy,omitempty"` // Capture all undefined fields and should be empty after loading XXX map[string]interface{} `yaml:",inline"` diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 21a8315d9..f03511836 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -162,22 +162,24 @@ func dataFor(ctx *context.Context, client client.Client, artifact artifact.Artif if ctx.Config.GitHubURLs.Download != "" { url = ctx.Config.GitHubURLs.Download } + var cfg = ctx.Config.Brew return templateData{ - Name: formulaNameFor(ctx.Config.ProjectName), - DownloadURL: url, - Desc: ctx.Config.Brew.Description, - Homepage: ctx.Config.Brew.Homepage, - Repo: ctx.Config.Release.GitHub, - Tag: ctx.Git.CurrentTag, - Version: ctx.Version, - Caveats: ctx.Config.Brew.Caveats, - File: artifact.Name, - SHA256: sum, - Dependencies: ctx.Config.Brew.Dependencies, - Conflicts: ctx.Config.Brew.Conflicts, - Plist: ctx.Config.Brew.Plist, - Install: split(ctx.Config.Brew.Install), - Tests: split(ctx.Config.Brew.Test), + Name: formulaNameFor(ctx.Config.ProjectName), + DownloadURL: url, + Desc: cfg.Description, + Homepage: cfg.Homepage, + Repo: ctx.Config.Release.GitHub, + Tag: ctx.Git.CurrentTag, + Version: ctx.Version, + Caveats: cfg.Caveats, + File: artifact.Name, + SHA256: sum, + Dependencies: cfg.Dependencies, + Conflicts: cfg.Conflicts, + Plist: cfg.Plist, + Install: split(cfg.Install), + Tests: split(cfg.Test), + DownloadStrategy: cfg.DownloadStrategy, }, nil } diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index 2354e2831..0bde35ce4 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -179,6 +179,23 @@ func TestRunPipe(t *testing.T) { assert.NoError(tt, err) assert.Equal(tt, string(bts), string(distBts)) }) + + t.Run("custom download strategy", func(tt *testing.T) { + ctx.Config.Brew.DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy" + assert.NoError(tt, doRun(ctx, client)) + assert.True(tt, client.CreatedFile) + var golden = "testdata/run_pipe_download_strategy.rb.golden" + if *update { + ioutil.WriteFile(golden, []byte(client.Content), 0655) + } + bts, err := ioutil.ReadFile(golden) + assert.NoError(tt, err) + assert.Equal(tt, string(bts), client.Content) + + distBts, err := ioutil.ReadFile(distFile) + assert.NoError(tt, err) + assert.Equal(tt, string(bts), string(distBts)) + }) } func TestRunPipeNoDarwin64Build(t *testing.T) { diff --git a/pipeline/brew/template.go b/pipeline/brew/template.go index 6723cd70d..a70ff4c82 100644 --- a/pipeline/brew/template.go +++ b/pipeline/brew/template.go @@ -3,27 +3,29 @@ package brew import "github.com/goreleaser/goreleaser/config" type templateData struct { - Name string - Desc string - Homepage string - DownloadURL string - Repo config.Repo // FIXME: will not work for anything but github right now. - Tag string - Version string - Caveats string - File string - SHA256 string - Plist string - Install []string - Dependencies []string - Conflicts []string - Tests []string + Name string + Desc string + Homepage string + DownloadURL string + Repo config.Repo // FIXME: will not work for anything but github right now. + Tag string + Version string + Caveats string + File string + SHA256 string + Plist string + DownloadStrategy string + Install []string + Dependencies []string + Conflicts []string + Tests []string } const formulaTemplate = `class {{ .Name }} < Formula desc "{{ .Desc }}" homepage "{{ .Homepage }}" url "{{ .DownloadURL }}/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}" + {{- if .DownloadStrategy }}, :using => {{ .DownloadStrategy }}{{- end }} version "{{ .Version }}" sha256 "{{ .SHA256 }}" diff --git a/pipeline/brew/testdata/run_pipe_download_strategy.rb.golden b/pipeline/brew/testdata/run_pipe_download_strategy.rb.golden new file mode 100644 index 000000000..c57407b76 --- /dev/null +++ b/pipeline/brew/testdata/run_pipe_download_strategy.rb.golden @@ -0,0 +1,33 @@ +class RunPipe < Formula + desc "A run pipe test formula" + homepage "https://github.com/goreleaser" + url "http://github.example.org/test/test/releases/download/v1.0.1/bin.tar.gz", :using => GitHubPrivateRepositoryReleaseDownloadStrategy + version "1.0.1" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + depends_on "zsh" + depends_on "bash" + + conflicts_with "gtk+" + conflicts_with "qt" + + def install + bin.install "foo" + end + + def caveats + "don't do this" + end + + plist_options :startup => false + + def plist; <<-EOS.undent + whatever + EOS + end + + test do + system "true" + system "#{bin}/foo -h" + end +end