1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00

75 lines
1.5 KiB
Go
Raw Normal View History

2016-12-21 11:37:31 -02:00
package build
import (
"bytes"
2016-12-29 14:12:54 -02:00
"errors"
2016-12-30 09:27:35 -02:00
"log"
2016-12-21 14:42:23 -02:00
"os"
2016-12-21 11:37:31 -02:00
"os/exec"
2017-01-14 12:34:22 -02:00
"github.com/goreleaser/releaser/context"
2016-12-29 14:12:54 -02:00
"golang.org/x/sync/errgroup"
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{}
2016-12-30 12:41:59 -02:00
// Name of the pipe
2016-12-30 09:27:35 -02:00
func (Pipe) Name() string {
return "Build"
}
2016-12-30 12:41:59 -02:00
// Run the pipe
2017-01-14 12:34:22 -02:00
func (Pipe) Run(context *context.Context) error {
2016-12-29 14:12:54 -02:00
var g errgroup.Group
2017-01-14 12:34:22 -02:00
for _, system := range context.Config.Build.Oses {
for _, arch := range context.Config.Build.Arches {
2016-12-30 10:01:40 -02:00
system := system
2016-12-29 14:12:54 -02:00
arch := arch
2017-01-14 12:34:22 -02:00
name, err := context.Config.ArchiveName(system, arch)
if err != nil {
return err
}
context.Archives = append(context.Archives, name)
2016-12-29 14:12:54 -02:00
g.Go(func() error {
2017-01-14 12:34:22 -02:00
return build(name, system, arch, context)
2016-12-29 14:12:54 -02:00
})
2016-12-21 11:37:31 -02:00
}
}
2016-12-29 14:12:54 -02:00
return g.Wait()
}
2017-01-14 12:34:22 -02:00
func build(name, system, arch string, context *context.Context) error {
ldflags := context.Config.Build.Ldflags + " -X main.version=" + context.Git.CurrentTag
2017-01-14 12:51:09 -02:00
output := "dist/" + name + "/" + context.Config.BinaryName + extFor(system)
2017-01-11 20:25:52 -02:00
log.Println("Building", output, "...")
2016-12-29 14:12:54 -02:00
cmd := exec.Command(
"go",
"build",
2017-01-11 20:25:52 -02:00
"-ldflags="+ldflags,
"-o", output,
2017-01-14 12:34:22 -02:00
context.Config.Build.Main,
2016-12-29 14:12:54 -02:00
)
cmd.Env = append(
cmd.Env,
2016-12-30 10:01:40 -02:00
"GOOS="+system,
2016-12-29 14:12:54 -02:00
"GOARCH="+arch,
"GOROOT="+os.Getenv("GOROOT"),
"GOPATH="+os.Getenv("GOPATH"),
)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
2017-01-04 10:13:49 -02:00
if err := cmd.Run(); err != nil {
2016-12-29 14:12:54 -02:00
return errors.New(stdout.String())
}
2016-12-21 11:37:31 -02:00
return nil
}
2017-01-14 12:51:09 -02:00
func extFor(system string) string {
if system == "windows" {
return ".exe"
}
return ""
}