1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

added hooks support

add support for pre and post hooks on build pipe
This commit is contained in:
Carlos Alexandro Becker 2017-01-21 20:02:51 -02:00
parent 9a2effeeec
commit 26a2471526
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
3 changed files with 36 additions and 7 deletions

View File

@ -179,6 +179,13 @@ build:
# Defaults are 386 and amd64
goarch:
- amd64
# Hooks can be used to customize the final binary, for example, to run
# generator or whatever you want.
# Default is both hooks empty.
hooks:
pre: touch somefile
post: ./script.sh
```
### Archive customization

View File

@ -14,6 +14,12 @@ type Homebrew struct {
Dependencies []string
}
// Hooks define actions to run before and/or after something
type Hooks struct {
Pre string
Post string
}
// Build contains the build configuration section
type Build struct {
Goos []string
@ -21,6 +27,7 @@ type Build struct {
Main string
Ldflags string
BinaryName string `yaml:"binary_name"`
Hooks Hooks
}
// Archive config used for the archive

View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/context"
"golang.org/x/sync/errgroup"
@ -43,13 +44,27 @@ func build(name, goos, goarch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos)
log.Println("Building", output)
cmd := exec.Command(
"go",
"build",
"-ldflags="+ldflags,
"-o", output,
ctx.Config.Build.Main,
)
if ctx.Config.Build.Hooks.Pre != "" {
cmd := strings.Split(ctx.Config.Build.Hooks.Pre, " ")
if err := run(goos, goarch, cmd); err != nil {
return err
}
}
cmd := []string{"go", "build", "-ldflags=" + ldflags, "-o", output, ctx.Config.Build.Main}
if err := run(goos, goarch, cmd); err != nil {
return err
}
if ctx.Config.Build.Hooks.Post != "" {
cmd := strings.Split(ctx.Config.Build.Hooks.Post, " ")
if err := run(goos, goarch, cmd); err != nil {
return err
}
}
return nil
}
func run(goos, goarch string, command []string) error {
cmd := exec.Command(command[0], command[1:]...)
cmd.Env = append(
cmd.Env,
"GOOS="+goos,