mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-09 13:36:56 +02:00
refactor: allow builders to have custom defaults
This commit is contained in:
parent
42a8f0b194
commit
4e6982a524
@ -31,6 +31,7 @@ type Options struct {
|
||||
}
|
||||
|
||||
type Builder interface {
|
||||
Default(build config.Build) config.Build
|
||||
Build(ctx *context.Context, build config.Build, options Options) error
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,28 @@ func init() {
|
||||
type Builder struct {
|
||||
}
|
||||
|
||||
func (*Builder) Default(build config.Build) config.Build {
|
||||
if build.Main == "" {
|
||||
build.Main = "."
|
||||
}
|
||||
if len(build.Goos) == 0 {
|
||||
build.Goos = []string{"linux", "darwin"}
|
||||
}
|
||||
if len(build.Goarch) == 0 {
|
||||
build.Goarch = []string{"amd64", "386"}
|
||||
}
|
||||
if len(build.Goarm) == 0 {
|
||||
build.Goarm = []string{"6"}
|
||||
}
|
||||
if build.Ldflags == "" {
|
||||
build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"
|
||||
}
|
||||
if build.Lang == "go" && len(build.Targets) == 0 {
|
||||
build.Targets = matrix(build)
|
||||
}
|
||||
return build
|
||||
}
|
||||
|
||||
func (*Builder) Build(ctx *context.Context, cfg config.Build, options build.Options) error {
|
||||
if err := checkMain(ctx, cfg); err != nil {
|
||||
return err
|
||||
|
@ -1,4 +1,4 @@
|
||||
package buildmatrix
|
||||
package golang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -18,8 +18,7 @@ func (t target) String() string {
|
||||
return fmt.Sprintf("%s_%s", t.os, t.arch)
|
||||
}
|
||||
|
||||
// All returns all valid build targets for a given build
|
||||
func All(build config.Build) (result []string) {
|
||||
func matrix(build config.Build) (result []string) {
|
||||
var targets []target
|
||||
for _, target := range allBuildTargets(build) {
|
||||
if !valid(target) {
|
@ -1,4 +1,4 @@
|
||||
package buildmatrix
|
||||
package golang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -52,7 +52,7 @@ func TestAllBuildTargets(t *testing.T) {
|
||||
"freebsd_arm_7",
|
||||
"openbsd_386",
|
||||
"openbsd_amd64",
|
||||
}, All(build))
|
||||
}, matrix(build))
|
||||
}
|
||||
|
||||
func TestGoosGoarchCombos(t *testing.T) {
|
@ -15,7 +15,6 @@ import (
|
||||
|
||||
// langs to init
|
||||
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
|
||||
"github.com/goreleaser/goreleaser/internal/builders/golang/buildmatrix"
|
||||
)
|
||||
|
||||
// Pipe for build
|
||||
@ -56,25 +55,7 @@ func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
|
||||
if build.Binary == "" {
|
||||
build.Binary = ctx.Config.Release.GitHub.Name
|
||||
}
|
||||
if build.Main == "" {
|
||||
build.Main = "."
|
||||
}
|
||||
if len(build.Goos) == 0 {
|
||||
build.Goos = []string{"linux", "darwin"}
|
||||
}
|
||||
if len(build.Goarch) == 0 {
|
||||
build.Goarch = []string{"amd64", "386"}
|
||||
}
|
||||
if len(build.Goarm) == 0 {
|
||||
build.Goarm = []string{"6"}
|
||||
}
|
||||
if build.Ldflags == "" {
|
||||
build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"
|
||||
}
|
||||
if build.Lang == "go" && len(build.Targets) == 0 {
|
||||
build.Targets = buildmatrix.All(build)
|
||||
}
|
||||
return build
|
||||
return builders.For(build.Lang).Default(build)
|
||||
}
|
||||
|
||||
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
||||
|
Loading…
x
Reference in New Issue
Block a user