Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions internal/pipe/brew/brew.go
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down Expand Up @@ -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]++
Expand Down
87 changes: 73 additions & 14 deletions internal/pipe/brew/brew_test.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
},
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1116,34 +1147,40 @@ 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,
Extra: map[string]interface{}{
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",
Expand All @@ -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)
})
}

Expand Down
34 changes: 22 additions & 12 deletions internal/pipe/brew/testdata/TestFullPipe/custom_block.rb.golden
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions internal/pipe/brew/testdata/TestFullPipe/custom_require.rb.golden
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions internal/pipe/brew/testdata/TestFullPipe/default.rb.golden
Expand Up @@ -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
Expand Down