1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

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 <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2024-01-19 12:41:28 -03:00
committed by GitHub
parent 2a71473bf6
commit ac398de727
13 changed files with 272 additions and 259 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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

View File

@@ -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 }}

View File

@@ -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 }}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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