1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-30 04:50:45 +02:00

feat: build id (#1008)

* feat: build id

* test: added more tests

* fix: imports
This commit is contained in:
Carlos Alexandro Becker 2019-04-14 15:16:01 -03:00 committed by GitHub
parent 1a971c0d9a
commit 15475c6484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 4 deletions

View File

@ -106,6 +106,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
Extra: map[string]interface{}{
"Binary": build.Binary,
"Ext": options.Ext,
"ID": build.ID,
},
})
return nil

View File

@ -27,6 +27,7 @@ func TestWithDefaults(t *testing.T) {
}{
"full": {
build: config.Build{
ID: "foo",
Binary: "foo",
Goos: []string{
"linux",
@ -50,6 +51,7 @@ func TestWithDefaults(t *testing.T) {
},
"empty": {
build: config.Build{
ID: "foo2",
Binary: "foo",
},
targets: []string{
@ -81,6 +83,7 @@ func TestBuild(t *testing.T) {
var config = config.Project{
Builds: []config.Build{
{
ID: "foo",
Env: []string{"GO111MODULE=off"},
Binary: "foo",
Targets: []string{
@ -122,6 +125,7 @@ func TestBuild(t *testing.T) {
Extra: map[string]interface{}{
"Ext": "",
"Binary": "foo",
"ID": "foo",
},
},
{
@ -133,6 +137,7 @@ func TestBuild(t *testing.T) {
Extra: map[string]interface{}{
"Ext": "",
"Binary": "foo",
"ID": "foo",
},
},
{
@ -145,6 +150,7 @@ func TestBuild(t *testing.T) {
Extra: map[string]interface{}{
"Ext": "",
"Binary": "foo",
"ID": "foo",
},
},
{
@ -156,6 +162,7 @@ func TestBuild(t *testing.T) {
Extra: map[string]interface{}{
"Ext": ".exe",
"Binary": "foo",
"ID": "foo",
},
},
})
@ -168,6 +175,7 @@ func TestBuildFailed(t *testing.T) {
var config = config.Project{
Builds: []config.Build{
{
ID: "buildid",
Flags: []string{"-flag-that-dont-exists-to-force-failure"},
Targets: []string{
runtimeTarget,
@ -192,6 +200,7 @@ func TestBuildInvalidTarget(t *testing.T) {
var config = config.Project{
Builds: []config.Build{
{
ID: "foo",
Binary: "foo",
Targets: []string{target},
},

View File

@ -3,19 +3,19 @@
package build
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
builders "github.com/goreleaser/goreleaser/pkg/build"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/pkg/errors"
// langs to init
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
@ -41,16 +41,20 @@ func (Pipe) Run(ctx *context.Context) error {
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
var ids = map[string]int{}
for i, build := range ctx.Config.Builds {
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
ids[ctx.Config.Builds[i].ID]++
}
if len(ctx.Config.Builds) == 0 {
ctx.Config.Builds = []config.Build{
buildWithDefaults(ctx, ctx.Config.SingleBuild),
}
}
if len(ctx.Config.Builds) > 1 {
log.Warn("you have more than 1 build setup: please make sure it is a not a typo on your config")
for id, cont := range ids {
if cont > 1 {
return fmt.Errorf("there are more than %d builds with the ID '%s', please fix your config", cont, id)
}
}
return nil
}
@ -62,6 +66,9 @@ func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
if build.Binary == "" {
build.Binary = ctx.Config.ProjectName
}
if build.ID == "" {
build.ID = build.Binary
}
for k, v := range build.Env {
build.Env[k] = os.ExpandEnv(v)
}

View File

@ -218,16 +218,36 @@ func TestDefaultEmptyBuild(t *testing.T) {
assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}", build.Ldflags[0])
}
func TestSeveralBuildsWithTheSameID(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "a",
Binary: "bar",
},
{
ID: "a",
Binary: "foo",
},
},
},
}
assert.EqualError(t, Pipe{}.Default(ctx), "there are more than 2 builds with the ID 'a', please fix your config")
}
func TestDefaultPartialBuilds(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Builds: []config.Build{
{
ID: "build1",
Binary: "bar",
Goos: []string{"linux"},
Main: "./cmd/main.go",
},
{
ID: "build2",
Binary: "foo",
Ldflags: []string{"-s -w"},
Goarch: []string{"386"},

View File

@ -123,6 +123,7 @@ func (a *FlagArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Build contains the build configuration section
type Build struct {
ID string `yaml:",omitempty"`
Goos []string `yaml:",omitempty"`
Goarch []string `yaml:",omitempty"`
Goarm []string `yaml:",omitempty"`

View File

@ -17,6 +17,10 @@ Here is a commented `builds` section with all fields specified:
builds:
# You can have multiple builds defined as a yaml list
-
# ID of the build.
# Defaults to the binary name.
id: "my-build"
# Path to main.go file or main package.
# Default is `.`.
main: ./cmd/main.go