1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-06-06 23:46:42 +02:00

feat: support custom wrap directory

closes #728
This commit is contained in:
Carlos Alexandro Becker 2018-11-13 13:48:16 -02:00 committed by Carlos Alexandro Becker
parent ce267acabf
commit 18b8be6818
4 changed files with 47 additions and 7 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/tmpl" "github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/archive" "github.com/goreleaser/goreleaser/pkg/archive"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
) )
@ -90,7 +91,7 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
} }
archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format) archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
lock.Lock() lock.Lock()
if _, err := os.Stat(archivePath); !os.IsNotExist(err) { if _, err = os.Stat(archivePath); !os.IsNotExist(err) {
lock.Unlock() lock.Unlock()
return fmt.Errorf("archive named %s already exists. Check your archive name template", archivePath) return fmt.Errorf("archive named %s already exists. Check your archive name template", archivePath)
} }
@ -101,12 +102,17 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
} }
lock.Unlock() lock.Unlock()
defer archiveFile.Close() // nolint: errcheck defer archiveFile.Close() // nolint: errcheck
var log = log.WithField("archive", archivePath) var log = log.WithField("archive", archivePath)
log.Info("creating") log.Info("creating")
var wrap string
if ctx.Config.Archive.WrapInDirectory { wrap, err := tmpl.New(ctx).
wrap = folder WithArtifact(binaries[0], ctx.Config.Archive.Replacements).
Apply(wrapFolder(ctx.Config.Archive))
if err != nil {
return err
} }
var a = NewEnhancedArchive(archive.New(archiveFile), wrap) var a = NewEnhancedArchive(archive.New(archiveFile), wrap)
defer a.Close() // nolint: errcheck defer a.Close() // nolint: errcheck
@ -135,6 +141,17 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
return nil return nil
} }
func wrapFolder(a config.Archive) string {
switch a.WrapInDirectory {
case "true":
return a.NameTemplate
case "false":
return ""
default:
return a.WrapInDirectory
}
}
func skip(ctx *context.Context, binaries []artifact.Artifact) error { func skip(ctx *context.Context, binaries []artifact.Artifact) error {
for _, binary := range binaries { for _, binary := range binaries {
log.WithField("binary", binary.Name).Info("skip archiving") log.WithField("binary", binary.Name).Info("skip archiving")

View File

@ -282,8 +282,11 @@ func TestRunPipeWrap(t *testing.T) {
Dist: dist, Dist: dist,
Archive: config.Archive{ Archive: config.Archive{
NameTemplate: "foo", NameTemplate: "foo",
WrapInDirectory: true, WrapInDirectory: "foo_{{ .Os }}",
Format: "tar.gz", Format: "tar.gz",
Replacements: map[string]string{
"darwin": "macOS",
},
Files: []string{ Files: []string{
"README.*", "README.*",
}, },
@ -317,7 +320,7 @@ func TestRunPipeWrap(t *testing.T) {
break break
} }
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, filepath.Join("foo", n), h.Name) require.Equal(t, filepath.Join("foo_macOS", n), h.Name)
} }
} }
@ -520,3 +523,22 @@ func TestDuplicateFilesInsideArchive(t *testing.T) {
require.NoError(t, a.Add("foo", ff.Name())) require.NoError(t, a.Add("foo", ff.Name()))
require.EqualError(t, a.Add("foo", ff.Name()), "file foo already exists in the archive") require.EqualError(t, a.Add("foo", ff.Name()), "file foo already exists in the archive")
} }
func TestWrapInDirectory(t *testing.T) {
t.Run("false", func(t *testing.T) {
require.Equal(t, "", wrapFolder(config.Archive{
WrapInDirectory: "false",
}))
})
t.Run("true", func(t *testing.T) {
require.Equal(t, "foo", wrapFolder(config.Archive{
WrapInDirectory: "true",
NameTemplate: "foo",
}))
})
t.Run("custom", func(t *testing.T) {
require.Equal(t, "foobar", wrapFolder(config.Archive{
WrapInDirectory: "foobar",
}))
})
}

View File

@ -149,7 +149,7 @@ type Archive struct {
Format string `yaml:",omitempty"` Format string `yaml:",omitempty"`
FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"` FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"`
WrapInDirectory bool `yaml:"wrap_in_directory,omitempty"` WrapInDirectory string `yaml:"wrap_in_directory,omitempty"`
Files []string `yaml:",omitempty"` Files []string `yaml:",omitempty"`
} }

View File

@ -36,6 +36,7 @@ archive:
# If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz', # If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz',
# you get a folder 'goreleaser_Linux_arm64'. # you get a folder 'goreleaser_Linux_arm64'.
# If set to false, all files are extracted separately. # If set to false, all files are extracted separately.
# You can also set it to a custom folder name.
# Default is false. # Default is false.
wrap_in_directory: true wrap_in_directory: true