mirror of
https://github.com/goreleaser/goreleaser.git
synced 2024-12-31 01:53:50 +02:00
feat: strip_parent_binary_folder (#3261)
* feat: strip_parent_binary_folder Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * docs: clarify dir Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: fmt Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
53ed81665b
commit
4da595ed26
@ -190,11 +190,15 @@ func doCreate(ctx *context.Context, arch config.Archive, binaries []*artifact.Ar
|
||||
}
|
||||
bins := []string{}
|
||||
for _, binary := range binaries {
|
||||
dst := binary.Name
|
||||
if arch.StripParentBinaryFolder {
|
||||
dst = filepath.Base(dst)
|
||||
}
|
||||
if err := a.Add(config.File{
|
||||
Source: binary.Path,
|
||||
Destination: binary.Name,
|
||||
Destination: dst,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, binary.Name, err)
|
||||
return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, dst, err)
|
||||
}
|
||||
bins = append(bins, binary.Name)
|
||||
}
|
||||
|
@ -33,9 +33,35 @@ func createFakeBinary(t *testing.T, dist, arch, bin string) {
|
||||
|
||||
func TestRunPipe(t *testing.T) {
|
||||
folder := testlib.Mktmp(t)
|
||||
for _, format := range []string{"tar.gz", "zip"} {
|
||||
t.Run("Archive format "+format, func(t *testing.T) {
|
||||
dist := filepath.Join(folder, format+"_dist")
|
||||
for _, dets := range []struct {
|
||||
Format string
|
||||
Strip bool
|
||||
}{
|
||||
{
|
||||
Format: "tar.gz",
|
||||
Strip: true,
|
||||
},
|
||||
{
|
||||
Format: "tar.gz",
|
||||
Strip: false,
|
||||
},
|
||||
|
||||
{
|
||||
Format: "zip",
|
||||
Strip: true,
|
||||
},
|
||||
{
|
||||
Format: "zip",
|
||||
Strip: false,
|
||||
},
|
||||
} {
|
||||
format := dets.Format
|
||||
name := "archive." + format
|
||||
if dets.Strip {
|
||||
name = "strip_" + name
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
dist := filepath.Join(folder, name+"_dist")
|
||||
require.NoError(t, os.Mkdir(dist, 0o755))
|
||||
for _, arch := range []string{"darwinamd64v1", "darwinall", "linux386", "linuxarm7", "linuxmipssoftfloat", "linuxamd64v3"} {
|
||||
createFakeBinary(t, dist, arch, "bin/mybin")
|
||||
@ -56,9 +82,10 @@ func TestRunPipe(t *testing.T) {
|
||||
ProjectName: "foobar",
|
||||
Archives: []config.Archive{
|
||||
{
|
||||
ID: "myid",
|
||||
Builds: []string{"default"},
|
||||
NameTemplate: defaultNameTemplate,
|
||||
ID: "myid",
|
||||
Builds: []string{"default"},
|
||||
NameTemplate: defaultNameTemplate,
|
||||
StripParentBinaryFolder: dets.Strip,
|
||||
Files: []config.File{
|
||||
{Source: "README.{{.Os}}.*"},
|
||||
{Source: "./foo/**/*"},
|
||||
@ -169,6 +196,7 @@ func TestRunPipe(t *testing.T) {
|
||||
ctx.Config.Archives[0].Format = format
|
||||
require.NoError(t, Pipe{}.Run(ctx))
|
||||
archives := ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive)).List()
|
||||
|
||||
for _, arch := range archives {
|
||||
expectBin := "bin/mybin"
|
||||
if arch.Goos == "windows" {
|
||||
@ -181,6 +209,11 @@ func TestRunPipe(t *testing.T) {
|
||||
require.Len(t, archives, 7)
|
||||
// TODO: should verify the artifact fields here too
|
||||
|
||||
expectBin := "bin/mybin"
|
||||
if dets.Strip {
|
||||
expectBin = "mybin"
|
||||
}
|
||||
|
||||
if format == "tar.gz" {
|
||||
// Check archive contents
|
||||
for name, os := range map[string]string{
|
||||
@ -196,7 +229,7 @@ func TestRunPipe(t *testing.T) {
|
||||
[]string{
|
||||
fmt.Sprintf("README.%s.md", os),
|
||||
"foo/bar/foobar/blah.txt",
|
||||
"bin/mybin",
|
||||
expectBin,
|
||||
},
|
||||
tarFiles(t, filepath.Join(dist, name)),
|
||||
)
|
||||
@ -208,7 +241,7 @@ func TestRunPipe(t *testing.T) {
|
||||
[]string{
|
||||
"README.windows.md",
|
||||
"foo/bar/foobar/blah.txt",
|
||||
"bin/mybin.exe",
|
||||
expectBin + ".exe",
|
||||
},
|
||||
zipFiles(t, filepath.Join(dist, "foobar_0.0.1_windows_amd64.zip")),
|
||||
)
|
||||
|
@ -493,6 +493,7 @@ type Archive struct {
|
||||
Format string `yaml:"format,omitempty" json:"format,omitempty"`
|
||||
FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
|
||||
WrapInDirectory string `yaml:"wrap_in_directory,omitempty" json:"wrap_in_directory,omitempty" jsonschema:"oneof_type=string;boolean"`
|
||||
StripParentBinaryFolder bool `yaml:"strip_parent_binary_folder,omitempty" json:"strip_parent_binary_folder,omitempty"`
|
||||
Files []File `yaml:"files,omitempty" json:"files,omitempty"`
|
||||
Meta bool `yaml:"meta,omitempty" json:"meta,omitempty"`
|
||||
AllowDifferentBinaryCount bool `yaml:"allow_different_binary_count,omitempty" json:"allow_different_binary_count,omitempty"`
|
||||
|
@ -56,6 +56,11 @@ archives:
|
||||
# Default is false.
|
||||
wrap_in_directory: true
|
||||
|
||||
# If set to true, will strip the parent directories away from binary files.
|
||||
# This might be useful if you have your binary be built with a subdir for some reason, but do no want that subdir inside the archive.
|
||||
# Default is false.
|
||||
strip_parent_binary_folder: true
|
||||
|
||||
# Can be used to change the archive formats for specific GOOSs.
|
||||
# Most common use case is to archive as zip on Windows.
|
||||
# Default is empty.
|
||||
|
Loading…
Reference in New Issue
Block a user