mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-24 04:16:27 +02:00
Merge pull request #201 from goreleaser/format-overrides
Added archive format overrides support
This commit is contained in:
commit
a5f184d6d5
@ -90,6 +90,9 @@ build:
|
||||
# Archive customization
|
||||
archive:
|
||||
format: tar.gz
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
replacements:
|
||||
amd64: 64-bit
|
||||
darwin: macOS
|
||||
|
@ -47,12 +47,19 @@ type Build struct {
|
||||
Hooks Hooks
|
||||
}
|
||||
|
||||
// FormatOverride is used to specify a custom format for a specific GOOS.
|
||||
type FormatOverride struct {
|
||||
Goos string
|
||||
Format string
|
||||
}
|
||||
|
||||
// Archive config used for the archive
|
||||
type Archive struct {
|
||||
Format string
|
||||
NameTemplate string `yaml:"name_template"`
|
||||
Replacements map[string]string
|
||||
Files []string
|
||||
Format string
|
||||
FormatOverrides []FormatOverride `yaml:"format_overrides"`
|
||||
NameTemplate string `yaml:"name_template"`
|
||||
Replacements map[string]string
|
||||
Files []string
|
||||
}
|
||||
|
||||
// Release config used for the GitHub release
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/pipeline/archive/tar"
|
||||
@ -26,10 +27,11 @@ func (Pipe) Description() string {
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) error {
|
||||
var g errgroup.Group
|
||||
for _, archive := range ctx.Archives {
|
||||
for platform, archive := range ctx.Archives {
|
||||
archive := archive
|
||||
platform := platform
|
||||
g.Go(func() error {
|
||||
return create(ctx, archive)
|
||||
return create(ctx, platform, archive)
|
||||
})
|
||||
}
|
||||
return g.Wait()
|
||||
@ -41,15 +43,16 @@ type Archive interface {
|
||||
Add(name, path string) error
|
||||
}
|
||||
|
||||
func create(ctx *context.Context, name string) error {
|
||||
folder := filepath.Join(ctx.Config.Dist, name)
|
||||
file, err := os.Create(folder + "." + ctx.Config.Archive.Format)
|
||||
func create(ctx *context.Context, platform, name string) error {
|
||||
var folder = filepath.Join(ctx.Config.Dist, name)
|
||||
var format = formatFor(ctx, platform)
|
||||
file, err := os.Create(folder + "." + format)
|
||||
log.Println("Creating", file.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
var archive = archiveFor(file, ctx.Config.Archive.Format)
|
||||
var archive = archiveFor(file, format)
|
||||
defer func() { _ = archive.Close() }()
|
||||
for _, f := range ctx.Config.Archive.Files {
|
||||
if err = archive.Add(f, f); err != nil {
|
||||
@ -75,3 +78,12 @@ func archiveFor(file *os.File, format string) Archive {
|
||||
}
|
||||
return tar.New(file)
|
||||
}
|
||||
|
||||
func formatFor(ctx *context.Context, platform string) string {
|
||||
for _, override := range ctx.Config.Archive.FormatOverrides {
|
||||
if strings.HasPrefix(platform, override.Goos) {
|
||||
return override.Format
|
||||
}
|
||||
}
|
||||
return ctx.Config.Archive.Format
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ func TestRunPipe(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
var ctx = &context.Context{
|
||||
Archives: map[string]string{
|
||||
"darwinamd64": "mybin",
|
||||
"darwinamd64": "mybin",
|
||||
"windowsamd64": "mybin",
|
||||
},
|
||||
Config: config.Project{
|
||||
Dist: dist,
|
||||
@ -42,6 +43,12 @@ func TestRunPipe(t *testing.T) {
|
||||
Files: []string{
|
||||
"README.md",
|
||||
},
|
||||
FormatOverrides: []config.FormatOverride{
|
||||
{
|
||||
Goos: "windows",
|
||||
Format: "zip",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -56,3 +63,22 @@ func TestRunPipe(t *testing.T) {
|
||||
assert.Error(Pipe{}.Run(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFormatFor(t *testing.T) {
|
||||
var assert = assert.New(t)
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
Archive: config.Archive{
|
||||
Format: "tar.gz",
|
||||
FormatOverrides: []config.FormatOverride{
|
||||
{
|
||||
Goos: "windows",
|
||||
Format: "zip",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal("zip", formatFor(ctx, "windowsamd64"))
|
||||
assert.Equal("tar.gz", formatFor(ctx, "linux386"))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user