1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00

feat: allow to use .Path and .Name on universal binary post hooks (#2891)

* feat: allow to use .Path and .Name on universal binary post hooks

This allows the usage of `{{ .Path }}` and `{{ .Name }}` on post
templates.

Closes #2890

* docs: improve docs

* test: add more tests
This commit is contained in:
Carlos Alexandro Becker 2022-02-08 11:44:47 -03:00 committed by GitHub
parent 5f30f3606a
commit c457d8fff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 8 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/shell"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/build"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
@ -51,13 +52,18 @@ func (Pipe) Run(ctx *context.Context) error {
for _, unibin := range ctx.Config.UniversalBinaries {
unibin := unibin
g.Go(func() error {
if err := runHook(ctx, unibin.Hooks.Pre); err != nil {
opts := build.Options{
Target: "darwin_all",
Goos: "darwin",
Goarch: "all",
}
if err := runHook(ctx, &opts, unibin.Hooks.Pre); err != nil {
return fmt.Errorf("pre hook failed: %w", err)
}
if err := makeUniversalBinary(ctx, unibin); err != nil {
if err := makeUniversalBinary(ctx, &opts, unibin); err != nil {
return err
}
if err := runHook(ctx, unibin.Hooks.Post); err != nil {
if err := runHook(ctx, &opts, unibin.Hooks.Post); err != nil {
return fmt.Errorf("post hook failed: %w", err)
}
if !unibin.Replace {
@ -69,7 +75,7 @@ func (Pipe) Run(ctx *context.Context) error {
return g.Wait()
}
func runHook(ctx *context.Context, hooks config.Hooks) error {
func runHook(ctx *context.Context, opts *build.Options, hooks config.Hooks) error {
if len(hooks) == 0 {
return nil
}
@ -78,8 +84,9 @@ func runHook(ctx *context.Context, hooks config.Hooks) error {
var envs []string
envs = append(envs, ctx.Env.Strings()...)
tpl := tmpl.New(ctx).WithBuildOptions(*opts)
for _, rawEnv := range hook.Env {
env, err := tmpl.New(ctx).Apply(rawEnv)
env, err := tpl.Apply(rawEnv)
if err != nil {
return err
}
@ -87,12 +94,13 @@ func runHook(ctx *context.Context, hooks config.Hooks) error {
envs = append(envs, env)
}
dir, err := tmpl.New(ctx).Apply(hook.Dir)
tpl = tpl.WithEnvS(envs)
dir, err := tpl.Apply(hook.Dir)
if err != nil {
return err
}
sh, err := tmpl.New(ctx).WithEnvS(envs).Apply(hook.Cmd)
sh, err := tpl.Apply(hook.Cmd)
if err != nil {
return err
}
@ -125,13 +133,15 @@ const (
)
// heavily based on https://github.com/randall77/makefat
func makeUniversalBinary(ctx *context.Context, unibin config.UniversalBinary) error {
func makeUniversalBinary(ctx *context.Context, opts *build.Options, unibin config.UniversalBinary) error {
name, err := tmpl.New(ctx).Apply(unibin.NameTemplate)
if err != nil {
return err
}
opts.Name = name
path := filepath.Join(ctx.Config.Dist, name+"_darwin_all", name)
opts.Path = path
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}

View File

@ -185,6 +185,7 @@ func TestRun(t *testing.T) {
},
Post: []config.Hook{
{Cmd: "touch " + post},
{Cmd: `sh -c 'echo "{{ .Name }} {{ .Os }} {{ .Arch }} {{ .Arm }} {{ .Target }} {{ .Ext }}" > {{ .Path }}.post'`, Output: true},
},
},
},
@ -268,6 +269,11 @@ func TestRun(t *testing.T) {
require.NoError(t, Pipe{}.Run(ctx5))
require.FileExists(t, pre)
require.FileExists(t, post)
post := filepath.Join(dist, "foo_darwin_all/foo.post")
require.FileExists(t, post)
bts, err := os.ReadFile(post)
require.NoError(t, err)
require.Equal(t, "foo darwin all darwin_all \n", string(bts))
})
t.Run("failing pre-hook", func(t *testing.T) {

View File

@ -37,6 +37,7 @@ universal_binaries:
# Hooks can be used to customize the final binary,
# for example, to run generators.
# Those fields allow templates.
#
# Default is both hooks empty.
hooks:
pre: rice embed-go
@ -73,3 +74,22 @@ You can use the Go template engine to remove it if you'd like.
- id: bar
name_template: bin2
```
## Naming templates
Most fields that support [templating](/customization/templates/) will also
support the following build details:
| Key | Description |
|---------|-----------------------------------|
| .Os | `GOOS`, always `darwin` |
| .Arch | `GOARCH`, always `all` |
| .Arm | `GOARM`, always empty |
| .Ext | Extension, always empty |
| .Target | Build target, always `darwin_all` |
| .Path | The binary path |
| .Name | The binary name |
!!! tip
Notice that `.Path` and `.Name` will only be available after they are
evaluated, so they are mostly only useful in the `post` hooks.