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

fix: re-added .Binary to supported fields

Also added a deprecation warning and tests,

Closes #476
This commit is contained in:
Carlos Alexandro Becker 2017-12-20 09:18:59 -02:00
parent 4ca8faa862
commit 449ec3f49b
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
2 changed files with 60 additions and 0 deletions

View File

@ -2,15 +2,22 @@ package nametemplate
import (
"bytes"
"regexp"
"text/template"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
)
var deprecatedBinary = regexp.MustCompile("\\{\\{ ?\\.Binary ?\\}\\}")
// Apply applies the name template to the given artifact and name
// TODO: this should be refactored alongside with other name template related todos
func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, error) {
if deprecatedBinary.MatchString(ctx.Config.Archive.NameTemplate) {
log.WithField("field", "{{.Binary}}").Warn("you are using a deprecated field on your template, please check the documentation")
}
var out bytes.Buffer
t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate)
if err != nil {
@ -26,6 +33,7 @@ func Apply(ctx *context.Context, a artifact.Artifact, name string) (string, erro
Version: ctx.Version,
Tag: ctx.Git.CurrentTag,
ProjectName: name,
Binary: name, // TODO: deprecated, remove soon
Env: ctx.Env,
}
err = t.Execute(&out, data)

View File

@ -0,0 +1,52 @@
package nametemplate
import (
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/tj/assert"
)
func TestNameTemplate(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
Archive: config.Archive{
NameTemplate: "{{.Binary}}_{{.ProjectName}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}",
Replacements: map[string]string{
"windows": "windao",
},
},
})
s, err := Apply(ctx, artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.NoError(t, err)
assert.Equal(t, "bin_bin_windao_amd64", s)
s, err = Apply(ctx, artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.NoError(t, err)
assert.Equal(t, "bin_bin_darwin_amd64", s)
}
func TestInvalidNameTemplate(t *testing.T) {
var ctx = context.New(config.Project{
ProjectName: "proj",
Archive: config.Archive{
NameTemplate: "{{.Binary}",
},
})
s, err := Apply(ctx, artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "winbin",
}, "bin")
assert.EqualError(t, err, `template: archive_name:1: unexpected "}" in operand`)
assert.Empty(t, s)
}