You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	refactor: improved name template
Should keep the previous behavior of having binary == nametemplate
This commit is contained in:
		| @@ -23,16 +23,21 @@ type Fields struct { | ||||
| } | ||||
|  | ||||
| // NewFields returns a Fields instances filled with the data provided | ||||
| func NewFields(ctx *context.Context, a artifact.Artifact, replacements map[string]string) Fields { | ||||
| func NewFields(ctx *context.Context, replacements map[string]string, artifacts ...artifact.Artifact) Fields { | ||||
| 	// This will fail if artifacts is empty - should never be though... | ||||
| 	var binary = artifacts[0].Extra["Binary"] | ||||
| 	if len(artifacts) > 1 { | ||||
| 		binary = ctx.Config.ProjectName | ||||
| 	} | ||||
| 	return Fields{ | ||||
| 		Env:         ctx.Env, | ||||
| 		Version:     ctx.Version, | ||||
| 		Tag:         ctx.Git.CurrentTag, | ||||
| 		ProjectName: ctx.Config.ProjectName, | ||||
| 		Binary:      a.Extra["Binary"], | ||||
| 		Os:          replace(replacements, a.Goos), | ||||
| 		Arch:        replace(replacements, a.Goarch), | ||||
| 		Arm:         replace(replacements, a.Goarm), | ||||
| 		Os:          replace(replacements, artifacts[0].Goos), | ||||
| 		Arch:        replace(replacements, artifacts[0].Goarch), | ||||
| 		Arm:         replace(replacements, artifacts[0].Goarm), | ||||
| 		Binary:      binary, | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -18,7 +18,7 @@ func TestTemplate(t *testing.T) { | ||||
| 	} | ||||
| 	ctx.Version = "1.0.0" | ||||
| 	ctx.Git.CurrentTag = "v1.0.0" | ||||
| 	var fields = NewFields(ctx, artifact.Artifact{ | ||||
| 	var artifact = artifact.Artifact{ | ||||
| 		Name:   "not-this-binary", | ||||
| 		Goarch: "amd64", | ||||
| 		Goos:   "linux", | ||||
| @@ -26,9 +26,8 @@ func TestTemplate(t *testing.T) { | ||||
| 		Extra: map[string]string{ | ||||
| 			"Binary": "binary", | ||||
| 		}, | ||||
| 	}, map[string]string{ | ||||
| 		"linux": "Linux", | ||||
| 	}) | ||||
| 	} | ||||
| 	var fields = NewFields(ctx, map[string]string{"linux": "Linux"}, artifact) | ||||
| 	for expect, tmpl := range map[string]string{ | ||||
| 		"bar":    "{{.Env.FOO}}", | ||||
| 		"Linux":  "{{.Os}}", | ||||
| @@ -50,9 +49,28 @@ func TestTemplate(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestNewFields(t *testing.T) { | ||||
| 	var ctx = context.New(config.Project{ | ||||
| 		ProjectName: "proj", | ||||
| 	}) | ||||
| 	ctx.Version = "1.0.0" | ||||
| 	ctx.Git.CurrentTag = "v1.0.0" | ||||
| 	var artifact = artifact.Artifact{ | ||||
| 		Name:   "not-this-binary", | ||||
| 		Goarch: "amd64", | ||||
| 		Goos:   "linux", | ||||
| 		Goarm:  "6", | ||||
| 		Extra: map[string]string{ | ||||
| 			"Binary": "binary", | ||||
| 		}, | ||||
| 	} | ||||
| 	var fields = NewFields(ctx, map[string]string{}, artifact, artifact) | ||||
| 	assert.Equal(t, "proj", fields.Binary) | ||||
| } | ||||
|  | ||||
| func TestInvalidTemplate(t *testing.T) { | ||||
| 	var ctx = context.New(config.Project{}) | ||||
| 	var fields = NewFields(ctx, artifact.Artifact{}, map[string]string{}) | ||||
| 	var fields = NewFields(ctx, map[string]string{}, artifact.Artifact{}) | ||||
| 	result, err := Apply("{{.Foo}", fields) | ||||
| 	assert.Empty(t, result) | ||||
| 	assert.EqualError(t, err, `template: {{.Foo}:1: unexpected "}" in operand`) | ||||
|   | ||||
| @@ -75,11 +75,11 @@ func (Pipe) Run(ctx *context.Context) error { | ||||
| 	return g.Wait() | ||||
| } | ||||
|  | ||||
| func create(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 	var format = packageFormat(ctx, artifacts[0].Goos) | ||||
| func create(ctx *context.Context, binaries []artifact.Artifact) error { | ||||
| 	var format = packageFormat(ctx, binaries[0].Goos) | ||||
| 	folder, err := filenametemplate.Apply( | ||||
| 		ctx.Config.Archive.NameTemplate, | ||||
| 		filenametemplate.NewFields(ctx, artifacts[0], ctx.Config.Archive.Replacements), | ||||
| 		filenametemplate.NewFields(ctx, ctx.Config.Archive.Replacements, binaries...), | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| @@ -103,7 +103,7 @@ func create(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 			return fmt.Errorf("failed to add %s to the archive: %s", f, err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| 	for _, binary := range artifacts { | ||||
| 	for _, binary := range binaries { | ||||
| 		if err := a.Add(wrap(ctx, binary.Name, folder), binary.Path); err != nil { | ||||
| 			return fmt.Errorf("failed to add %s -> %s to the archive: %s", binary.Path, binary.Name, err.Error()) | ||||
| 		} | ||||
| @@ -112,24 +112,24 @@ func create(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 		Type:   artifact.UploadableArchive, | ||||
| 		Name:   folder + "." + format, | ||||
| 		Path:   archivePath, | ||||
| 		Goos:   artifacts[0].Goos, | ||||
| 		Goarch: artifacts[0].Goarch, | ||||
| 		Goarm:  artifacts[0].Goarm, | ||||
| 		Goos:   binaries[0].Goos, | ||||
| 		Goarch: binaries[0].Goarch, | ||||
| 		Goarm:  binaries[0].Goarm, | ||||
| 	}) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func skip(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 	for _, a := range artifacts { | ||||
| 		log.WithField("binary", a.Name).Info("skip archiving") | ||||
| 		var fields = filenametemplate.NewFields(ctx, a, ctx.Config.Archive.Replacements) | ||||
| func skip(ctx *context.Context, binaries []artifact.Artifact) error { | ||||
| 	for _, binary := range binaries { | ||||
| 		log.WithField("binary", binary.Name).Info("skip archiving") | ||||
| 		var fields = filenametemplate.NewFields(ctx, ctx.Config.Archive.Replacements, binary) | ||||
| 		name, err := filenametemplate.Apply(ctx.Config.Archive.NameTemplate, fields) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		a.Type = artifact.UploadableBinary | ||||
| 		a.Name = name + a.Extra["Ext"] | ||||
| 		ctx.Artifacts.Add(a) | ||||
| 		binary.Type = artifact.UploadableBinary | ||||
| 		binary.Name = name + binary.Extra["Ext"] | ||||
| 		ctx.Artifacts.Add(binary) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -88,7 +88,7 @@ func doRun(ctx *context.Context) error { | ||||
| func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error { | ||||
| 	name, err := filenametemplate.Apply( | ||||
| 		ctx.Config.FPM.NameTemplate, | ||||
| 		filenametemplate.NewFields(ctx, binaries[0], ctx.Config.FPM.Replacements), | ||||
| 		filenametemplate.NewFields(ctx, ctx.Config.FPM.Replacements, binaries...), | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
|   | ||||
| @@ -102,7 +102,7 @@ func create(ctx *context.Context, arch string, binaries []artifact.Artifact) err | ||||
| 	var log = log.WithField("arch", arch) | ||||
| 	folder, err := filenametemplate.Apply( | ||||
| 		ctx.Config.Snapcraft.NameTemplate, | ||||
| 		filenametemplate.NewFields(ctx, binaries[0], ctx.Config.Snapcraft.Replacements), | ||||
| 		filenametemplate.NewFields(ctx, ctx.Config.Snapcraft.Replacements, binaries...), | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
|   | ||||
		Reference in New Issue
	
	Block a user