重新调整了构建时输出流和错误流的处理
TBD:构建出错时的 lints 结构组装
This commit is contained in:
Liang Ding 2017-03-15 22:26:12 +08:00
parent a89af9a374
commit 029bc7c917
1 changed files with 155 additions and 107 deletions

View File

@ -18,13 +18,10 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"math/rand"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
@ -148,8 +145,6 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
wsChannel.Refresh() wsChannel.Refresh()
} }
reader := bufio.NewReader(io.MultiReader(stdout, stderr))
if err := cmd.Start(); nil != err { if err := cmd.Start(); nil != err {
logger.Error(err) logger.Error(err)
result.Succ = false result.Succ = false
@ -157,22 +152,141 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
go func(runningId int) {
defer util.Recover()
defer cmd.Wait()
// logger.Debugf("User [%s, %s] is building [id=%d, dir=%s]", username, sid, runningId, curDir) // logger.Debugf("User [%s, %s] is building [id=%d, dir=%s]", username, sid, runningId, curDir)
// read all
buf, _ := ioutil.ReadAll(reader)
channelRet := map[string]interface{}{}
channelRet["cmd"] = "build" channelRet["cmd"] = "build"
channelRet["executable"] = executable channelRet["executable"] = executable
// FIXME: Daniel, build output process, should check stdout/stderr rather than length of output outReader := bufio.NewReader(stdout)
if 0 == len(buf) { // build success /////////
go func() {
defer util.Recover()
for {
wsChannel := session.OutputWS[sid]
if nil == wsChannel {
break
}
line, err := outReader.ReadString('\n')
if io.EOF == err {
break
}
if nil != err {
logger.Warn(err)
break
}
channelRet["output"] = line
err = wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
}
}()
errReader := bufio.NewReader(stderr)
for {
wsChannel := session.OutputWS[sid]
if nil == wsChannel {
break
}
line, err := errReader.ReadString('\n')
if io.EOF == err {
break
}
if nil != err {
logger.Warn(err)
break
}
// path process
errOutWithPath := parsePath(curDir, line)
channelRet["output"] = "<span class='stderr'>" + errOutWithPath + "</span>"
// lint process
// if lines[0][0] == '#' {
// lines = lines[1:] // skip the first line
// }
// lints := []*Lint{}
// for _, line := range lines {
// if len(line) < 1 {
// continue
// }
// if line[0] == '\t' {
// // append to the last 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:]
// index := strings.Index(left, ":")
// lineNo := 0
// msg := left
// if index >= 0 {
// lineNo, err = strconv.Atoi(left[:index])
// if nil != err {
// continue
// }
// msg = left[index+2:]
// }
// lint := &Lint{
// File: filepath.Join(curDir, file),
// LineNo: lineNo - 1,
// Severity: lintSeverityError,
// Msg: msg,
// }
// lints = append(lints, lint)
// }
// channelRet["lints"] = lints
err = wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
}
/////////////
// if 0 < len(stderrBuf) { // build error
// // build gutter lint
// errOut := string(stderrBuf)
// lines := strings.Split(errOut, "\n")
//
// }
if nil == cmd.Wait() {
channelRet["nextCmd"] = args["nextCmd"] channelRet["nextCmd"] = args["nextCmd"]
channelRet["output"] = "<span class='build-succ'>" + i18n.Get(locale, "build-succ").(string) + "</span>\n" channelRet["output"] = "<span class='build-succ'>" + i18n.Get(locale, "build-succ").(string) + "</span>\n"
@ -189,84 +303,18 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
logger.Warn(string(out)) logger.Warn(string(out))
} }
}() }()
} else { // build error } else {
// build gutter lint channelRet["output"] = "<span class='build-error'>" + i18n.Get(locale, "build-error").(string) + "</span>\n"
errOut := string(buf)
lines := strings.Split(errOut, "\n")
// path process
var errOutWithPath string
for _, line := range lines {
errOutWithPath += parsePath(curDir, line) + "\n"
} }
channelRet["output"] = "<span class='build-error'>" + i18n.Get(locale, "build-error").(string) + "</span>\n" +
"<span class='stderr'>" + errOutWithPath + "</span>"
// lint process
if lines[0][0] == '#' {
lines = lines[1:] // skip the first line
}
lints := []*Lint{}
for _, line := range lines {
if len(line) < 1 {
continue
}
if line[0] == '\t' {
// append to the last 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:]
index := strings.Index(left, ":")
lineNo := 0
msg := left
if index >= 0 {
lineNo, err = strconv.Atoi(left[:index])
if nil != err {
continue
}
msg = left[index+2:]
}
lint := &Lint{
File: filepath.Join(curDir, file),
LineNo: lineNo - 1,
Severity: lintSeverityError,
Msg: msg,
}
lints = append(lints, lint)
}
channelRet["lints"] = lints
}
if nil != session.OutputWS[sid] {
// logger.Debugf("User [%s, %s] 's build [id=%d, dir=%s] has done", username, sid, runningId, curDir)
wsChannel := session.OutputWS[sid] wsChannel := session.OutputWS[sid]
err := wsChannel.WriteJSON(&channelRet) if nil == wsChannel {
return
}
err = wsChannel.WriteJSON(&channelRet)
if nil != err { if nil != err {
logger.Warn(err) logger.Warn(err)
} }
wsChannel.Refresh() wsChannel.Refresh()
}
}(rand.Int())
} }