diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index 6bab5c43a..2eb480213 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -18,6 +18,7 @@ import ( "github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/tmpl" "github.com/goreleaser/goreleaser/pkg/archive" + "github.com/goreleaser/goreleaser/pkg/config" "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) lock.Lock() - if _, err := os.Stat(archivePath); !os.IsNotExist(err) { + if _, err = os.Stat(archivePath); !os.IsNotExist(err) { lock.Unlock() 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() defer archiveFile.Close() // nolint: errcheck + var log = log.WithField("archive", archivePath) log.Info("creating") - var wrap string - if ctx.Config.Archive.WrapInDirectory { - wrap = folder + + wrap, err := tmpl.New(ctx). + WithArtifact(binaries[0], ctx.Config.Archive.Replacements). + Apply(wrapFolder(ctx.Config.Archive)) + if err != nil { + return err } + var a = NewEnhancedArchive(archive.New(archiveFile), wrap) defer a.Close() // nolint: errcheck @@ -135,6 +141,17 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error { 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 { for _, binary := range binaries { log.WithField("binary", binary.Name).Info("skip archiving") diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go index 439eafc94..31648981f 100644 --- a/internal/pipe/archive/archive_test.go +++ b/internal/pipe/archive/archive_test.go @@ -282,8 +282,11 @@ func TestRunPipeWrap(t *testing.T) { Dist: dist, Archive: config.Archive{ NameTemplate: "foo", - WrapInDirectory: true, + WrapInDirectory: "foo_{{ .Os }}", Format: "tar.gz", + Replacements: map[string]string{ + "darwin": "macOS", + }, Files: []string{ "README.*", }, @@ -317,7 +320,7 @@ func TestRunPipeWrap(t *testing.T) { break } 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.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", + })) + }) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index ca212c149..1ed6afdec 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -149,7 +149,7 @@ type Archive struct { Format string `yaml:",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"` } diff --git a/www/content/archive.md b/www/content/archive.md index 6dcde51a3..00e0e3feb 100644 --- a/www/content/archive.md +++ b/www/content/archive.md @@ -36,6 +36,7 @@ archive: # If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz', # you get a folder 'goreleaser_Linux_arm64'. # If set to false, all files are extracted separately. + # You can also set it to a custom folder name. # Default is false. wrap_in_directory: true