1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat(brew): allow to template brew.install (#3593)

This commit is contained in:
Carlos Alexandro Becker 2022-11-25 09:32:43 -03:00 committed by GitHub
parent eedf9578a6
commit 541e3dfed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 219 additions and 94 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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