From 541e3dfed9b23e277948ea641db91ea468aafc88 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 25 Nov 2022 09:32:43 -0300 Subject: [PATCH] feat(brew): allow to template brew.install (#3593) --- internal/pipe/brew/brew.go | 22 +++-- internal/pipe/brew/brew_test.go | 87 ++++++++++++++++--- .../TestFullPipe/custom_block.rb.golden | 34 +++++--- .../custom_download_strategy.rb.golden | 34 +++++--- .../TestFullPipe/custom_require.rb.golden | 34 +++++--- .../testdata/TestFullPipe/default.rb.golden | 34 +++++--- .../TestFullPipe/default_gitlab.rb.golden | 34 +++++--- .../valid_tap_templates.rb.golden | 34 +++++--- 8 files changed, 219 insertions(+), 94 deletions(-) diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go index 85b497a585f..85f5f3c50dc 100644 --- a/internal/pipe/brew/brew.go +++ b/internal/pipe/brew/brew.go @@ -247,9 +247,6 @@ func buildFormula(ctx *context.Context, brew config.Homebrew, client client.Clie func doBuildFormula(ctx *context.Context, data templateData) (string, error) { t, err := template. New(data.Name). - Funcs(template.FuncMap{ - "join": strings.Join, - }). Parse(formulaTemplate) if err != nil { return "", err @@ -282,9 +279,13 @@ func doBuildFormula(ctx *context.Context, data templateData) (string, error) { return out.String(), nil } -func installs(cfg config.Homebrew, art *artifact.Artifact) []string { - if cfg.Install != "" { - return split(cfg.Install) +func installs(ctx *context.Context, cfg config.Homebrew, art *artifact.Artifact) ([]string, error) { + applied, err := tmpl.New(ctx).WithArtifact(art, nil).Apply(cfg.Install) + if err != nil { + return nil, err + } + if applied != "" { + return split(applied), nil } install := map[string]bool{} @@ -302,7 +303,7 @@ func installs(cfg config.Homebrew, art *artifact.Artifact) []string { result := keys(install) sort.Strings(result) log.Warnf("guessing install to be %q", strings.Join(result, ", ")) - return result + return result, nil } func keys(m map[string]bool) []string { @@ -351,13 +352,18 @@ func dataFor(ctx *context.Context, cfg config.Homebrew, cl client.Client, artifa return result, err } + install, err := installs(ctx, cfg, art) + if err != nil { + return result, err + } + pkg := releasePackage{ DownloadURL: url, SHA256: sum, OS: art.Goos, Arch: art.Goarch, DownloadStrategy: cfg.DownloadStrategy, - Install: installs(cfg, art), + Install: install, } counts[pkg.OS+pkg.Arch]++ diff --git a/internal/pipe/brew/brew_test.go b/internal/pipe/brew/brew_test.go index 38ea0064a3e..fc94b42cad4 100644 --- a/internal/pipe/brew/brew_test.go +++ b/internal/pipe/brew/brew_test.go @@ -247,6 +247,14 @@ func TestFullPipe(t *testing.T) { }, expectedRunError: `template: tmpl:1: unexpected "}" in operand`, }, + "invalid_install_template": { + prepare: func(ctx *context.Context) { + ctx.Config.Brews[0].Tap.Owner = "test" + ctx.Config.Brews[0].Tap.Name = "test" + ctx.Config.Brews[0].Install = "{{ .aaaa }" + }, + expectedRunError: `template: tmpl:1: unexpected "}" in operand`, + }, } { t.Run(name, func(t *testing.T) { folder := t.TempDir() @@ -280,7 +288,7 @@ func TestFullPipe(t *testing.T) { Conflicts: []string{"gtk+", "qt"}, Service: "run foo/bar\nkeep_alive true", PostInstall: "system \"echo\"\ntouch \"/tmp/hi\"", - Install: `bin.install "{{ .ProjectName }}"`, + Install: `bin.install "{{ .ProjectName }}_{{.Os}}_{{.Arch}} => {{.ProjectName}}"`, Goamd64: "v1", }, }, @@ -312,6 +320,29 @@ func TestFullPipe(t *testing.T) { artifact.ExtraFormat: "tar.gz", }, }) + ctx.Artifacts.Add(&artifact.Artifact{ + Name: "bin.tar.gz", + Path: path, + Goos: "darwin", + Goarch: "arm64", + Type: artifact.UploadableArchive, + Extra: map[string]interface{}{ + artifact.ExtraID: "foo", + artifact.ExtraFormat: "tar.gz", + }, + }) + ctx.Artifacts.Add(&artifact.Artifact{ + Name: "bin.tar.gz", + Path: path, + Goos: "linux", + Goarch: "amd64", + Goamd64: "v1", + Type: artifact.UploadableArchive, + Extra: map[string]interface{}{ + artifact.ExtraID: "foo", + artifact.ExtraFormat: "tar.gz", + }, + }) f, err := os.Create(path) require.NoError(t, err) @@ -1116,20 +1147,22 @@ func TestRunSkipNoName(t *testing.T) { func TestInstalls(t *testing.T) { t.Run("provided", func(t *testing.T) { + install, err := installs( + context.New(config.Project{}), + config.Homebrew{Install: "bin.install \"foo\"\nbin.install \"bar\""}, + &artifact.Artifact{}, + ) + require.NoError(t, err) require.Equal(t, []string{ `bin.install "foo"`, `bin.install "bar"`, - }, installs( - config.Homebrew{Install: "bin.install \"foo\"\nbin.install \"bar\""}, - &artifact.Artifact{}, - )) + }, install) }) t.Run("from archives", func(t *testing.T) { - require.Equal(t, []string{ - `bin.install "bar"`, - `bin.install "foo"`, - }, installs( + install, err := installs( + context.New(config.Project{}), + config.Homebrew{}, &artifact.Artifact{ Type: artifact.UploadableArchive, @@ -1137,13 +1170,17 @@ func TestInstalls(t *testing.T) { artifact.ExtraBinaries: []string{"foo", "bar"}, }, }, - )) + ) + require.NoError(t, err) + require.Equal(t, []string{ + `bin.install "bar"`, + `bin.install "foo"`, + }, install) }) t.Run("from binary", func(t *testing.T) { - require.Equal(t, []string{ - `bin.install "foo_macos" => "foo"`, - }, installs( + install, err := installs( + context.New(config.Project{}), config.Homebrew{}, &artifact.Artifact{ Name: "foo_macos", @@ -1152,7 +1189,29 @@ func TestInstalls(t *testing.T) { artifact.ExtraBinary: "foo", }, }, - )) + ) + require.NoError(t, err) + require.Equal(t, []string{ + `bin.install "foo_macos" => "foo"`, + }, install) + }) + + t.Run("from template", func(t *testing.T) { + install, err := installs( + context.New(config.Project{}), + config.Homebrew{ + Install: `bin.install "foo_{{.Os}}" => "foo"`, + }, + &artifact.Artifact{ + Name: "foo_darwin", + Goos: "darwin", + Type: artifact.UploadableBinary, + }, + ) + require.NoError(t, err) + require.Equal(t, []string{ + `bin.install "foo_darwin" => "foo"`, + }, install) }) } diff --git a/internal/pipe/brew/testdata/TestFullPipe/custom_block.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/custom_block.rb.golden index 5bef8efc042..f1184a21a0d 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/custom_block.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/custom_block.rb.golden @@ -10,23 +10,33 @@ class CustomBlock < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "custom_block" + def install + bin.install "custom_block_darwin_amd64 => custom_block" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the CustomBlock - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_block_darwin_arm64 => custom_block" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_block_linux_amd64 => custom_block" end end end diff --git a/internal/pipe/brew/testdata/TestFullPipe/custom_download_strategy.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/custom_download_strategy.rb.golden index 90525b1f195..2dd74674612 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/custom_download_strategy.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/custom_download_strategy.rb.golden @@ -10,23 +10,33 @@ class CustomDownloadStrategy < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: GitHubPrivateRepositoryReleaseDownloadStrategy - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: GitHubPrivateRepositoryReleaseDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "custom_download_strategy" + def install + bin.install "custom_download_strategy_darwin_amd64 => custom_download_strategy" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the CustomDownloadStrategy - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: GitHubPrivateRepositoryReleaseDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_download_strategy_darwin_arm64 => custom_download_strategy" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: GitHubPrivateRepositoryReleaseDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_download_strategy_linux_amd64 => custom_download_strategy" end end end diff --git a/internal/pipe/brew/testdata/TestFullPipe/custom_require.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/custom_require.rb.golden index 6dee341f2aa..ab512b6427f 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/custom_require.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/custom_require.rb.golden @@ -11,23 +11,33 @@ class CustomRequire < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: CustomDownloadStrategy - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: CustomDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "custom_require" + def install + bin.install "custom_require_darwin_amd64 => custom_require" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the CustomRequire - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: CustomDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_require_darwin_arm64 => custom_require" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz", using: CustomDownloadStrategy + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "custom_require_linux_amd64 => custom_require" end end end diff --git a/internal/pipe/brew/testdata/TestFullPipe/default.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/default.rb.golden index d8d4b479ba6..35e3f9d7be0 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/default.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/default.rb.golden @@ -10,23 +10,33 @@ class Default < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "default" + def install + bin.install "default_darwin_amd64 => default" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the Default - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "default_darwin_arm64 => default" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "default_linux_amd64 => default" end end end diff --git a/internal/pipe/brew/testdata/TestFullPipe/default_gitlab.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/default_gitlab.rb.golden index 23dc20da5d4..76d873757a5 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/default_gitlab.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/default_gitlab.rb.golden @@ -10,23 +10,33 @@ class DefaultGitlab < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "default_gitlab" + def install + bin.install "default_gitlab_darwin_amd64 => default_gitlab" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the DefaultGitlab - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "default_gitlab_darwin_arm64 => default_gitlab" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "default_gitlab_linux_amd64 => default_gitlab" end end end diff --git a/internal/pipe/brew/testdata/TestFullPipe/valid_tap_templates.rb.golden b/internal/pipe/brew/testdata/TestFullPipe/valid_tap_templates.rb.golden index 28a01583e89..a87974ae17a 100644 --- a/internal/pipe/brew/testdata/TestFullPipe/valid_tap_templates.rb.golden +++ b/internal/pipe/brew/testdata/TestFullPipe/valid_tap_templates.rb.golden @@ -10,23 +10,33 @@ class ValidTapTemplates < Formula depends_on "zsh" => :optional depends_on "bash" => "3.2.57" depends_on "fish" => :optional - depends_on :macos on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "valid_tap_templates" + def install + bin.install "valid_tap_templates_darwin_amd64 => valid_tap_templates" + end end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the ValidTapTemplates - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "valid_tap_templates_darwin_arm64 => valid_tap_templates" + end + end + end + + on_linux do + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + + def install + bin.install "valid_tap_templates_linux_amd64 => valid_tap_templates" end end end