1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

149 lines
3.5 KiB
Go
Raw Normal View History

2018-01-22 01:10:17 -02:00
// Package build provides a pipe that can build binaries for several
// languages.
2016-12-21 11:37:31 -02:00
package build
import (
"fmt"
2018-01-26 19:26:28 -02:00
"os"
"os/exec"
2017-03-25 21:29:38 -03:00
"path/filepath"
"strings"
2016-12-21 11:37:31 -02:00
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
"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"
2018-01-21 14:31:08 -02:00
// langs to init
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
2016-12-21 11:37:31 -02:00
)
2016-12-30 12:41:59 -02:00
// Pipe for build
2016-12-30 09:27:35 -02:00
type Pipe struct{}
func (Pipe) String() string {
return "building binaries"
2016-12-30 09:27:35 -02:00
}
2016-12-30 12:41:59 -02:00
// Run the pipe
2017-01-14 14:06:57 -02:00
func (Pipe) Run(ctx *context.Context) error {
2017-06-27 19:20:08 -03:00
for _, build := range ctx.Config.Builds {
2017-06-27 19:36:36 -03:00
log.WithField("build", build).Debug("building")
2017-06-27 19:20:08 -03:00
if err := runPipeOnBuild(ctx, build); err != nil {
return err
}
}
return nil
}
// 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),
}
}
for id, cont := range ids {
if cont > 1 {
return fmt.Errorf("found %d builds with the ID '%s', please fix your config", cont, id)
}
2018-10-10 13:14:12 -03:00
}
return nil
}
func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
2018-01-21 14:31:08 -02:00
if build.Lang == "" {
build.Lang = "go"
}
if build.Binary == "" {
2018-08-16 14:25:02 -03:00
build.Binary = ctx.Config.ProjectName
}
if build.ID == "" {
build.ID = build.Binary
}
2018-02-12 20:53:57 -02:00
for k, v := range build.Env {
build.Env[k] = os.ExpandEnv(v)
}
2018-01-26 19:35:12 -02:00
return builders.For(build.Lang).WithDefaults(build)
}
2017-06-27 19:20:08 -03:00
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
2017-12-29 17:07:06 -02:00
if err := runHook(ctx, build.Env, build.Hooks.Pre); err != nil {
return errors.Wrap(err, "pre hook failed")
2017-03-23 14:20:24 -03:00
}
var g = semerrgroup.New(ctx.Parallelism)
2018-01-21 22:44:06 -02:00
for _, target := range build.Targets {
2017-04-24 14:27:21 -03:00
target := target
2017-07-01 12:27:13 -03:00
build := build
2017-04-24 14:27:21 -03:00
g.Go(func() error {
2017-07-01 12:27:13 -03:00
return doBuild(ctx, build, target)
2017-04-24 14:27:21 -03:00
})
2016-12-21 11:37:31 -02:00
}
2017-03-23 18:32:27 -03:00
if err := g.Wait(); err != nil {
return err
}
2017-12-29 17:07:06 -02:00
return errors.Wrap(runHook(ctx, build.Env, build.Hooks.Post), "post hook failed")
2017-04-09 10:43:23 -03:00
}
2017-12-29 17:07:06 -02:00
func runHook(ctx *context.Context, env []string, hook string) error {
2017-04-09 10:43:23 -03:00
if hook == "" {
return nil
2017-03-23 14:20:24 -03:00
}
sh, err := tmpl.New(ctx).WithEnvS(env).Apply(hook)
if err != nil {
return err
}
log.WithField("hook", sh).Info("running hook")
cmd := strings.Fields(sh)
env = append(env, ctx.Env.Strings()...)
2018-01-26 19:26:28 -02:00
return run(ctx, cmd, env)
2016-12-29 14:12:54 -02:00
}
2018-01-21 22:44:06 -02:00
func doBuild(ctx *context.Context, build config.Build, target string) error {
var ext = extFor(target)
binary, err := tmpl.New(ctx).Apply(build.Binary)
if err != nil {
return err
}
build.Binary = binary
2018-01-21 22:44:06 -02:00
var name = build.Binary + ext
var path = filepath.Join(ctx.Config.Dist, target, name)
log.WithField("binary", path).Info("building")
2018-01-21 14:31:08 -02:00
return builders.For(build.Lang).Build(ctx, build, builders.Options{
Target: target,
2018-01-21 22:44:06 -02:00
Name: name,
Path: path,
2018-01-21 14:31:08 -02:00
Ext: ext,
2017-12-17 15:24:49 -02:00
})
2016-12-21 11:37:31 -02:00
}
2018-01-21 22:44:06 -02:00
func extFor(target string) string {
if strings.Contains(target, "windows") {
return ".exe"
}
return ""
}
2018-01-26 19:26:28 -02:00
func run(ctx *context.Context, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var log = log.WithField("env", env).WithField("cmd", command)
cmd.Env = env
log.Debug("running")
2018-01-26 19:26:28 -02:00
if out, err := cmd.CombinedOutput(); err != nil {
log.WithError(err).Debug("failed")
return errors.New(string(out))
}
return nil
}