From a8c55a6f9b10540e57bae9dc604f484e5e593bb5 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 22 Oct 2014 17:47:53 +0800 Subject: [PATCH 1/2] . --- output/outputs.go | 210 +++++++++++++++++++++++++--------------------- static/js/wide.js | 8 +- 2 files changed, 122 insertions(+), 96 deletions(-) diff --git a/output/outputs.go b/output/outputs.go index 1436d76..59b523d 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -262,106 +262,128 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { return } - if data["succ"].(bool) { - reader := io.MultiReader(stdout, stderr) + if !data["succ"].(bool) { + return + } - if err := cmd.Start(); nil != err { + channelRet := map[string]interface{}{} + + if nil != session.OutputWS[sid] { + // 在前端 output 中显示“开始构建” + + channelRet["output"] = "go build" + channelRet["cmd"] = "pre-build" + + wsChannel := session.OutputWS[sid] + + err := wsChannel.Conn.WriteJSON(&channelRet) + if nil != err { glog.Error(err) - data["succ"] = false - return } - go func(runningId int) { - defer util.Recover() - defer cmd.Wait() - - glog.V(3).Infof("Session [%s] is building [id=%d, dir=%s]", sid, runningId, curDir) - - // 一次性读取 - buf := make([]byte, 1024*8) - count, _ := reader.Read(buf) - - channelRet := map[string]interface{}{} - channelRet["output"] = string(buf[:count]) - channelRet["cmd"] = "build" - channelRet["executable"] = executable - - if 0 == count { // 说明构建成功,没有错误信息输出 - // 设置下一次执行命令(前端会根据这个发送请求) - channelRet["nextCmd"] = args["nextCmd"] - - go func() { // 运行 go install,生成的库用于 gocode lib-path - cmd := exec.Command("go", "install") - cmd.Dir = curDir - - setCmdEnv(cmd, username) - - out, _ := cmd.CombinedOutput() - if len(out) > 0 { - glog.Warning(string(out)) - } - }() - } else { // 构建失败 - // 解析错误信息,返回给编辑器 gutter lint - errOut := string(buf[:count]) - lines := strings.Split(errOut, "\n") - - if lines[0][0] == '#' { - lines = lines[1:] // 跳过第一行 - } - - lints := []*Lint{} - - for _, line := range lines { - if len(line) < 1 { - continue - } - - if line[0] == '\t' { - // 添加到上一个 lint 中 - last := len(lints) - msg := lints[last-1].Msg - msg += line - - lints[last-1].Msg = msg - - continue - } - - file := line[:strings.Index(line, ":")] - left := line[strings.Index(line, ":")+1:] - lineNo, _ := strconv.Atoi(left[:strings.Index(left, ":")]) - msg := left[strings.Index(left, ":")+2:] - - lint := &Lint{ - File: file, - LineNo: lineNo - 1, - Severity: lintSeverityError, - Msg: msg, - } - - lints = append(lints, lint) - } - - channelRet["lints"] = lints - } - - if nil != session.OutputWS[sid] { - glog.V(3).Infof("Session [%s] 's build [id=%d, dir=%s] has done", sid, runningId, curDir) - - wsChannel := session.OutputWS[sid] - err := wsChannel.Conn.WriteJSON(&channelRet) - if nil != err { - glog.Error(err) - } - - // 更新通道最近使用时间 - wsChannel.Time = time.Now() - } - - }(rand.Int()) + // 更新通道最近使用时间 + wsChannel.Time = time.Now() } + + reader := io.MultiReader(stdout, stderr) + + if err := cmd.Start(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + go func(runningId int) { + defer util.Recover() + defer cmd.Wait() + + glog.V(3).Infof("Session [%s] is building [id=%d, dir=%s]", sid, runningId, curDir) + + // 一次性读取 + buf := make([]byte, 1024*8) + count, _ := reader.Read(buf) + + channelRet := map[string]interface{}{} + channelRet["output"] = string(buf[:count]) + channelRet["cmd"] = "build" + channelRet["executable"] = executable + + if 0 == count { // 说明构建成功,没有错误信息输出 + // 设置下一次执行命令(前端会根据这个发送请求) + channelRet["nextCmd"] = args["nextCmd"] + + go func() { // 运行 go install,生成的库用于 gocode lib-path + cmd := exec.Command("go", "install") + cmd.Dir = curDir + + setCmdEnv(cmd, username) + + out, _ := cmd.CombinedOutput() + if len(out) > 0 { + glog.Warning(string(out)) + } + }() + } else { // 构建失败 + // 解析错误信息,返回给编辑器 gutter lint + errOut := string(buf[:count]) + lines := strings.Split(errOut, "\n") + + if lines[0][0] == '#' { + lines = lines[1:] // 跳过第一行 + } + + lints := []*Lint{} + + for _, line := range lines { + if len(line) < 1 { + continue + } + + if line[0] == '\t' { + // 添加到上一个 lint 中 + last := len(lints) + msg := lints[last-1].Msg + msg += line + + lints[last-1].Msg = msg + + continue + } + + file := line[:strings.Index(line, ":")] + left := line[strings.Index(line, ":")+1:] + lineNo, _ := strconv.Atoi(left[:strings.Index(left, ":")]) + msg := left[strings.Index(left, ":")+2:] + + lint := &Lint{ + File: file, + LineNo: lineNo - 1, + Severity: lintSeverityError, + Msg: msg, + } + + lints = append(lints, lint) + } + + channelRet["lints"] = lints + } + + if nil != session.OutputWS[sid] { + glog.V(3).Infof("Session [%s] 's build [id=%d, dir=%s] has done", sid, runningId, curDir) + + wsChannel := session.OutputWS[sid] + err := wsChannel.Conn.WriteJSON(&channelRet) + if nil != err { + glog.Error(err) + } + + // 更新通道最近使用时间 + wsChannel.Time = time.Now() + } + + }(rand.Int()) } // go install. diff --git a/static/js/wide.js b/static/js/wide.js index 25c70d2..5a5a0a2 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -297,8 +297,8 @@ var wide = { if (goLintFound) { goLintFound = []; - } - + } + if ('run' === data.nextCmd) { var request = newWideRequest(); request.executable = data.executable; @@ -317,6 +317,8 @@ var wide = { }); } + // TODO: 重构成 switch-case + if ('run' === data.cmd) { // 正在运行 wide.fillOutput($('.bottom-window-group .output').text() + data.output); wide.curProcessId = data.pid; @@ -345,6 +347,8 @@ var wide = { CodeMirror.signal(wide.curEditor, "change", wide.curEditor); } else if ('go get' === data.cmd || 'go install' === data.cmd) { wide.fillOutput($('.bottom-window-group .output').text() + data.output); + } else if ('pre-build' === data.cmd) { + wide.fillOutput(data.output); } }; outputWS.onclose = function (e) { From 08fe6aa03d7893604e59b1b07f243e562dbe4c13 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 22 Oct 2014 17:55:22 +0800 Subject: [PATCH 2/2] . --- output/outputs.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/output/outputs.go b/output/outputs.go index 59b523d..57ef856 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -306,13 +306,13 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { count, _ := reader.Read(buf) channelRet := map[string]interface{}{} - channelRet["output"] = string(buf[:count]) channelRet["cmd"] = "build" channelRet["executable"] = executable if 0 == count { // 说明构建成功,没有错误信息输出 // 设置下一次执行命令(前端会根据这个发送请求) channelRet["nextCmd"] = args["nextCmd"] + channelRet["output"] = "Build Succ" go func() { // 运行 go install,生成的库用于 gocode lib-path cmd := exec.Command("go", "install") @@ -328,6 +328,8 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { } else { // 构建失败 // 解析错误信息,返回给编辑器 gutter lint errOut := string(buf[:count]) + channelRet["output"] = "Build Failed\n" + errOut + lines := strings.Split(errOut, "\n") if lines[0][0] == '#' {