forked from codegangsta/gin
/
builder.go
73 lines (58 loc) · 1.29 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
67
68
69
70
71
72
73
package gin
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type Builder interface {
Build() error
Binary() string
Errors() string
}
type builder struct {
dir string
binary string
errors string
useGodep bool
wd string
buildArgs []string
}
func NewBuilder(dir string, bin string, useGodep bool, wd string, buildArgs []string) Builder {
if len(bin) == 0 {
bin = "bin"
}
// does not work on Windows without the ".exe" extension
if runtime.GOOS == "windows" {
if !strings.HasSuffix(bin, ".exe") { // check if it already has the .exe extension
bin += ".exe"
}
}
return &builder{dir: dir, binary: bin, useGodep: useGodep, wd: wd, buildArgs: buildArgs}
}
func (b *builder) Binary() string {
return b.binary
}
func (b *builder) Errors() string {
return b.errors
}
func (b *builder) Build() error {
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
var command *exec.Cmd
if b.useGodep {
args = append([]string{"godep"}, args...)
}
command = exec.Command(args[0], args[1:]...)
command.Dir = b.dir
output, err := command.CombinedOutput()
if command.ProcessState.Success() {
b.errors = ""
} else {
b.errors = string(output)
}
if len(b.errors) > 0 {
return fmt.Errorf(b.errors)
}
return err
}