forked from KurokuLabs/margo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gocmd.go
251 lines (219 loc) · 5.09 KB
/
gocmd.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package golang
import (
"fmt"
"go/build"
"io/ioutil"
"margo.sh/mg"
"os"
"path/filepath"
"strings"
)
type GoCmd struct{ mg.ReducerType }
func (gc *GoCmd) Reduce(mx *mg.Ctx) *mg.State {
switch act := mx.Action.(type) {
case mg.QueryUserCmds:
return gc.userCmds(mx)
case mg.RunCmd:
return gc.runCmd(mx, act)
}
return mx.State
}
func (gc *GoCmd) userCmds(mx *mg.Ctx) *mg.State {
return mx.AddUserCmds(
mg.UserCmd{
Name: "go.play",
Title: "Go Play",
},
mg.UserCmd{
Name: "go.replay",
Title: "Go RePlay (single instance)",
},
)
}
func (gc *GoCmd) runCmd(mx *mg.Ctx, rc mg.RunCmd) *mg.State {
return mx.State.AddBuiltinCmds(
mg.BuiltinCmd{
Run: gc.goBuiltin,
Name: "go",
Desc: "Wrapper around the go command, adding linter support",
},
mg.BuiltinCmd{
Run: gc.playBuiltin,
Name: "go.play",
Desc: "Automatically build and run go commands or run go test for packages with support for linting and unsaved files",
},
mg.BuiltinCmd{
Run: gc.replayBuiltin,
Name: "go.replay",
Desc: "Wrapper around go.play limited to a single instance",
},
)
}
func (gc *GoCmd) goBuiltin(bx *mg.CmdCtx) *mg.State {
go gc.goTool(bx)
return bx.State
}
func (gc *GoCmd) playBuiltin(bx *mg.CmdCtx) *mg.State {
go gc.playTool(bx, "")
return bx.State
}
func (gc *GoCmd) replayBuiltin(bx *mg.CmdCtx) *mg.State {
v := bx.View
cid := ""
if v.Path == "" {
cid = v.Name
} else {
cid = v.Dir()
}
go gc.playTool(bx, "go.replay`"+cid+"`")
return bx.State
}
func (gc *GoCmd) goTool(bx *mg.CmdCtx) {
gx := newGoCmdCtx(bx, "go.builtin", "", "", "")
defer gx.Output.Close()
gx.run(gx.View)
}
func (gc *GoCmd) playTool(bx *mg.CmdCtx, cancelID string) {
origView := bx.View
bx, tDir, tFn, err := gc.playTempDir(bx)
gx := newGoCmdCtx(bx, "go.play", cancelID, tDir, tFn)
defer gx.Output.Close()
if err != nil {
fmt.Fprintf(gx.Output, "Error: %s\n", err)
}
if tDir == "" {
return
}
defer os.RemoveAll(tDir)
bld := BuildContext(gx.Ctx)
pkg, err := bld.ImportDir(gx.pkgDir, 0)
switch {
case err != nil:
fmt.Fprintln(gx.Output, "Error: cannot import package:", err)
case !pkg.IsCommand() || strings.HasSuffix(bx.View.Filename(), "_test.go"):
gc.playToolTest(gx, bld, origView)
default:
gc.playToolRun(gx, bld, origView)
}
}
func (gc *GoCmd) playTempDir(bx *mg.CmdCtx) (newBx *mg.CmdCtx, tDir string, tFn string, err error) {
tDir, err = mg.MkTempDir("go.play")
if err != nil {
return bx, "", "", fmt.Errorf("cannot MkTempDir: %s", err)
}
if !bx.LangIs(mg.Go) {
return bx, tDir, "", nil
}
v := bx.View
if v.Path != "" {
return bx, tDir, tFn, nil
}
tFn = filepath.Join(tDir, v.Name)
src, err := v.ReadAll()
if err == nil {
err = ioutil.WriteFile(tFn, src, 0600)
}
if err != nil {
return bx, tDir, "", fmt.Errorf("cannot create temp file: %s", err)
}
bx = bx.Copy(func(bx *mg.CmdCtx) {
bx.Ctx = bx.Ctx.Copy(func(mx *mg.Ctx) {
mx.State = mx.State.Copy(func(st *mg.State) {
st.View = st.View.Copy(func(v *mg.View) {
v.Path = tFn
})
})
})
})
return bx, tDir, tFn, nil
}
func (gc *GoCmd) playToolTest(gx *goCmdCtx, bld *build.Context, origView *mg.View) {
gx.Args = append([]string{"test"}, gx.Args...)
gx.run(origView)
}
func (gc *GoCmd) playToolRun(gx *goCmdCtx, bld *build.Context, origView *mg.View) {
nm := filepath.Base(origView.Name)
if origView.Path != "" {
nm = filepath.Base(origView.Dir())
}
args := gx.Args
exe := filepath.Join(gx.tDir, "margo.play~~"+nm+".exe")
gx.CmdCtx = gx.CmdCtx.Copy(func(bx *mg.CmdCtx) {
bx.Name = "go"
bx.Args = []string{"build", "-o", exe}
bx.Ctx = bx.Ctx.Copy(func(mx *mg.Ctx) {
mx.State = mx.State.Copy(func(st *mg.State) {
st.View = st.View.Copy(func(v *mg.View) {
v.Wd = v.Dir()
})
})
})
})
if err := gx.run(origView); err != nil {
return
}
gx.CmdCtx = gx.CmdCtx.Copy(func(bx *mg.CmdCtx) {
bx.Name = exe
bx.Args = args
bx.Ctx = bx.Ctx.Copy(func(mx *mg.Ctx) {
mx.State = mx.State.Copy(func(st *mg.State) {
st.View = origView
})
})
})
gx.RunProc()
}
type goCmdCtx struct {
*mg.CmdCtx
pkgDir string
key interface{}
iw *mg.IssueOut
tDir string
tFn string
}
func newGoCmdCtx(bx *mg.CmdCtx, label, cancelID string, tDir, tFn string) *goCmdCtx {
gx := &goCmdCtx{
pkgDir: bx.View.Dir(),
tDir: tDir,
tFn: tFn,
}
type Key struct{ label string }
gx.key = Key{label}
gx.iw = &mg.IssueOut{
Base: mg.Issue{Label: label},
Patterns: bx.CommonPatterns(),
Dir: gx.pkgDir,
}
gx.CmdCtx = bx.Copy(func(bx *mg.CmdCtx) {
bx.Name = "go"
bx.CancelID = cancelID
bx.Output = mg.OutputStreams{
bx.Output,
gx.iw,
}
})
return gx
}
func (gx *goCmdCtx) run(origView *mg.View) error {
p, err := gx.StartProc()
if err == nil {
err = p.Wait()
}
gx.iw.Flush()
issues := gx.iw.Issues()
for i, isu := range issues {
if isu.Path == gx.tFn {
isu.Name = origView.Name
isu.Path = origView.Path
}
issues[i] = isu
}
ik := mg.IssueKey{Key: gx.key}
if origView.Path == "" {
ik.Name = origView.Name
} else {
ik.Dir = origView.Dir()
}
gx.Store.Dispatch(mg.StoreIssues{IssueKey: ik, Issues: issues})
return err
}