forked from slene/margo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_play.go
114 lines (98 loc) · 2.12 KB
/
m_play.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
package main
import (
"bytes"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"
)
type mPlay struct {
Args []string `json:"args"`
Dir string `json:"dir"`
Fn string `json:"fn"`
Src string `json:"src"`
Env map[string]string `json:"env"`
Cid string `json:"cid"`
BuildOnly bool `json:"build_only"`
b *Broker
}
// todo: send the client output as it comes
func (m *mPlay) Call() (interface{}, string) {
env := envSlice(m.Env)
dir, err := ioutil.TempDir(tempDir(m.Env), "play-")
if err != nil {
return nil, err.Error()
}
defer os.RemoveAll(dir)
tmpFn := ""
if m.Src != "" {
tmpFn = filepath.Join(dir, "a.go")
err = ioutil.WriteFile(tmpFn, []byte(m.Src), 0644)
if err != nil {
return nil, err.Error()
}
m.Dir = dir
}
if m.Args == nil {
m.Args = []string{}
}
if m.Dir == "" {
return nil, "missing directory"
}
if m.Cid == "" {
m.Cid = "play.auto." + numbers.nextString()
} else {
killCmd(m.Cid)
}
res := M{}
stdErr := bytes.NewBuffer(nil)
stdOut := bytes.NewBuffer(nil)
runCmd := func(name string, args ...string) (M, error) {
start := time.Now()
stdOut.Reset()
stdErr.Reset()
c := exec.Command(name, args...)
c.Stdout = stdOut
c.Stderr = stdErr
c.Dir = m.Dir
c.Env = env
watchCmd(m.Cid, c)
defer unwatchCmd(m.Cid)
err := c.Run()
res := M{
"tmpFn": tmpFn,
"fn": m.Fn,
"out": jData(stdOut.Bytes()),
"err": jData(stdErr.Bytes()),
"dur": time.Now().Sub(start).String(),
}
return res, err
}
if !m.BuildOnly {
pkg, err := build.ImportDir(m.Dir, 0)
if err != nil {
return res, err.Error()
}
if !pkg.IsCommand() {
res, err = runCmd("go", "test")
return res, errStr(err)
}
}
fn := filepath.Join(dir, "gosublime.a.exe")
res, err = runCmd("go", "build", "-o", fn)
if m.BuildOnly || err != nil {
return res, errStr(err)
}
res, err = runCmd(fn, m.Args...)
return res, errStr(err)
}
func init() {
registry.Register("play", func(b *Broker) Caller {
return &mPlay{
b: b,
Env: map[string]string{},
}
})
}