1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-17 01:42:37 +02:00

feat: Adds template support to binary property (#544)

I added support for using the Go template engine with the binary
property. This is needed for generating binaries that include
the current version number in their file name. For example
`terraform-provider-NAME_vX.Y.Z`.
This commit is contained in:
Edward Wilde
2018-02-15 11:30:15 +00:00
committed by Carlos Alexandro Becker
parent efd1588119
commit eef2647570
3 changed files with 81 additions and 3 deletions

View File

@ -17,6 +17,13 @@ builds:
main: ./cmd/main.go main: ./cmd/main.go
# Name of the binary. # Name of the binary.
# This is parsed with the Go template engine and the following variables
# are available:
# - Date
# - Commit
# - Tag
# - Version (Git tag without `v` prefix)
# Date format is `2006-01-02_15:04:05`.
# Default is the name of the project directory. # Default is the name of the project directory.
binary: program binary: program

View File

@ -18,6 +18,9 @@ import (
// langs to init // langs to init
_ "github.com/goreleaser/goreleaser/internal/builders/golang" _ "github.com/goreleaser/goreleaser/internal/builders/golang"
"time"
"bytes"
"text/template"
) )
// Pipe for build // Pipe for build
@ -98,6 +101,13 @@ func runHook(ctx *context.Context, env []string, hook string) error {
func doBuild(ctx *context.Context, build config.Build, target string) error { func doBuild(ctx *context.Context, build config.Build, target string) error {
var ext = extFor(target) var ext = extFor(target)
binary, err := binary(ctx, build)
if err != nil {
return err
}
build.Binary = binary
var name = build.Binary + ext var name = build.Binary + ext
var path = filepath.Join(ctx.Config.Dist, target, name) var path = filepath.Join(ctx.Config.Dist, target, name)
log.WithField("binary", path).Info("building") log.WithField("binary", path).Info("building")
@ -109,6 +119,31 @@ func doBuild(ctx *context.Context, build config.Build, target string) error {
}) })
} }
func binary(ctx *context.Context, build config.Build) (string, error) {
var data = struct {
Commit string
Tag string
Version string
Date string
Env map[string]string
}{
Commit: ctx.Git.Commit,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Date: time.Now().UTC().Format(time.RFC3339),
Env: ctx.Env,
}
var out bytes.Buffer
t, err := template.New("binary").
Option("missingkey=error").
Parse(build.Binary)
if err != nil {
return "", err
}
err = t.Execute(&out, data)
return out.String(), err
}
func extFor(target string) string { func extFor(target string) string {
if strings.Contains(target, "windows") { if strings.Contains(target, "windows") {
return ".exe" return ".exe"

View File

@ -52,14 +52,23 @@ func TestBuild(t *testing.T) {
Builds: []config.Build{ Builds: []config.Build{
{ {
Lang: "fake", Lang: "fake",
Binary: "testing", Binary: "testing.v{{.Version}}",
Flags: "-n", Flags: "-n",
Env: []string{"BLAH=1"}, Env: []string{"BLAH=1"},
}, },
}, },
} }
var ctx = context.New(config) var ctx = &context.Context{
assert.NoError(t, doBuild(ctx, ctx.Config.Builds[0], "darwin_amd64")) Artifacts: artifact.New(),
Git: context.GitInfo{
CurrentTag: "v1.2.3",
Commit: "123",
},
Version: "1.2.3",
Config: config,
}
error := doBuild(ctx, ctx.Config.Builds[0], "darwin_amd64")
assert.NoError(t, error)
} }
func TestRunPipe(t *testing.T) { func TestRunPipe(t *testing.T) {
@ -276,6 +285,33 @@ func TestExtOthers(t *testing.T) {
assert.Empty(t, "", extFor("winasdasd_sad")) assert.Empty(t, "", extFor("winasdasd_sad"))
} }
func TestBinaryFullTemplate(t *testing.T) {
var config = config.Project{
Builds: []config.Build{
{
Binary: `-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}"`,
},
},
}
var ctx = &context.Context{
Git: context.GitInfo{
CurrentTag: "v1.2.3",
Commit: "123",
},
Version: "1.2.3",
Config: config,
Env: map[string]string{"FOO": "123"},
}
binary, err := binary(ctx, ctx.Config.Builds[0])
assert.NoError(t, err)
assert.Contains(t, binary, "-s -w")
assert.Contains(t, binary, "-X main.version=1.2.3")
assert.Contains(t, binary, "-X main.tag=v1.2.3")
assert.Contains(t, binary, "-X main.commit=123")
assert.Contains(t, binary, "-X main.date=")
assert.Contains(t, binary, `-X "main.foo=123"`)
}
// //
// Helpers // Helpers
// //