You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	refactor: name templates for archive pipe
This commit is contained in:
		| @@ -15,15 +15,30 @@ archive: | ||||
|   # This is parsed with the Go template engine and the following variables | ||||
|   # are available: | ||||
|   # - ProjectName | ||||
|   # - Binary (Name of the binary if the packaging format is binary) | ||||
|   # - Tag | ||||
|   # - Version (Git tag without `v` prefix) | ||||
|   # - Os | ||||
|   # - Arch | ||||
|   # - Arm (ARM version) | ||||
|   # - Env (environment variables) | ||||
|   # Default is `{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}`. | ||||
|   # Defaults: | ||||
|   # - if format is `tar.gz` or `zip`: | ||||
|   #   - `{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}` | ||||
|   # - if format is `binary`: | ||||
|   #   - `{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}` | ||||
|   name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" | ||||
|  | ||||
|   # Replacements for GOOS and GOARCH in the archive name. | ||||
|   # Keys should be valid GOOSs or GOARCHs. | ||||
|   # Values are the respective replacements. | ||||
|   # Default is empty. | ||||
|   replacements: | ||||
|     amd64: 64-bit | ||||
|     386: 32-bit | ||||
|     darwin: macOS | ||||
|     linux: Tux | ||||
|  | ||||
|   # Set to true, if you want all files in the archive to be in a single directory. | ||||
|   # If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz', | ||||
|   # you get a folder 'goreleaser_Linux_arm64'. | ||||
| @@ -44,16 +59,6 @@ archive: | ||||
|     - goos: windows | ||||
|       format: zip | ||||
|  | ||||
|   # Replacements for GOOS and GOARCH in the archive name. | ||||
|   # Keys should be valid GOOSs or GOARCHs. | ||||
|   # Values are the respective replacements. | ||||
|   # Default is empty. | ||||
|   replacements: | ||||
|     amd64: 64-bit | ||||
|     386: 32-bit | ||||
|     darwin: macOS | ||||
|     linux: Tux | ||||
|  | ||||
|   # Additional files/globs you want to add to the archive. | ||||
|   # Defaults are any files matching `LICENCE*`, `LICENSE*`, | ||||
|   # `README*` and `CHANGELOG*` (case-insensitive). | ||||
|   | ||||
| @@ -25,7 +25,7 @@ func NewFields(ctx *context.Context, a artifact.Artifact, replacements map[strin | ||||
| 		Version:     ctx.Version, | ||||
| 		Tag:         ctx.Git.CurrentTag, | ||||
| 		ProjectName: ctx.Config.ProjectName, | ||||
| 		Binary:      a.Name, | ||||
| 		Binary:      a.Extra["Binary"], | ||||
| 		Os:          replace(replacements, a.Goos), | ||||
| 		Arch:        replace(replacements, a.Goarch), | ||||
| 		Arm:         replace(replacements, a.Goarm), | ||||
|   | ||||
| @@ -16,10 +16,13 @@ import ( | ||||
| 	"github.com/goreleaser/archive" | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| 	"github.com/goreleaser/goreleaser/internal/artifact" | ||||
| 	"github.com/goreleaser/goreleaser/internal/nametemplate" | ||||
| 	"github.com/goreleaser/goreleaser/internal/template" | ||||
| ) | ||||
|  | ||||
| const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" | ||||
| const ( | ||||
| 	defaultNameTemplate       = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" | ||||
| 	defaultBinaryNameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" | ||||
| ) | ||||
|  | ||||
| // Pipe for archive | ||||
| type Pipe struct{} | ||||
| @@ -28,6 +31,34 @@ func (Pipe) String() string { | ||||
| 	return "creating archives" | ||||
| } | ||||
|  | ||||
| // Default sets the pipe defaults | ||||
| func (Pipe) Default(ctx *context.Context) error { | ||||
| 	var archive = &ctx.Config.Archive | ||||
| 	if archive.Format == "" { | ||||
| 		archive.Format = "tar.gz" | ||||
| 	} | ||||
| 	if len(archive.Files) == 0 { | ||||
| 		archive.Files = []string{ | ||||
| 			"licence*", | ||||
| 			"LICENCE*", | ||||
| 			"license*", | ||||
| 			"LICENSE*", | ||||
| 			"readme*", | ||||
| 			"README*", | ||||
| 			"changelog*", | ||||
| 			"CHANGELOG*", | ||||
| 		} | ||||
| 	} | ||||
| 	if archive.NameTemplate == "" { | ||||
| 		if archive.Format == "binary" { | ||||
| 			archive.NameTemplate = defaultBinaryNameTemplate | ||||
| 		} else { | ||||
| 			archive.NameTemplate = defaultNameTemplate | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Run the pipe | ||||
| func (Pipe) Run(ctx *context.Context) error { | ||||
| 	var g errgroup.Group | ||||
| @@ -44,32 +75,12 @@ func (Pipe) Run(ctx *context.Context) error { | ||||
| 	return g.Wait() | ||||
| } | ||||
|  | ||||
| // Default sets the pipe defaults | ||||
| func (Pipe) Default(ctx *context.Context) error { | ||||
| 	if ctx.Config.Archive.NameTemplate == "" { | ||||
| 		ctx.Config.Archive.NameTemplate = defaultNameTemplate | ||||
| 	} | ||||
| 	if ctx.Config.Archive.Format == "" { | ||||
| 		ctx.Config.Archive.Format = "tar.gz" | ||||
| 	} | ||||
| 	if len(ctx.Config.Archive.Files) == 0 { | ||||
| 		ctx.Config.Archive.Files = []string{ | ||||
| 			"licence*", | ||||
| 			"LICENCE*", | ||||
| 			"license*", | ||||
| 			"LICENSE*", | ||||
| 			"readme*", | ||||
| 			"README*", | ||||
| 			"changelog*", | ||||
| 			"CHANGELOG*", | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func create(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 	var format = packageFormat(ctx, artifacts[0].Goos) | ||||
| 	folder, err := nametemplate.Apply(ctx, artifacts[0], ctx.Config.ProjectName) | ||||
| 	folder, err := template.Apply( | ||||
| 		ctx.Config.Archive.NameTemplate, | ||||
| 		template.NewFields(ctx, artifacts[0], ctx.Config.Archive.Replacements), | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -111,7 +122,8 @@ func create(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| func skip(ctx *context.Context, artifacts []artifact.Artifact) error { | ||||
| 	for _, a := range artifacts { | ||||
| 		log.WithField("binary", a.Name).Info("skip archiving") | ||||
| 		name, err := nametemplate.Apply(ctx, a, a.Extra["Binary"]) | ||||
| 		var fields = template.NewFields(ctx, a, ctx.Config.Archive.Replacements) | ||||
| 		name, err := template.Apply(ctx.Config.Archive.NameTemplate, fields) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|   | ||||
| @@ -8,12 +8,11 @@ import ( | ||||
| 	"path/filepath" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/config" | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| 	"github.com/goreleaser/goreleaser/internal/artifact" | ||||
| 	"github.com/goreleaser/goreleaser/internal/testlib" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestDescription(t *testing.T) { | ||||
| @@ -122,7 +121,7 @@ func TestRunPipeBinary(t *testing.T) { | ||||
| 			Dist: dist, | ||||
| 			Archive: config.Archive{ | ||||
| 				Format:       "binary", | ||||
| 				NameTemplate: defaultNameTemplate, | ||||
| 				NameTemplate: defaultBinaryNameTemplate, | ||||
| 			}, | ||||
| 		}, | ||||
| 	) | ||||
| @@ -297,6 +296,18 @@ func TestDefaultSet(t *testing.T) { | ||||
| 	assert.Equal(t, "foo", ctx.Config.Archive.Files[0]) | ||||
| } | ||||
|  | ||||
| func TestDefaultFormatBinary(t *testing.T) { | ||||
| 	var ctx = &context.Context{ | ||||
| 		Config: config.Project{ | ||||
| 			Archive: config.Archive{ | ||||
| 				Format: "binary", | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	assert.NoError(t, Pipe{}.Default(ctx)) | ||||
| 	assert.Equal(t, defaultBinaryNameTemplate, ctx.Config.Archive.NameTemplate) | ||||
| } | ||||
|  | ||||
| func TestFormatFor(t *testing.T) { | ||||
| 	var ctx = &context.Context{ | ||||
| 		Config: config.Project{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user