diff --git a/README.md b/README.md index 5f81b57af..db954d9a5 100644 --- a/README.md +++ b/README.md @@ -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: rice embed-go + post: ./script.sh ``` ### Archive customization diff --git a/config/config.go b/config/config.go index 1ff97ee08..af5489d41 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 729cfe3f4..435c46182 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -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, "GOARCH="+goarch) cmd.Env = append(cmd.Env, os.Environ()...) var stdout bytes.Buffer