Skip to content

Commit

Permalink
fix(brew): improve handling of single os (#4562)
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
caarlos0 committed Jan 19, 2024
1 parent 2a71473 commit ac398de
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -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
18 changes: 15 additions & 3 deletions internal/pipe/brew/brew.go
Expand Up @@ -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
}
Expand Down
183 changes: 7 additions & 176 deletions 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
Expand Down Expand Up @@ -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
109 changes: 109 additions & 0 deletions 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
23 changes: 23 additions & 0 deletions 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 }}

0 comments on commit ac398de

Please sign in to comment.