This commit is contained in:
Liang Ding 2014-09-09 15:33:12 +08:00
parent cef9652fae
commit 1caedf8c41
3 changed files with 43 additions and 15 deletions

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strconv"
"strings" "strings"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
@ -199,6 +200,12 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 1024*8) buf := make([]byte, 1024*8)
count, _ := reader.Read(buf) count, _ := reader.Read(buf)
channelRet := map[string]interface{}{}
channelRet["output"] = string(buf[:count])
channelRet["cmd"] = "build"
channelRet["nextCmd"] = "run"
channelRet["executable"] = executable
if 0 == count { // 说明构建成功,没有错误信息输出 if 0 == count { // 说明构建成功,没有错误信息输出
go func() { // 运行 go install生成的库用于 gocode lib-path go func() { // 运行 go install生成的库用于 gocode lib-path
cmd := exec.Command("go", "install") cmd := exec.Command("go", "install")
@ -213,16 +220,29 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
}() }()
} else { // 构建失败 } else { // 构建失败
// 解析错误信息,返回给编辑器 gutter lint // 解析错误信息,返回给编辑器 gutter lint
lines := strings.Split(string(buf[:count]), "\n")[1:]
lints := []map[string]interface{}{}
for _, line := range lines {
if len(line) <= 1 {
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 := map[string]interface{}{}
lint["file"] = file
lint["lineNo"] = lineNo - 1
lint["msg"] = msg
lints = append(lints, lint)
}
channelRet["lints"] = lints
} }
channelRet := map[string]interface{}{}
channelRet["output"] = string(buf[:count])
channelRet["cmd"] = "build"
channelRet["nextCmd"] = "run"
channelRet["executable"] = executable
if nil != outputWS[sid] { if nil != outputWS[sid] {
glog.V(3).Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath) glog.V(3).Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath)

View File

@ -1,16 +1,12 @@
goLintFound = [];
(function(mod) { (function(mod) {
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { })(function(CodeMirror) {
"use strict"; "use strict";
CodeMirror.registerHelper("lint", "go", function(text) { CodeMirror.registerHelper("lint", "go", function(text) {
var found = []; return goLintFound;
found.push({from: CodeMirror.Pos(1, 1),
to: CodeMirror.Pos(1, 10),
message: "test commpile err"});
return found;
}); });
}); });

View File

@ -7,12 +7,24 @@ outputWS.onmessage = function(e) {
console.log('[output onmessage]' + e.data); console.log('[output onmessage]' + e.data);
var data = JSON.parse(e.data); var data = JSON.parse(e.data);
if (goLintFound) {
goLintFound = [];
}
if ('run' === data.cmd) { if ('run' === data.cmd) {
$('#output').text($('#output').text() + data.output); $('#output').text($('#output').text() + data.output);
} else if ('build' === data.cmd) { } else if ('build' === data.cmd) {
$('#output').text(data.output); $('#output').text(data.output);
if (0 !== data.output.length) { // 说明编译有错误输出 if (0 !== data.output.length) { // 说明编译有错误输出
for (var i = 0; i < data.lints.length; i++) {
var lint = data.lints[i];
goLintFound.push({from: CodeMirror.Pos(lint.lineNo, 0),
to: CodeMirror.Pos(lint.lineNo, 0),
message: lint.msg});
}
return; return;
} }
} else if ('go get' === data.cmd) { } else if ('go get' === data.cmd) {