You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	ldflags template
This commit is contained in:
		
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
									
									
									
									
								
							| @@ -176,9 +176,15 @@ build: | ||||
|   # Default is empty | ||||
|   flags: -tags dev | ||||
|  | ||||
|   # Custom ldflags. | ||||
|   # Default is `-s -w` | ||||
|   ldflags: -s -w | ||||
|   # Custom ldflags template. | ||||
|   # This is parsed with Golang template engine and the following variables | ||||
|   # are available: | ||||
|   # - Version | ||||
|   # - Date | ||||
|   # - Commit | ||||
|   # The default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}` | ||||
|   # Date format is `2006-01-02_15:04:05` | ||||
|   ldflags_template: -s -w -X main.build={{.Version}} | ||||
|  | ||||
|   # GOOS list to build in. | ||||
|   # For more info refer to https://golang.org/doc/install/source#environment | ||||
|   | ||||
| @@ -11,6 +11,7 @@ type GitInfo struct { | ||||
| 	CurrentTag  string | ||||
| 	PreviousTag string | ||||
| 	Diff        string | ||||
| 	Commit      string | ||||
| } | ||||
|  | ||||
| // Context carries along some data through the pipes | ||||
|   | ||||
							
								
								
									
										9
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								main.go
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ package main | ||||
| import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/config" | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| @@ -19,12 +20,16 @@ import ( | ||||
| 	"github.com/urfave/cli" | ||||
| ) | ||||
|  | ||||
| var version = "master" | ||||
| var ( | ||||
| 	version = "master" | ||||
| 	commit  = "master" | ||||
| 	date    = time.Now().Format("2006-01-02_15:04:05") | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	var app = cli.NewApp() | ||||
| 	app.Name = "goreleaser" | ||||
| 	app.Version = version | ||||
| 	app.Version = version + ", commit: " + commit + ", built at " + date | ||||
| 	app.Usage = "Deliver Go binaries as fast and easily as possible" | ||||
| 	app.Flags = []cli.Flag{ | ||||
| 		cli.StringFlag{ | ||||
|   | ||||
| @@ -61,14 +61,18 @@ func (Pipe) Run(ctx *context.Context) error { | ||||
| } | ||||
|  | ||||
| func build(name, goos, goarch string, ctx *context.Context) error { | ||||
| 	ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Version | ||||
| 	output := "dist/" + name + "/" + ctx.Config.Build.Binary + extFor(goos) | ||||
| 	log.Println("Building", output) | ||||
| 	cmd := []string{"go", "build"} | ||||
| 	if ctx.Config.Build.Flags != "" { | ||||
| 		cmd = append(cmd, strings.Fields(ctx.Config.Build.Flags)...) | ||||
| 	} | ||||
| 	cmd = append(cmd, "-ldflags="+ldflags, "-o", output, ctx.Config.Build.Main) | ||||
| 	flags, err := ldflags(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	log.Println(flags) | ||||
| 	cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main) | ||||
| 	if err := run(goos, goarch, cmd); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -84,47 +88,3 @@ func run(goos, goarch string, command []string) error { | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // list from https://golang.org/doc/install/source#environment | ||||
| var valids = []string{ | ||||
| 	"androidarm", | ||||
| 	"darwin386", | ||||
| 	"darwinamd64", | ||||
| 	"darwinarm", | ||||
| 	"darwinarm64", | ||||
| 	"dragonflyamd64", | ||||
| 	"freebsd386", | ||||
| 	"freebsdamd64", | ||||
| 	"freebsdarm", | ||||
| 	"linux386", | ||||
| 	"linuxamd64", | ||||
| 	"linuxarm", | ||||
| 	"linuxarm64", | ||||
| 	"linuxppc64", | ||||
| 	"linuxppc64le", | ||||
| 	"linuxmips", | ||||
| 	"linuxmipsle", | ||||
| 	"linuxmips64", | ||||
| 	"linuxmips64le", | ||||
| 	"netbsd386", | ||||
| 	"netbsdamd64", | ||||
| 	"netbsdarm", | ||||
| 	"openbsd386", | ||||
| 	"openbsdamd64", | ||||
| 	"openbsdarm", | ||||
| 	"plan9386", | ||||
| 	"plan9amd64", | ||||
| 	"solarisamd64", | ||||
| 	"windows386", | ||||
| 	"windowsamd64", | ||||
| } | ||||
|  | ||||
| func valid(goos, goarch string) bool { | ||||
| 	var s = goos + goarch | ||||
| 	for _, a := range valids { | ||||
| 		if a == s { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|   | ||||
| @@ -1,13 +1,32 @@ | ||||
| package build | ||||
|  | ||||
| import ( | ||||
| 	"runtime" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/config" | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestValid(t *testing.T) { | ||||
| 	assert.True(t, valid("windows", "386")) | ||||
| 	assert.True(t, valid("linux", "386")) | ||||
| 	assert.False(t, valid("windows", "arm")) | ||||
| func TestRun(t *testing.T) { | ||||
| 	assert.NoError(t, run(runtime.GOOS, runtime.GOARCH, []string{"go", "list", "./..."})) | ||||
| } | ||||
|  | ||||
| func TestRunInvalidCommand(t *testing.T) { | ||||
| 	assert.Error(t, run(runtime.GOOS, runtime.GOARCH, []string{"gggggo", "nope"})) | ||||
| } | ||||
|  | ||||
| func TestBuild(t *testing.T) { | ||||
| 	assert := assert.New(t) | ||||
| 	var config = config.Project{ | ||||
| 		Build: config.Build{ | ||||
| 			Binary: "testing", | ||||
| 			Flags:  "-n", | ||||
| 		}, | ||||
| 	} | ||||
| 	var ctx = &context.Context{ | ||||
| 		Config: config, | ||||
| 	} | ||||
| 	assert.NoError(build("build_test", runtime.GOOS, runtime.GOARCH, ctx)) | ||||
| } | ||||
|   | ||||
							
								
								
									
										32
									
								
								pipeline/build/ldflags.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								pipeline/build/ldflags.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package build | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"log" | ||||
| 	"text/template" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| ) | ||||
|  | ||||
| type ldflagsData struct { | ||||
| 	Date    string | ||||
| 	Commit  string | ||||
| 	Version string | ||||
| } | ||||
|  | ||||
| func ldflags(ctx *context.Context) (string, error) { | ||||
| 	var data = ldflagsData{ | ||||
| 		Commit:  ctx.Git.Commit, | ||||
| 		Version: ctx.Git.CurrentTag, | ||||
| 		Date:    time.Now().Format("2006-01-02_15:04:05"), | ||||
| 	} | ||||
| 	log.Println(ctx.Git) | ||||
| 	var out bytes.Buffer | ||||
| 	t, err := template.New("ldflags").Parse(ctx.Config.Build.Ldflags) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	err = t.Execute(&out, data) | ||||
| 	return out.String(), err | ||||
| } | ||||
							
								
								
									
										46
									
								
								pipeline/build/ldflags_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								pipeline/build/ldflags_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| package build | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/config" | ||||
| 	"github.com/goreleaser/goreleaser/context" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestLdFlagsFullTemplate(t *testing.T) { | ||||
| 	assert := assert.New(t) | ||||
| 	var config = config.Project{ | ||||
| 		Build: config.Build{ | ||||
| 			Ldflags: "-s -w -X main.version={{.Version}} -X main.date={{.Date}} -X main.commit={{.Commit}}", | ||||
| 		}, | ||||
| 	} | ||||
| 	var ctx = &context.Context{ | ||||
| 		Git: context.GitInfo{ | ||||
| 			CurrentTag: "v1.2.3", | ||||
| 			Commit:     "123", | ||||
| 		}, | ||||
| 		Config: config, | ||||
| 	} | ||||
| 	flags, err := ldflags(ctx) | ||||
| 	assert.NoError(err) | ||||
| 	assert.Contains(flags, "-s -w") | ||||
| 	assert.Contains(flags, "-X main.version=v1.2.3") | ||||
| 	assert.Contains(flags, "-X main.commit=123") | ||||
| 	// TODO assert main.date | ||||
| } | ||||
|  | ||||
| func TestInvalidTemplate(t *testing.T) { | ||||
| 	assert := assert.New(t) | ||||
| 	var config = config.Project{ | ||||
| 		Build: config.Build{ | ||||
| 			Ldflags: "{invalid{.Template}}}{{}}}", | ||||
| 		}, | ||||
| 	} | ||||
| 	var ctx = &context.Context{ | ||||
| 		Config: config, | ||||
| 	} | ||||
| 	flags, err := ldflags(ctx) | ||||
| 	assert.Error(err) | ||||
| 	assert.Equal(flags, "") | ||||
| } | ||||
							
								
								
									
										45
									
								
								pipeline/build/valid_os.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								pipeline/build/valid_os.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| package build | ||||
|  | ||||
| // list from https://golang.org/doc/install/source#environment | ||||
| var valids = []string{ | ||||
| 	"androidarm", | ||||
| 	"darwin386", | ||||
| 	"darwinamd64", | ||||
| 	"darwinarm", | ||||
| 	"darwinarm64", | ||||
| 	"dragonflyamd64", | ||||
| 	"freebsd386", | ||||
| 	"freebsdamd64", | ||||
| 	"freebsdarm", | ||||
| 	"linux386", | ||||
| 	"linuxamd64", | ||||
| 	"linuxarm", | ||||
| 	"linuxarm64", | ||||
| 	"linuxppc64", | ||||
| 	"linuxppc64le", | ||||
| 	"linuxmips", | ||||
| 	"linuxmipsle", | ||||
| 	"linuxmips64", | ||||
| 	"linuxmips64le", | ||||
| 	"netbsd386", | ||||
| 	"netbsdamd64", | ||||
| 	"netbsdarm", | ||||
| 	"openbsd386", | ||||
| 	"openbsdamd64", | ||||
| 	"openbsdarm", | ||||
| 	"plan9386", | ||||
| 	"plan9amd64", | ||||
| 	"solarisamd64", | ||||
| 	"windows386", | ||||
| 	"windowsamd64", | ||||
| } | ||||
|  | ||||
| func valid(goos, goarch string) bool { | ||||
| 	var s = goos + goarch | ||||
| 	for _, a := range valids { | ||||
| 		if a == s { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
							
								
								
									
										13
									
								
								pipeline/build/valid_os_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								pipeline/build/valid_os_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| package build | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestValid(t *testing.T) { | ||||
| 	assert.True(t, valid("windows", "386")) | ||||
| 	assert.True(t, valid("linux", "386")) | ||||
| 	assert.False(t, valid("windows", "arm")) | ||||
| } | ||||
| @@ -51,7 +51,7 @@ func (Pipe) Run(ctx *context.Context) error { | ||||
| 		ctx.Config.Build.Goarch = []string{"amd64", "386"} | ||||
| 	} | ||||
| 	if ctx.Config.Build.Ldflags == "" { | ||||
| 		ctx.Config.Build.Ldflags = "-s -w" | ||||
| 		ctx.Config.Build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" | ||||
| 	} | ||||
|  | ||||
| 	if ctx.Config.Archive.NameTemplate == "" { | ||||
|   | ||||
							
								
								
									
										21
									
								
								pipeline/git/commit.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								pipeline/git/commit.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| package git | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| func commitHash() (string, error) { | ||||
| 	cmd := exec.Command( | ||||
| 		"git", | ||||
| 		"show", | ||||
| 		"--format='%H'", | ||||
| 		"HEAD", | ||||
| 	) | ||||
| 	bts, err := cmd.CombinedOutput() | ||||
| 	if err != nil { | ||||
| 		return "", errors.New(err.Error() + ": " + string(bts)) | ||||
| 	} | ||||
| 	return strings.Replace(strings.Split(string(bts), "\n")[0], "'", "", -1), err | ||||
| } | ||||
							
								
								
									
										15
									
								
								pipeline/git/commit_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								pipeline/git/commit_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| package git | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestCommit(t *testing.T) { | ||||
| 	assert := assert.New(t) | ||||
| 	commit, err := commitHash() | ||||
| 	assert.NoError(err) | ||||
| 	assert.NotEmpty(commit) | ||||
| 	assert.NotContains(commit, "'") | ||||
| } | ||||
| @@ -49,5 +49,10 @@ func (Pipe) Run(ctx *context.Context) (err error) { | ||||
| 	if matches, err := regexp.MatchString("^[0-9.]+", ctx.Version); !matches || err != nil { | ||||
| 		return ErrInvalidVersionFormat{ctx.Version} | ||||
| 	} | ||||
| 	commit, err := commitHash() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	ctx.Git.Commit = commit | ||||
| 	return | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user