-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder.go
66 lines (54 loc) · 1.4 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gaper
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// Builder is a interface for the build process
type Builder interface {
Build() error
Binary() string
}
type builder struct {
dir string
binary string
wd string
buildArgs []string
}
// NewBuilder creates a new builder
func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
// resolve bin name by current folder name
if bin == "" {
bin = filepath.Base(wd)
}
// does not work on Windows without the ".exe" extension
if runtime.GOOS == OSWindows {
// check if it already has the .exe extension
if !strings.HasSuffix(bin, ".exe") {
bin += ".exe"
}
}
return &builder{dir: dir, binary: bin, wd: wd, buildArgs: buildArgs}
}
// Binary returns its build binary's path
func (b *builder) Binary() string {
return b.binary
}
// Build the Golang project set for this builder
func (b *builder) Build() error {
logger.Info("Building program")
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
logger.Debug("Build command", args)
command := exec.Command(args[0], args[1:]...) // nolint gas
command.Dir = b.dir
output, err := command.CombinedOutput()
if err != nil {
return fmt.Errorf("build failed with %v\n%s", err, output)
}
if !command.ProcessState.Success() {
return fmt.Errorf("error building: %s", output)
}
return nil
}