mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-08 03:31:59 +02:00
refactor: name templates
This commit is contained in:
parent
cd03056dca
commit
fc4d9363bc
@ -1,49 +0,0 @@
|
||||
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 {
|
||||
return "", err
|
||||
}
|
||||
data := struct {
|
||||
Os, Arch, Arm, Version, Tag, Binary, ProjectName string
|
||||
Env map[string]string
|
||||
}{
|
||||
Os: replace(ctx.Config.Archive.Replacements, a.Goos),
|
||||
Arch: replace(ctx.Config.Archive.Replacements, a.Goarch),
|
||||
Arm: replace(ctx.Config.Archive.Replacements, a.Goarm),
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
ProjectName: name,
|
||||
Binary: name, // TODO: deprecated, remove soon
|
||||
Env: ctx.Env,
|
||||
}
|
||||
err = t.Execute(&out, data)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func replace(replacements map[string]string, original string) string {
|
||||
result := replacements[original]
|
||||
if result == "" {
|
||||
return original
|
||||
}
|
||||
return result
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package nametemplate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/apex/log/handlers/cli"
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/stretchr/testify/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)
|
||||
}
|
||||
|
||||
func TestDeprecatedFieldOnNameTemplate(t *testing.T) {
|
||||
for _, temp := range []string{
|
||||
"{{.Binary}}",
|
||||
"{{ .Binary}}",
|
||||
"{{.Binary }}",
|
||||
"{{ .Binary }}",
|
||||
} {
|
||||
t.Run(temp, func(tt *testing.T) {
|
||||
var out bytes.Buffer
|
||||
log.SetHandler(cli.New(&out))
|
||||
var ctx = context.New(config.Project{
|
||||
ProjectName: "proj",
|
||||
Archive: config.Archive{
|
||||
NameTemplate: temp,
|
||||
},
|
||||
})
|
||||
s, err := Apply(ctx, artifact.Artifact{
|
||||
Goos: "windows",
|
||||
Goarch: "amd64",
|
||||
Name: "winbin",
|
||||
}, "bin")
|
||||
assert.NoError(tt, err)
|
||||
assert.Equal(tt, "bin", s)
|
||||
assert.Contains(tt, out.String(), "you are using a deprecated field on your template")
|
||||
})
|
||||
}
|
||||
}
|
51
internal/template/template.go
Normal file
51
internal/template/template.go
Normal file
@ -0,0 +1,51 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
gotemplate "text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
)
|
||||
|
||||
type Fields struct {
|
||||
Version string
|
||||
Tag string
|
||||
ProjectName string
|
||||
Env map[string]string
|
||||
Os string
|
||||
Arch string
|
||||
Arm string
|
||||
Binary string
|
||||
}
|
||||
|
||||
func NewFields(ctx *context.Context, a artifact.Artifact, replacements map[string]string) Fields {
|
||||
return Fields{
|
||||
Env: ctx.Env,
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
Binary: a.Name,
|
||||
Os: replace(replacements, a.Goos),
|
||||
Arch: replace(replacements, a.Goarch),
|
||||
Arm: replace(replacements, a.Goarm),
|
||||
}
|
||||
}
|
||||
|
||||
func Apply(tmpl string, fields Fields) (string, error) {
|
||||
t, err := gotemplate.New(tmpl).Parse(tmpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var out bytes.Buffer
|
||||
err = t.Execute(&out, fields)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func replace(replacements map[string]string, original string) string {
|
||||
result := replacements[original]
|
||||
if result == "" {
|
||||
return original
|
||||
}
|
||||
return result
|
||||
}
|
56
internal/template/template_test.go
Normal file
56
internal/template/template_test.go
Normal file
@ -0,0 +1,56 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
var ctx = context.New(config.Project{
|
||||
ProjectName: "proj",
|
||||
})
|
||||
ctx.Env = map[string]string{
|
||||
"FOO": "bar",
|
||||
}
|
||||
ctx.Version = "1.0.0"
|
||||
ctx.Git.CurrentTag = "v1.0.0"
|
||||
var fields = NewFields(ctx, artifact.Artifact{
|
||||
Name: "binary",
|
||||
Goarch: "amd64",
|
||||
Goos: "linux",
|
||||
Goarm: "6",
|
||||
}, map[string]string{
|
||||
"linux": "Linux",
|
||||
})
|
||||
for expect, tmpl := range map[string]string{
|
||||
"bar": "{{.Env.FOO}}",
|
||||
"Linux": "{{.Os}}",
|
||||
"amd64": "{{.Arch}}",
|
||||
"6": "{{.Arm}}",
|
||||
"1.0.0": "{{.Version}}",
|
||||
"v1.0.0": "{{.Tag}}",
|
||||
"binary": "{{.Binary}}",
|
||||
"proj": "{{.ProjectName}}",
|
||||
} {
|
||||
tmpl := tmpl
|
||||
expect := expect
|
||||
t.Run(expect, func(tt *testing.T) {
|
||||
tt.Parallel()
|
||||
result, err := Apply(tmpl, fields)
|
||||
assert.NoError(tt, err)
|
||||
assert.Equal(tt, expect, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidTemplate(t *testing.T) {
|
||||
var ctx = context.New(config.Project{})
|
||||
var fields = NewFields(ctx, artifact.Artifact{}, map[string]string{})
|
||||
result, err := Apply("{{.Foo}", fields)
|
||||
assert.Empty(t, result)
|
||||
assert.EqualError(t, err, `template: {{.Foo}:1: unexpected "}" in operand`)
|
||||
}
|
Loading…
Reference in New Issue
Block a user