mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-10 03:47:03 +02:00
commit
e844d176be
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
|
||||
|
8
main.go
8
main.go
@ -19,12 +19,16 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var version = "master"
|
||||
var (
|
||||
version = "dev"
|
||||
commit = "none"
|
||||
date = "unknown"
|
||||
)
|
||||
|
||||
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{
|
||||
|
@ -62,7 +62,6 @@ 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 := filepath.Join(
|
||||
ctx.Config.Dist,
|
||||
name,
|
||||
@ -73,7 +72,11 @@ func build(name, goos, goarch string, ctx *context.Context) error {
|
||||
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
|
||||
}
|
||||
cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main)
|
||||
if err := run(goos, goarch, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDescription(t *testing.T) {
|
||||
func TestPipeDescription(t *testing.T) {
|
||||
assert.NotEmpty(t, Pipe{}.Description())
|
||||
}
|
||||
|
||||
|
30
pipeline/build/ldflags.go
Normal file
30
pipeline/build/ldflags.go
Normal file
@ -0,0 +1,30 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"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"),
|
||||
}
|
||||
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")
|
||||
assert.Contains(flags, "-X 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, "")
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user