-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.go
140 lines (129 loc) · 2.93 KB
/
build.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
_ "flag"
"html/template"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
)
type Chunk struct {
env *Environ
buf []byte
index int
}
func (c *Chunk) createData(chunks chan *Chunk) {
cmd := exec.Command("go-bindata", "-func="+c.FuncName(), "-nomemcopy")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatalln("Failed to get bindata input pipe.", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalln("Failed to get bindata output pipe.", err)
}
err = cmd.Start()
if err != nil {
log.Fatalln("Failed to start bindata.", err)
}
_, err = stdin.Write(c.buf)
if err != nil {
log.Fatalln("Failed to write bindata input pipe.", err)
}
err = stdin.Close()
if err != nil {
log.Fatalln("Failed to close bindata input pipe.", err)
}
f, err := os.Create(c.outPath())
if err != nil {
log.Fatalln("Failed to create output file.", err)
}
_, err = io.Copy(f, stdout)
if err != nil {
log.Fatalln("Failed to write output file.", err)
}
f.Close()
err = cmd.Wait()
if err != nil {
log.Fatalln("Failed to wait for bindata.", err)
}
chunks <- c
}
func (c *Chunk) FuncName() string {
return c.env.buildFuncPrefix + strconv.Itoa(c.index)
}
func (c *Chunk) outPath() string {
return c.env.buildPkgPath(c.FuncName() + ".go")
}
type Builder struct {
env *Environ
chunks []*Chunk
}
func NewBuilder(env *Environ) *Builder {
b := &Builder{env: env}
b.splitChunks()
return b
}
func (b *Builder) splitChunks() {
f, err := os.Open(b.env.zipPath)
if err != nil {
log.Fatalln("Failed to open archive for splitting.", err)
}
defer f.Close()
cs := []*Chunk{}
ci := 0
for {
c := &Chunk{env: b.env, buf: make([]byte, b.env.buildChunkSize), index: ci}
n, err := f.Read(c.buf)
if err != nil && err != io.EOF {
log.Fatalln("Failed to read archive for splitting.", err)
}
cs = append(cs, c)
if n == 0 {
break
}
ci++
}
b.chunks = cs
}
func (b *Builder) Generate() {
ch := make(chan *Chunk)
for _, c := range b.chunks {
go c.createData(ch)
}
rcs := 0
for rcs < len(b.chunks) {
<-ch
rcs++
}
}
func (b *Builder) CopyMain() {
dst, err := os.Create(b.env.buildPkgPath("main.go"))
if err != nil {
log.Fatalln("Failed to create main.go.", err)
}
src := b.env.mainTemplatePath
tmpl, err := template.ParseFiles(src)
if err != nil {
log.Fatalln("Failed to parse main template.", err)
}
if err := tmpl.Execute(dst, b.chunks); err != nil {
log.Fatalln("Failed to write main template.", err)
}
}
func (b *Builder) Compile() {
files, err := filepath.Glob(b.env.buildPkgPath(b.env.buildFuncPrefix + "*.go"))
if err != nil {
log.Fatalln("Failed to glob our bindata.", err)
}
main := b.env.buildPkgPath("main.go")
args := append([]string{"build", "-o", b.env.buildBinExec, main}, files...)
cmd := exec.Command("go", args...)
cmd.Env = []string{"GOPATH=" + b.env.buildDir}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalln("Failed run go.", err, string(out))
}
}