From ac398de727e20033f9d36ad0823419dad22fa512 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 19 Jan 2024 12:41:28 -0300 Subject: [PATCH] fix(brew): improve handling of single os (#4562) - refactors brew template into separated files using embed.FS - moves the macos and linux packages to different template files - includes and indent those accordingly to which OSes are supported closes #4561 Signed-off-by: Carlos Alexandro Becker --- .editorconfig | 2 +- internal/pipe/brew/brew.go | 18 +- internal/pipe/brew/template.go | 183 +----------------- internal/pipe/brew/templates/cask.rb | 109 +++++++++++ .../pipe/brew/templates/linux_packages.rb | 23 +++ .../pipe/brew/templates/macos_packages.rb | 52 +++++ .../TestFullFormulaeLinuxOnly.rb.golden | 36 ++-- .../TestFullFormulaeMacOSOnly.rb.golden | 24 ++- .../TestRunPipeBinaryRelease.rb.golden | 12 +- .../TestRunPipeNameTemplate.rb.golden | 26 ++- .../testdata/TestRunPipePullRequest.rb.golden | 12 +- .../TestRunPipeUniversalBinary.rb.golden | 10 +- ...nPipeUniversalBinaryNotReplacing.rb.golden | 24 ++- 13 files changed, 272 insertions(+), 259 deletions(-) create mode 100644 internal/pipe/brew/templates/cask.rb create mode 100644 internal/pipe/brew/templates/linux_packages.rb create mode 100644 internal/pipe/brew/templates/macos_packages.rb diff --git a/.editorconfig b/.editorconfig index cedad1af4b2..88ffcb692be 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,6 @@ trim_trailing_whitespace = true insert_final_newline = true charset = utf-8 -[*.{md,yml,yaml}] +[*.{md,yml,yam,rbl}] indent_size = 2 indent_style = space diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go index b8b31e20e7d..4e99f3e1a42 100644 --- a/internal/pipe/brew/brew.go +++ b/internal/pipe/brew/brew.go @@ -296,9 +296,21 @@ func buildFormula(ctx *context.Context, brew config.Homebrew, client client.Rele } func doBuildFormula(ctx *context.Context, data templateData) (string, error) { - t, err := template. - New(data.Name). - Parse(formulaTemplate) + t := template.New("cask.rb") + var err error + t, err = t.Funcs(map[string]any{ + "include": func(name string, data interface{}) (string, error) { + buf := bytes.NewBuffer(nil) + if err := t.ExecuteTemplate(buf, name, data); err != nil { + return "", err + } + return buf.String(), nil + }, + "indent": func(spaces int, v string) string { + pad := strings.Repeat(" ", spaces) + return pad + strings.ReplaceAll(v, "\n", "\n"+pad) + }, + }).ParseFS(formulaTemplate, "templates/*.rb") if err != nil { return "", err } diff --git a/internal/pipe/brew/template.go b/internal/pipe/brew/template.go index 0e41847524c..1ba2db2323f 100644 --- a/internal/pipe/brew/template.go +++ b/internal/pipe/brew/template.go @@ -1,6 +1,10 @@ package brew -import "github.com/goreleaser/goreleaser/pkg/config" +import ( + "embed" + + "github.com/goreleaser/goreleaser/pkg/config" +) type templateData struct { Name string @@ -31,178 +35,5 @@ type releasePackage struct { Install []string } -const formulaTemplate = `# typed: false -# frozen_string_literal: true - -# This file was generated by GoReleaser. DO NOT EDIT. -{{ if .CustomRequire -}} -require_relative "{{ .CustomRequire }}" -{{ end -}} -class {{ .Name }} < Formula - desc "{{ .Desc }}" - homepage "{{ .Homepage }}" - version "{{ .Version }}" - {{- if .License }} - license "{{ .License }}" - {{- end }} - {{- with .Dependencies }} - {{ range $index, $element := . }} - depends_on "{{ .Name }}" - {{- if .Type }} => :{{ .Type }}{{- else if .Version }} => "{{ .Version }}"{{- end }} - {{- with .OS }} if OS.{{ . }}?{{- end }} - {{- end }} - {{- end -}} - - {{- if and (not .LinuxPackages) .MacOSPackages }} - depends_on :macos - {{- end }} - {{- if and (not .MacOSPackages) .LinuxPackages }} - depends_on :linux - {{- end }} - {{- printf "\n" }} - - {{- if .MacOSPackages }} - on_macos do - {{- range $element := .MacOSPackages }} - {{- if eq $element.Arch "all" }} - url "{{ $element.DownloadURL }}" - {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} - sha256 "{{ $element.SHA256 }}" - - def install - {{- range $index, $element := .Install }} - {{ . -}} - {{- end }} - end - {{- else if $.HasOnlyAmd64MacOsPkg }} - url "{{ $element.DownloadURL }}" - {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} - sha256 "{{ $element.SHA256 }}" - - def install - {{- range $index, $element := .Install }} - {{ . -}} - {{- end }} - end - - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the {{ $.Name }} - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS - end - end - {{- else }} - {{- if eq $element.Arch "amd64" }} - if Hardware::CPU.intel? - {{- end }} - {{- if eq $element.Arch "arm64" }} - if Hardware::CPU.arm? - {{- end}} - url "{{ $element.DownloadURL }}" - {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} - sha256 "{{ $element.SHA256 }}" - - def install - {{- range $index, $element := .Install }} - {{ . -}} - {{- end }} - end - end - {{- end }} - {{- end }} - end - {{- end }} - - {{- if and .MacOSPackages .LinuxPackages }}{{ printf "\n" }}{{ end }} - - {{- if .LinuxPackages }} - on_linux do - {{- range $element := .LinuxPackages }} - {{- if eq $element.Arch "amd64" }} - if Hardware::CPU.intel? - {{- end }} - {{- if eq $element.Arch "arm" }} - if Hardware::CPU.arm? && !Hardware::CPU.is_64_bit? - {{- end }} - {{- if eq $element.Arch "arm64" }} - if Hardware::CPU.arm? && Hardware::CPU.is_64_bit? - {{- end }} - url "{{ $element.DownloadURL }}" - {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} - sha256 "{{ $element.SHA256 }}" - - def install - {{- range $index, $element := .Install }} - {{ . -}} - {{- end }} - end - end - {{- end }} - end - {{- end }} - - {{- with .Conflicts }} - {{ range $index, $element := . }} - conflicts_with "{{ . }}" - {{- end }} - {{- end }} - - {{- with .CustomBlock }} - {{ range $index, $element := . }} - {{ . }} - {{- end }} - {{- end }} - - {{- with .PostInstall }} - - def post_install - {{- range . }} - {{ . }} - {{- end }} - end - {{- end -}} - - {{- with .Caveats }} - - def caveats - <<~EOS - {{- range $index, $element := . }} - {{ . -}} - {{- end }} - EOS - end - {{- end -}} - - {{- with .Plist }} - - plist_options startup: false - - def plist - <<~EOS - {{ . }} - EOS - end - {{- end -}} - - {{- with .Service }} - - service do - {{- range . }} - {{ . }} - {{- end }} - end - {{- end -}} - - {{- if .Tests }} - - test do - {{- range $index, $element := .Tests }} - {{ . -}} - {{- end }} - end - {{- end }} -end -` +//go:embed templates +var formulaTemplate embed.FS diff --git a/internal/pipe/brew/templates/cask.rb b/internal/pipe/brew/templates/cask.rb new file mode 100644 index 00000000000..2b7808d0f00 --- /dev/null +++ b/internal/pipe/brew/templates/cask.rb @@ -0,0 +1,109 @@ +# typed: false +# frozen_string_literal: true + +# This file was generated by GoReleaser. DO NOT EDIT. +{{ if .CustomRequire -}} +require_relative "{{ .CustomRequire }}" +{{ end -}} +class {{ .Name }} < Formula + desc "{{ .Desc }}" + homepage "{{ .Homepage }}" + version "{{ .Version }}" + {{- if .License }} + license "{{ .License }}" + {{- end }} + {{- with .Dependencies }} + {{ range $index, $element := . }} + depends_on "{{ .Name }}" + {{- if .Type }} => :{{ .Type }}{{- else if .Version }} => "{{ .Version }}"{{- end }} + {{- with .OS }} if OS.{{ . }}?{{- end }} + {{- end }} + {{- end -}} + + {{- if and (not .LinuxPackages) .MacOSPackages }} + depends_on :macos + {{- end }} + {{- if and (not .MacOSPackages) .LinuxPackages }} + depends_on :linux + {{- end }} + {{- printf "\n" }} + + {{- if and .MacOSPackages .LinuxPackages }} + on_macos do + {{- include "macos_packages" . | indent 2 }} + end + + on_linux do + {{- include "linux_packages" . | indent 2 }} + end + {{- end }} + + {{- if and (.MacOSPackages) (not .LinuxPackages) }} + {{- template "macos_packages" . }} + {{- end }} + + {{- if and (not .MacOSPackages) (.LinuxPackages) }} + {{- template "linux_packages" . }} + {{- end }} + + {{- with .Conflicts }} + {{ range $index, $element := . }} + conflicts_with "{{ . }}" + {{- end }} + {{- end }} + + {{- with .CustomBlock }} + {{ range $index, $element := . }} + {{ . }} + {{- end }} + {{- end }} + + {{- with .PostInstall }} + + def post_install + {{- range . }} + {{ . }} + {{- end }} + end + {{- end -}} + + {{- with .Caveats }} + + def caveats + <<~EOS + {{- range $index, $element := . }} + {{ . -}} + {{- end }} + EOS + end + {{- end -}} + + {{- with .Plist }} + + plist_options startup: false + + def plist + <<~EOS + {{ . }} + EOS + end + {{- end -}} + + {{- with .Service }} + + service do + {{- range . }} + {{ . }} + {{- end }} + end + {{- end -}} + + {{- if .Tests }} + + test do + {{- range $index, $element := .Tests }} + {{ . -}} + {{- end }} + end + {{- end }} +end diff --git a/internal/pipe/brew/templates/linux_packages.rb b/internal/pipe/brew/templates/linux_packages.rb new file mode 100644 index 00000000000..8e86cea11d7 --- /dev/null +++ b/internal/pipe/brew/templates/linux_packages.rb @@ -0,0 +1,23 @@ +{{- define "linux_packages" }} +{{- range $element := .LinuxPackages }} + {{- if eq $element.Arch "amd64" }} + if Hardware::CPU.intel? + {{- end }} + {{- if eq $element.Arch "arm" }} + if Hardware::CPU.arm? && !Hardware::CPU.is_64_bit? + {{- end }} + {{- if eq $element.Arch "arm64" }} + if Hardware::CPU.arm? && Hardware::CPU.is_64_bit? + {{- end }} + url "{{ $element.DownloadURL }}" + {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} + sha256 "{{ $element.SHA256 }}" + + def install + {{- range $index, $element := .Install }} + {{ . -}} + {{- end }} + end + end +{{- end }} +{{- end }} diff --git a/internal/pipe/brew/templates/macos_packages.rb b/internal/pipe/brew/templates/macos_packages.rb new file mode 100644 index 00000000000..f4bcb4f59c3 --- /dev/null +++ b/internal/pipe/brew/templates/macos_packages.rb @@ -0,0 +1,52 @@ +{{- define "macos_packages" }} +{{- range $element := .MacOSPackages }} + {{- if eq $element.Arch "all" }} + url "{{ $element.DownloadURL }}" + {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} + sha256 "{{ $element.SHA256 }}" + + def install + {{- range $index, $element := .Install }} + {{ . -}} + {{- end }} + end + {{- else if $.HasOnlyAmd64MacOsPkg }} + url "{{ $element.DownloadURL }}" + {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} + sha256 "{{ $element.SHA256 }}" + + def install + {{- range $index, $element := .Install }} + {{ . -}} + {{- end }} + end + + if Hardware::CPU.arm? + def caveats + <<~EOS + The darwin_arm64 architecture is not supported for the {{ $.Name }} + formula at this time. The darwin_amd64 binary may work in compatibility + mode, but it might not be fully supported. + EOS + end + end + {{- else }} + {{- if eq $element.Arch "amd64" }} + if Hardware::CPU.intel? + {{- end }} + {{- if eq $element.Arch "arm64" }} + if Hardware::CPU.arm? + {{- end}} + url "{{ $element.DownloadURL }}" + {{- if .DownloadStrategy }}, using: {{ .DownloadStrategy }}{{- end }} + sha256 "{{ $element.SHA256 }}" + + def install + {{- range $index, $element := .Install }} + {{ . -}} + {{- end }} + end + end + {{- end }} +{{- end }} +{{- end }} diff --git a/internal/pipe/brew/testdata/TestFullFormulaeLinuxOnly.rb.golden b/internal/pipe/brew/testdata/TestFullFormulaeLinuxOnly.rb.golden index db3a652311e..a1e17afb20b 100644 --- a/internal/pipe/brew/testdata/TestFullFormulaeLinuxOnly.rb.golden +++ b/internal/pipe/brew/testdata/TestFullFormulaeLinuxOnly.rb.golden @@ -8,30 +8,28 @@ class Test < Formula version "0.1.3" depends_on :linux - on_linux do - if Hardware::CPU.intel? - url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Linux_x86_64.tar.gz" - sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" + if Hardware::CPU.intel? + url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Linux_x86_64.tar.gz" + sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" - def install - bin.install "test" - end + def install + bin.install "test" end - if Hardware::CPU.arm? && !Hardware::CPU.is_64_bit? - url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm6.tar.gz" - sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" + end + if Hardware::CPU.arm? && !Hardware::CPU.is_64_bit? + url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm6.tar.gz" + sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" - def install - bin.install "test" - end + def install + bin.install "test" end - if Hardware::CPU.arm? && Hardware::CPU.is_64_bit? - url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm64.tar.gz" - sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" + end + if Hardware::CPU.arm? && Hardware::CPU.is_64_bit? + url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Arm64.tar.gz" + sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c67" - def install - bin.install "test" - end + def install + bin.install "test" end end end diff --git a/internal/pipe/brew/testdata/TestFullFormulaeMacOSOnly.rb.golden b/internal/pipe/brew/testdata/TestFullFormulaeMacOSOnly.rb.golden index e5537ba074e..2d6ae1e458b 100644 --- a/internal/pipe/brew/testdata/TestFullFormulaeMacOSOnly.rb.golden +++ b/internal/pipe/brew/testdata/TestFullFormulaeMacOSOnly.rb.golden @@ -8,22 +8,20 @@ class Test < Formula version "0.1.3" depends_on :macos - on_macos do - if Hardware::CPU.intel? - url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz" - sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68" + if Hardware::CPU.intel? + url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz" + sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68" - def install - bin.install "test" - end + def install + bin.install "test" end - if Hardware::CPU.arm? - url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_arm64.tar.gz" - sha256 "1df5fdc2bad4ed4c28fbdc77b6c542988c0dc0e2ae34e0dc912bbb1c66646c58" + end + if Hardware::CPU.arm? + url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_arm64.tar.gz" + sha256 "1df5fdc2bad4ed4c28fbdc77b6c542988c0dc0e2ae34e0dc912bbb1c66646c58" - def install - bin.install "test" - end + def install + bin.install "test" end end end diff --git a/internal/pipe/brew/testdata/TestRunPipeBinaryRelease.rb.golden b/internal/pipe/brew/testdata/TestRunPipeBinaryRelease.rb.golden index e1921581dd5..e427ba1e3da 100644 --- a/internal/pipe/brew/testdata/TestRunPipeBinaryRelease.rb.golden +++ b/internal/pipe/brew/testdata/TestRunPipeBinaryRelease.rb.golden @@ -8,13 +8,11 @@ class Foo < Formula version "1.2.1" depends_on :macos - on_macos do - url "https://dummyhost/download/v1.2.1/foo_macos" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + url "https://dummyhost/download/v1.2.1/foo_macos" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "foo_macos" => "foo" - man1.install "./man/foo.1.gz" - end + def install + bin.install "foo_macos" => "foo" + man1.install "./man/foo.1.gz" end end diff --git a/internal/pipe/brew/testdata/TestRunPipeNameTemplate.rb.golden b/internal/pipe/brew/testdata/TestRunPipeNameTemplate.rb.golden index 8d43aaf7e02..be92de14d36 100644 --- a/internal/pipe/brew/testdata/TestRunPipeNameTemplate.rb.golden +++ b/internal/pipe/brew/testdata/TestRunPipeNameTemplate.rb.golden @@ -8,22 +8,20 @@ class FooIsBar < Formula version "1.0.1" depends_on :macos - on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "foo" - end + def install + bin.install "foo" + end - if Hardware::CPU.arm? - def caveats - <<~EOS - The darwin_arm64 architecture is not supported for the FooIsBar - formula at this time. The darwin_amd64 binary may work in compatibility - mode, but it might not be fully supported. - EOS - end + if Hardware::CPU.arm? + def caveats + <<~EOS + The darwin_arm64 architecture is not supported for the FooIsBar + formula at this time. The darwin_amd64 binary may work in compatibility + mode, but it might not be fully supported. + EOS end end end diff --git a/internal/pipe/brew/testdata/TestRunPipePullRequest.rb.golden b/internal/pipe/brew/testdata/TestRunPipePullRequest.rb.golden index e1921581dd5..e427ba1e3da 100644 --- a/internal/pipe/brew/testdata/TestRunPipePullRequest.rb.golden +++ b/internal/pipe/brew/testdata/TestRunPipePullRequest.rb.golden @@ -8,13 +8,11 @@ class Foo < Formula version "1.2.1" depends_on :macos - on_macos do - url "https://dummyhost/download/v1.2.1/foo_macos" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + url "https://dummyhost/download/v1.2.1/foo_macos" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "foo_macos" => "foo" - man1.install "./man/foo.1.gz" - end + def install + bin.install "foo_macos" => "foo" + man1.install "./man/foo.1.gz" end end diff --git a/internal/pipe/brew/testdata/TestRunPipeUniversalBinary.rb.golden b/internal/pipe/brew/testdata/TestRunPipeUniversalBinary.rb.golden index 3a599fc9073..0722da545c7 100644 --- a/internal/pipe/brew/testdata/TestRunPipeUniversalBinary.rb.golden +++ b/internal/pipe/brew/testdata/TestRunPipeUniversalBinary.rb.golden @@ -8,12 +8,10 @@ class Unibin < Formula version "1.0.1" depends_on :macos - on_macos do - url "https://dummyhost/download/v1.0.1/bin.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + url "https://dummyhost/download/v1.0.1/bin.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "unibin" - end + def install + bin.install "unibin" end end diff --git a/internal/pipe/brew/testdata/TestRunPipeUniversalBinaryNotReplacing.rb.golden b/internal/pipe/brew/testdata/TestRunPipeUniversalBinaryNotReplacing.rb.golden index f17ace78f1d..8f6f8f4027b 100644 --- a/internal/pipe/brew/testdata/TestRunPipeUniversalBinaryNotReplacing.rb.golden +++ b/internal/pipe/brew/testdata/TestRunPipeUniversalBinaryNotReplacing.rb.golden @@ -8,22 +8,20 @@ class Unibin < Formula version "1.0.1" depends_on :macos - on_macos do - if Hardware::CPU.intel? - url "https://dummyhost/download/v1.0.1/bin_amd64.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + if Hardware::CPU.intel? + url "https://dummyhost/download/v1.0.1/bin_amd64.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "unibin" - end + def install + bin.install "unibin" end - if Hardware::CPU.arm? - url "https://dummyhost/download/v1.0.1/bin_arm64.tar.gz" - sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + end + if Hardware::CPU.arm? + url "https://dummyhost/download/v1.0.1/bin_arm64.tar.gz" + sha256 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - def install - bin.install "unibin" - end + def install + bin.install "unibin" end end end