-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
restart.go
156 lines (134 loc) · 2.95 KB
/
restart.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
package mg
import (
"bytes"
"go/build"
"margo.sh/mgutil"
youtsuba "margo.sh/why_would_you_make_yotsuba_cry"
"os"
"os/exec"
"strings"
)
type rsIssues struct {
ActionType
issues IssueSet
}
type restartSupport struct {
ReducerType
q *mgutil.ChanQ
issues IssueSet
}
func (rs *restartSupport) RLabel() string {
return "Mg/Restart"
}
func (rs *restartSupport) RInit(mx *Ctx) {
go rs.onInit(mx)
}
func (rs *restartSupport) RCond(mx *Ctx) bool {
if len(rs.issues) != 0 || mx.ActionIs(rsIssues{}) {
return true
}
if mx.LangIs(Go) && mx.ActionIs(ViewSaved{}) {
return true
}
return false
}
func (rs *restartSupport) RMount(mx *Ctx) {
rs.q = mgutil.NewChanQ(1)
go rs.loop()
}
func (rs *restartSupport) RUnmount(mx *Ctx) {
rs.q.Close()
}
func (rs *restartSupport) Reduce(mx *Ctx) *State {
switch act := mx.Action.(type) {
case rsIssues:
rs.issues = act.issues
case ViewSaved:
rs.q.Put(mx)
}
return mx.State.AddIssues(rs.issues...)
}
func (rs *restartSupport) loop() {
for v := range rs.q.C() {
rs.onSave(v.(*Ctx))
}
}
func (rs *restartSupport) mgPkg(mx *Ctx) *build.Package {
v := mx.View
if !strings.HasSuffix(v.Path, ".go") || strings.HasSuffix(v.Path, "_test.go") {
return nil
}
src, _ := mx.View.ReadAll()
if bytes.Contains(src, []byte(`//margo:no-restart`)) {
return nil
}
pkg, _ := youtsuba.AgentBuildContext.ImportDir(mx.View.Dir(), 0)
if pkg == nil || pkg.ImportPath == "" {
return nil
}
imp := pkg.ImportPath + "/"
if strings.Contains(imp, "/cmd/") {
return nil
}
if !strings.HasPrefix(imp, "margo/") && !strings.HasPrefix(imp, "margo.sh/") {
return nil
}
return pkg
}
func (rs *restartSupport) onInit(mx *Ctx) {
if os.Getenv("MARGO_BUILD_ERROR") == "" {
return
}
res := rsIssues{issues: rs.slowLint(mx, nil)}
if len(res.issues) != 0 {
mx.Store.Dispatch(res)
}
}
func (rs *restartSupport) onSave(mx *Ctx) {
pkg := rs.mgPkg(mx)
if pkg == nil {
return
}
res := rsIssues{issues: rs.slowLint(mx, pkg)}
mx.Store.Dispatch(res)
if len(res.issues) == 0 {
mx.Log.Println(pkg.ImportPath, "saved with no issues, restarting")
mx.Store.Dispatch(Restart{})
}
}
func (rs *restartSupport) slowLint(mx *Ctx, pkg *build.Package) IssueSet {
defer mx.Begin(Task{Title: "prepping margo restart"}).Done()
cmds := []*exec.Cmd{
exec.Command("margo.sh", "build", mx.AgentName()),
}
if pkg != nil && pkg.ImportPath != "margo" {
cmds = append([]*exec.Cmd{
exec.Command("margo.sh", "ci", "-quick"),
}, cmds...)
}
buf := &bytes.Buffer{}
var err error
for _, cmd := range cmds {
cmd.Dir = mx.View.Dir()
cmd.Env = mx.Env.Environ()
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Run()
if err != nil {
break
}
}
output := buf.Bytes()
isuOut := &IssueOut{
Dir: mx.View.Dir(),
Patterns: mx.CommonPatterns(),
Base: Issue{Label: rs.RLabel()},
}
isuOut.Write(output)
isuOut.Close()
issues := isuOut.Issues()
if err != nil {
mx.Log.Printf(rs.RLabel()+": %s\n%s\n", err, output)
}
return issues
}