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/exec"
"runtime"
"strconv"
"strings"
"github.com/b3log/wide/conf"
@ -199,6 +200,12 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 1024*8)
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 { // 说明构建成功,没有错误信息输出
go func() { // 运行 go install生成的库用于 gocode lib-path
cmd := exec.Command("go", "install")
@ -213,16 +220,29 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
}()
} else { // 构建失败
// 解析错误信息,返回给编辑器 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] {
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) {
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
CodeMirror.registerHelper("lint", "go", function(text) {
var found = [];
found.push({from: CodeMirror.Pos(1, 1),
to: CodeMirror.Pos(1, 10),
message: "test commpile err"});
return found;
return goLintFound;
});
});

View File

@ -7,12 +7,24 @@ outputWS.onmessage = function(e) {
console.log('[output onmessage]' + e.data);
var data = JSON.parse(e.data);
if (goLintFound) {
goLintFound = [];
}
if ('run' === data.cmd) {
$('#output').text($('#output').text() + data.output);
} else if ('build' === data.cmd) {
$('#output').text(data.output);
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;
}
} else if ('go get' === data.cmd) {
@ -165,7 +177,7 @@ var wide = {
// 在客户端浏览器中进行 JSON 格式化
var json = JSON.parse(wide.curEditor.getValue());
wide.curEditor.setValue(JSON.stringify(json, "", " "));
this.save();
} catch (e) {
delete e;