1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

feat: allow to use ModulePath on templates (#2128)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker
2021-03-22 08:55:01 -03:00
committed by GitHub
parent a62c6792e0
commit 7378edc708
7 changed files with 104 additions and 27 deletions

View File

@@ -44,6 +44,7 @@ const (
env = "Env"
date = "Date"
timestamp = "Timestamp"
modulePath = "ModulePath"
// artifact-only keys.
osKey = "Os"
@@ -72,6 +73,7 @@ func New(ctx *context.Context) *Template {
return &Template{
fields: Fields{
projectName: ctx.Config.ProjectName,
modulePath: ctx.ModulePath,
version: ctx.Version,
rawVersion: rawVersionV,
tag: ctx.Git.CurrentTag,
@@ -97,9 +99,9 @@ func New(ctx *context.Context) *Template {
// WithEnvS overrides template's env field with the given KEY=VALUE list of
// environment variables.
func (t *Template) WithEnvS(envs []string) *Template {
var result = map[string]string{}
result := map[string]string{}
for _, env := range envs {
var parts = strings.SplitN(env, "=", 2)
parts := strings.SplitN(env, "=", 2)
result[parts[0]] = parts[1]
}
return t.WithEnv(result)
@@ -122,7 +124,7 @@ func (t *Template) WithExtraFields(f Fields) *Template {
// WithArtifact populates Fields from the artifact and replacements.
func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]string) *Template {
var bin = a.Extra[binary]
bin := a.Extra[binary]
if bin == nil {
bin = t.fields[projectName]
}

View File

@@ -13,9 +13,10 @@ import (
)
func TestWithArtifact(t *testing.T) {
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "proj",
})
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.Env = map[string]string{
"FOO": "bar",
}
@@ -31,21 +32,22 @@ func TestWithArtifact(t *testing.T) {
ctx.Git.FullCommit = "fullcommit"
ctx.Git.ShortCommit = "shortcommit"
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"softfloat": "{{.Mips}}",
"1.2.3": "{{.Version}}",
"v1.2.3": "{{.Tag}}",
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
"test-branch": "{{.Branch}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"": "{{.ArtifactUploadHash}}",
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"softfloat": "{{.Mips}}",
"1.2.3": "{{.Version}}",
"v1.2.3": "{{.Tag}}",
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
"test-branch": "{{.Branch}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"": "{{.ArtifactUploadHash}}",
"github.com/goreleaser/goreleaser": "{{ .ModulePath }}",
} {
tmpl := tmpl
expect := expect
@@ -126,7 +128,7 @@ func TestEnv(t *testing.T) {
out: "",
},
}
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
@@ -140,7 +142,7 @@ func TestEnv(t *testing.T) {
}
func TestWithEnv(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
@@ -154,7 +156,7 @@ func TestWithEnv(t *testing.T) {
}
func TestFuncMap(t *testing.T) {
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "proj",
})
wd, err := os.Getwd()
@@ -293,7 +295,7 @@ func TestInvalidTemplate(t *testing.T) {
}
func TestEnvNotFound(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.2.4"
result, err := New(ctx).Apply("{{.Env.FOO}}")
require.Empty(t, result)
@@ -301,10 +303,9 @@ func TestEnvNotFound(t *testing.T) {
}
func TestWithExtraFields(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
out, _ := New(ctx).WithExtraFields(Fields{
"MyCustomField": "foo",
}).Apply("{{ .MyCustomField }}")
require.Equal(t, "foo", out)
}