-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
181 lines (172 loc) · 4.12 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"regexp"
"strings"
)
type BuildStep struct {
Scope string
Dir string
Cmds []string
Pure []string
}
func (step *BuildStep) Init() {
step.Pure = make([]string, len(step.Cmds))
copy(step.Pure, step.Cmds)
}
var goLine *regexp.Regexp = regexp.MustCompile(`(?m)^.+?(\./.+?\.go).*?$`)
var goFiles *regexp.Regexp = regexp.MustCompile(`(?m)\./.+$`)
func GetCmdFiles(cmd string) []string {
if !goLine.MatchString(cmd) {
return nil
}
// TODO: this does not support quoted files
nameChunk := goFiles.FindAllStringSubmatch(cmd, -1)
if len(nameChunk) == 1 && len(nameChunk[0]) == 1 {
return strings.Split(nameChunk[0][0], " ")
}
return nil
}
func (o *Og) GetBuildFiles(steps []*BuildStep) ([]string, error) {
var names []string
for _, step := range steps {
// TODO: this will *not* work on files inside strings?
for _, cmd := range step.Pure {
for _, name := range GetCmdFiles(cmd) {
if !strings.HasPrefix(name, "/") {
name = path.Join(step.Dir, name)
}
names = append(names, name)
}
}
}
return names, nil
}
func (o *Og) ParseBuild(cmd string, args ...string) ([]*BuildStep, error) {
args = append([]string{cmd, "-n"}, args...)
out, err := exec.Command("go", args...).CombinedOutput()
if err != nil {
fmt.Printf("%s", out)
return nil, err
}
scopeRe := regexp.MustCompile(`(?m)^# (_.+|command-line-arguments)$`)
cdRe := regexp.MustCompile(`(?m)^cd (.+)$`)
var steps []*BuildStep
var newStep *BuildStep
step := &BuildStep{Dir: "."}
for _, line := range bytes.Split(out, []byte("\n")) {
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
if bytes.HasPrefix(line, []byte("#")) {
tmp := scopeRe.FindSubmatch(line)
if len(tmp) >= 2 {
newStep = &BuildStep{Scope: string(tmp[1]), Dir: step.Dir}
}
} else {
tmp := cdRe.FindSubmatch(line)
if len(tmp) >= 2 {
cd := string(tmp[1])
if path.Clean(cd) != "." {
// TODO: make sure this doesn't mess up the directory
if len(step.Cmds) == 0 || step.Dir == "." {
step.Dir = cd
} else {
newStep = &BuildStep{Scope: step.Scope, Dir: cd}
}
}
}
if newStep == nil {
step.Cmds = append(step.Cmds, string(line))
}
}
if newStep != nil {
if step.Scope != "" && len(step.Cmds) > 0 {
step.Init()
steps = append(steps, step)
}
step = newStep
newStep = nil
}
}
if len(step.Cmds) > 0 {
step.Init()
steps = append(steps, step)
}
return steps, nil
}
func (o *Og) RewriteBuild(steps []*BuildStep) error {
// TODO: what do ./*.go lines look like with `go build -n` on Windows?
for _, step := range steps {
for i, cmd := range step.Cmds {
if !goLine.Match([]byte(cmd)) {
continue
}
files := GetCmdFiles(cmd)
for i, name := range files {
// TODO: instead of escaping, exec commands without a shell
name = path.Join("_", step.Dir, name)
files[i] = "\"" + path.Join("$WORK", escapeFilename(name)) + "\""
}
suffix := strings.Join(files, " ")
step.Cmds[i] = goFiles.ReplaceAllString(cmd, "") + suffix
}
}
return nil
}
func (o *Og) RunBuild(steps []*BuildStep) error {
// TODO: support "don't delete $WORK after build" flag
noop := false
for _, v := range o.Args {
if v == "-n" {
noop = true
break
}
}
if noop {
for _, step := range steps {
// TODO: make sure this path makes sense on Windows
fmt.Printf("\n#\n# %s\n#\n\n", step.Scope)
for _, line := range step.Cmds {
fmt.Printf("%s\n", line)
}
}
} else {
work, err := ioutil.TempDir("", "")
if err != nil {
return err
}
names, err := o.GetBuildFiles(steps)
if err != nil {
return err
}
err = o.GenFiles(work, false, names...)
if err != nil {
return err
}
env := os.Environ()
env = append(env, "WORK="+work)
for _, step := range steps {
for _, line := range step.Cmds {
cmd := exec.Command("sh", "-c", line)
cmd.Env = env
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil && !strings.HasPrefix(line, "mkdir") {
log.Fatal(err)
}
}
}
os.RemoveAll(work)
}
return nil
}