You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	feat: back-reference build artifacts from archive artifact (#908)
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							e1eeba292f
						
					
				
				
					commit
					850205abf1
				
			| @@ -66,7 +66,16 @@ type Artifact struct { | ||||
| 	Goarch string | ||||
| 	Goarm  string | ||||
| 	Type   Type | ||||
| 	Extra  map[string]string | ||||
| 	Extra  map[string]interface{} | ||||
| } | ||||
|  | ||||
| // ExtraOr returns the Extra field with the given key or the or value specified | ||||
| // if it is nil. | ||||
| func (a Artifact) ExtraOr(key string, or interface{}) interface{} { | ||||
| 	if a.Extra[key] == nil { | ||||
| 		return or | ||||
| 	} | ||||
| 	return a.Extra[key] | ||||
| } | ||||
|  | ||||
| // Checksum calculates the SHA256 checksum of the artifact. | ||||
|   | ||||
| @@ -162,3 +162,13 @@ func TestChecksumFileDoesntExist(t *testing.T) { | ||||
| 	require.EqualError(t, err, `failed to checksum: open /tmp/adasdasdas/asdasd/asdas: no such file or directory`) | ||||
| 	require.Empty(t, sum) | ||||
| } | ||||
|  | ||||
| func TestExtraOr(t *testing.T) { | ||||
| 	var a = Artifact{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Foo": "foo", | ||||
| 		}, | ||||
| 	} | ||||
| 	require.Equal(t, "foo", a.ExtraOr("Foo", "bar")) | ||||
| 	require.Equal(t, "bar", a.ExtraOr("Foobar", "bar")) | ||||
| } | ||||
|   | ||||
| @@ -97,7 +97,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti | ||||
| 		Goos:   target.os, | ||||
| 		Goarch: target.arch, | ||||
| 		Goarm:  target.arm, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": build.Binary, | ||||
| 			"Ext":    options.Ext, | ||||
| 		}, | ||||
|   | ||||
| @@ -1,11 +1,13 @@ | ||||
| package golang | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/internal/artifact" | ||||
| 	"github.com/goreleaser/goreleaser/internal/testlib" | ||||
| @@ -115,7 +117,7 @@ func TestBuild(t *testing.T) { | ||||
| 			Goos:   "linux", | ||||
| 			Goarch: "amd64", | ||||
| 			Type:   artifact.Binary, | ||||
| 			Extra: map[string]string{ | ||||
| 			Extra: map[string]interface{}{ | ||||
| 				"Ext":    "", | ||||
| 				"Binary": "foo", | ||||
| 			}, | ||||
| @@ -126,7 +128,7 @@ func TestBuild(t *testing.T) { | ||||
| 			Goos:   "darwin", | ||||
| 			Goarch: "amd64", | ||||
| 			Type:   artifact.Binary, | ||||
| 			Extra: map[string]string{ | ||||
| 			Extra: map[string]interface{}{ | ||||
| 				"Ext":    "", | ||||
| 				"Binary": "foo", | ||||
| 			}, | ||||
| @@ -138,7 +140,7 @@ func TestBuild(t *testing.T) { | ||||
| 			Goarch: "arm", | ||||
| 			Goarm:  "6", | ||||
| 			Type:   artifact.Binary, | ||||
| 			Extra: map[string]string{ | ||||
| 			Extra: map[string]interface{}{ | ||||
| 				"Ext":    "", | ||||
| 				"Binary": "foo", | ||||
| 			}, | ||||
| @@ -149,7 +151,7 @@ func TestBuild(t *testing.T) { | ||||
| 			Goos:   "windows", | ||||
| 			Goarch: "amd64", | ||||
| 			Type:   artifact.Binary, | ||||
| 			Extra: map[string]string{ | ||||
| 			Extra: map[string]interface{}{ | ||||
| 				"Ext":    ".exe", | ||||
| 				"Binary": "foo", | ||||
| 			}, | ||||
| @@ -376,9 +378,8 @@ func TestLdFlagsFullTemplate(t *testing.T) { | ||||
| 	assert.Contains(t, flags, "-X main.version=1.2.3") | ||||
| 	assert.Contains(t, flags, "-X main.tag=v1.2.3") | ||||
| 	assert.Contains(t, flags, "-X main.commit=123") | ||||
| 	// TODO: this will break in 2019 | ||||
| 	assert.Contains(t, flags, "-X main.date=2018") | ||||
| 	assert.Contains(t, flags, "-X main.time=2018") | ||||
| 	assert.Contains(t, flags, fmt.Sprintf("-X main.date=%d", time.Now().Year())) | ||||
| 	assert.Contains(t, flags, fmt.Sprintf("-X main.time=%d", time.Now().Year())) | ||||
| 	assert.Contains(t, flags, `-X "main.foo=123"`) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -137,6 +137,9 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error { | ||||
| 		Goos:   binaries[0].Goos, | ||||
| 		Goarch: binaries[0].Goarch, | ||||
| 		Goarm:  binaries[0].Goarm, | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Builds": binaries, | ||||
| 		}, | ||||
| 	}) | ||||
| 	return nil | ||||
| } | ||||
| @@ -161,9 +164,17 @@ func skip(ctx *context.Context, binaries []artifact.Artifact) error { | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		binary.Type = artifact.UploadableBinary | ||||
| 		binary.Name = name + binary.Extra["Ext"] | ||||
| 		ctx.Artifacts.Add(binary) | ||||
| 		ctx.Artifacts.Add(artifact.Artifact{ | ||||
| 			Type:   artifact.UploadableBinary, | ||||
| 			Name:   name + binary.ExtraOr("Ext", "").(string), | ||||
| 			Path:   binary.Path, | ||||
| 			Goos:   binary.Goos, | ||||
| 			Goarch: binary.Goarch, | ||||
| 			Goarm:  binary.Goarm, | ||||
| 			Extra: map[string]interface{}{ | ||||
| 				"Builds": []artifact.Artifact{binary}, | ||||
| 			}, | ||||
| 		}) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -59,27 +59,29 @@ func TestRunPipe(t *testing.T) { | ||||
| 					}, | ||||
| 				}, | ||||
| 			) | ||||
| 			ctx.Artifacts.Add(artifact.Artifact{ | ||||
| 			var darwinBuild = artifact.Artifact{ | ||||
| 				Goos:   "darwin", | ||||
| 				Goarch: "amd64", | ||||
| 				Name:   "mybin", | ||||
| 				Path:   filepath.Join(dist, "darwinamd64", "mybin"), | ||||
| 				Type:   artifact.Binary, | ||||
| 				Extra: map[string]string{ | ||||
| 				Extra: map[string]interface{}{ | ||||
| 					"Binary": "mybin", | ||||
| 				}, | ||||
| 			}) | ||||
| 			ctx.Artifacts.Add(artifact.Artifact{ | ||||
| 			} | ||||
| 			var windowsBuild = artifact.Artifact{ | ||||
| 				Goos:   "windows", | ||||
| 				Goarch: "amd64", | ||||
| 				Name:   "mybin.exe", | ||||
| 				Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"), | ||||
| 				Type:   artifact.Binary, | ||||
| 				Extra: map[string]string{ | ||||
| 				Extra: map[string]interface{}{ | ||||
| 					"Binary":    "mybin", | ||||
| 					"Extension": ".exe", | ||||
| 				}, | ||||
| 			}) | ||||
| 			} | ||||
| 			ctx.Artifacts.Add(darwinBuild) | ||||
| 			ctx.Artifacts.Add(windowsBuild) | ||||
| 			ctx.Version = "0.0.1" | ||||
| 			ctx.Git.CurrentTag = "v0.0.1" | ||||
| 			ctx.Config.Archive.Format = format | ||||
| @@ -91,6 +93,9 @@ func TestRunPipe(t *testing.T) { | ||||
| 			require.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name) | ||||
| 			require.Equal(tt, "foobar_0.0.1_windows_amd64.zip", windows.Name) | ||||
|  | ||||
| 			require.Equal(t, []artifact.Artifact{darwinBuild}, darwin.Extra["Builds"].([]artifact.Artifact)) | ||||
| 			require.Equal(t, []artifact.Artifact{windowsBuild}, windows.Extra["Builds"].([]artifact.Artifact)) | ||||
|  | ||||
| 			if format == "tar.gz" { | ||||
| 				// Check archive contents | ||||
| 				require.Equal( | ||||
| @@ -184,7 +189,7 @@ func TestRunPipeBinary(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join(dist, "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -194,7 +199,7 @@ func TestRunPipeBinary(t *testing.T) { | ||||
| 		Name:   "mybin.exe", | ||||
| 		Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 			"Ext":    ".exe", | ||||
| 		}, | ||||
| @@ -225,7 +230,7 @@ func TestRunPipeDistRemoved(t *testing.T) { | ||||
| 		Name:   "mybin.exe", | ||||
| 		Path:   filepath.Join("/path/to/nope", "windowsamd64", "mybin.exe"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary":    "mybin", | ||||
| 			"Extension": ".exe", | ||||
| 		}, | ||||
| @@ -260,7 +265,7 @@ func TestRunPipeInvalidGlob(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join("dist", "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -291,7 +296,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join("dist", "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -323,7 +328,7 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join("dist", "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -363,7 +368,7 @@ func TestRunPipeWrap(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join("dist", "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -487,7 +492,7 @@ func TestBinaryOverride(t *testing.T) { | ||||
| 				Name:   "mybin", | ||||
| 				Path:   filepath.Join(dist, "darwinamd64", "mybin"), | ||||
| 				Type:   artifact.Binary, | ||||
| 				Extra: map[string]string{ | ||||
| 				Extra: map[string]interface{}{ | ||||
| 					"Binary": "mybin", | ||||
| 				}, | ||||
| 			}) | ||||
| @@ -497,7 +502,7 @@ func TestBinaryOverride(t *testing.T) { | ||||
| 				Name:   "mybin.exe", | ||||
| 				Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"), | ||||
| 				Type:   artifact.Binary, | ||||
| 				Extra: map[string]string{ | ||||
| 				Extra: map[string]interface{}{ | ||||
| 					"Binary": "mybin", | ||||
| 					"Ext":    ".exe", | ||||
| 				}, | ||||
| @@ -548,7 +553,7 @@ func TestRunPipeSameArchiveFilename(t *testing.T) { | ||||
| 		Name:   "mybin", | ||||
| 		Path:   filepath.Join(dist, "darwinamd64", "mybin"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary": "mybin", | ||||
| 		}, | ||||
| 	}) | ||||
| @@ -558,7 +563,7 @@ func TestRunPipeSameArchiveFilename(t *testing.T) { | ||||
| 		Name:   "mybin.exe", | ||||
| 		Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"), | ||||
| 		Type:   artifact.Binary, | ||||
| 		Extra: map[string]string{ | ||||
| 		Extra: map[string]interface{}{ | ||||
| 			"Binary":    "mybin", | ||||
| 			"Extension": ".exe", | ||||
| 		}, | ||||
|   | ||||
| @@ -105,7 +105,7 @@ func doRun(ctx *context.Context) error { | ||||
| 					artifact.ByGoarm(docker.Goarm), | ||||
| 					artifact.ByType(artifact.Binary), | ||||
| 					func(a artifact.Artifact) bool { | ||||
| 						return a.Extra["Binary"] == docker.Binary | ||||
| 						return a.ExtraOr("Binary", "").(string) == docker.Binary | ||||
| 					}, | ||||
| 				), | ||||
| 			).List() | ||||
|   | ||||
| @@ -567,7 +567,7 @@ func TestRunPipe(t *testing.T) { | ||||
| 						Goarch: arch, | ||||
| 						Goos:   os, | ||||
| 						Type:   artifact.Binary, | ||||
| 						Extra: map[string]string{ | ||||
| 						Extra: map[string]interface{}{ | ||||
| 							"Binary": "mybin", | ||||
| 						}, | ||||
| 					}) | ||||
|   | ||||
| @@ -64,13 +64,13 @@ func New(ctx *context.Context) *Template { | ||||
| // WithArtifact populates fields from the artifact and replacements | ||||
| func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template { | ||||
| 	var bin = a.Extra[binary] | ||||
| 	if bin == "" { | ||||
| 		bin = t.fields[projectName].(string) | ||||
| 	if bin == nil { | ||||
| 		bin = t.fields[projectName] | ||||
| 	} | ||||
| 	t.fields[os] = replace(replacements, a.Goos) | ||||
| 	t.fields[arch] = replace(replacements, a.Goarch) | ||||
| 	t.fields[arm] = replace(replacements, a.Goarm) | ||||
| 	t.fields[binary] = bin | ||||
| 	t.fields[binary] = bin.(string) | ||||
| 	t.fields[artifactName] = a.Name | ||||
| 	return t | ||||
| } | ||||
|   | ||||
| @@ -44,7 +44,7 @@ func TestWithArtifact(t *testing.T) { | ||||
| 					Goarch: "amd64", | ||||
| 					Goos:   "linux", | ||||
| 					Goarm:  "6", | ||||
| 					Extra: map[string]string{ | ||||
| 					Extra: map[string]interface{}{ | ||||
| 						"Binary": "binary", | ||||
| 					}, | ||||
| 				}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user