1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/build/build.go

67 lines
1.3 KiB
Go
Raw Normal View History

2016-12-21 15:37:31 +02:00
package build
import (
"bytes"
2016-12-29 18:12:54 +02:00
"errors"
2016-12-30 13:27:35 +02:00
"log"
2016-12-21 18:42:23 +02:00
"os"
2016-12-21 15:37:31 +02:00
"os/exec"
"github.com/goreleaser/releaser/config"
2016-12-29 02:53:56 +02:00
"github.com/goreleaser/releaser/uname"
2016-12-29 18:12:54 +02:00
"golang.org/x/sync/errgroup"
2016-12-21 15:37:31 +02:00
)
2016-12-30 16:41:59 +02:00
// Pipe for build
2016-12-30 13:27:35 +02:00
type Pipe struct{}
2016-12-30 16:41:59 +02:00
// Name of the pipe
2016-12-30 13:27:35 +02:00
func (Pipe) Name() string {
return "Build"
}
2016-12-30 16:41:59 +02:00
// Run the pipe
func (Pipe) Run(config config.ProjectConfig) error {
2016-12-29 18:12:54 +02:00
var g errgroup.Group
2016-12-30 14:01:40 +02:00
for _, system := range config.Build.Oses {
2016-12-21 15:37:31 +02:00
for _, arch := range config.Build.Arches {
2016-12-30 14:01:40 +02:00
system := system
2016-12-29 18:12:54 +02:00
arch := arch
g.Go(func() error {
2016-12-30 14:01:40 +02:00
return build(system, arch, config)
2016-12-29 18:12:54 +02:00
})
2016-12-21 15:37:31 +02:00
}
}
2016-12-29 18:12:54 +02:00
return g.Wait()
}
2016-12-30 14:01:40 +02:00
func build(system, arch string, config config.ProjectConfig) error {
log.Println("Building", system+"/"+arch, "...")
2016-12-29 18:12:54 +02:00
cmd := exec.Command(
"go",
"build",
2016-12-30 13:27:35 +02:00
"-ldflags=-s -w -X main.version="+config.Git.CurrentTag,
2016-12-30 14:01:40 +02:00
"-o", target(system, arch, config.BinaryName),
2016-12-29 18:12:54 +02:00
config.Build.Main,
)
cmd.Env = append(
cmd.Env,
2016-12-30 14:01:40 +02:00
"GOOS="+system,
2016-12-29 18:12:54 +02:00
"GOARCH="+arch,
"GOROOT="+os.Getenv("GOROOT"),
"GOPATH="+os.Getenv("GOPATH"),
)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
err := cmd.Run()
if err != nil {
return errors.New(stdout.String())
}
2016-12-21 15:37:31 +02:00
return nil
}
2016-12-30 14:01:40 +02:00
func target(system, arch, binary string) string {
return "dist/" + binary + "_" + uname.FromGo(system) + "_" + uname.FromGo(arch) + "/" + binary
2016-12-21 15:37:31 +02:00
}