2015-01-18 08:59:10 +03:00
|
|
|
// Copyright (c) 2014-2015, b3log.org
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-12-07 06:33:56 +03:00
|
|
|
// Package output includes build, run and go tool related manipulations.
|
2014-08-18 17:45:43 +04:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-10-28 05:13:00 +03:00
|
|
|
"path/filepath"
|
2014-08-22 07:57:05 +04:00
|
|
|
"runtime"
|
2014-09-09 11:33:12 +04:00
|
|
|
"strconv"
|
2014-08-22 07:57:05 +04:00
|
|
|
"strings"
|
2014-09-16 11:06:52 +04:00
|
|
|
"time"
|
2014-09-08 07:37:34 +04:00
|
|
|
|
|
|
|
"github.com/b3log/wide/conf"
|
2014-12-13 13:47:41 +03:00
|
|
|
"github.com/b3log/wide/log"
|
2014-09-17 10:35:48 +04:00
|
|
|
"github.com/b3log/wide/session"
|
2014-09-08 07:37:34 +04:00
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
"github.com/gorilla/websocket"
|
2014-08-18 17:45:43 +04:00
|
|
|
)
|
|
|
|
|
2014-09-26 13:36:12 +04:00
|
|
|
const (
|
2014-10-29 13:15:18 +03:00
|
|
|
lintSeverityError = "error" // lint severity: error
|
|
|
|
lintSeverityWarn = "warning" // lint severity: warning
|
2014-09-26 13:36:12 +04:00
|
|
|
)
|
|
|
|
|
2014-12-13 13:47:41 +03:00
|
|
|
// Logger.
|
|
|
|
var logger = log.NewLogger(os.Stdout)
|
|
|
|
|
2014-12-07 06:33:56 +03:00
|
|
|
// Lint represents a code lint.
|
2014-09-26 13:36:12 +04:00
|
|
|
type Lint struct {
|
|
|
|
File string `json:"file"`
|
|
|
|
LineNo int `json:"lineNo"`
|
|
|
|
Severity string `json:"severity"`
|
2014-10-28 04:12:32 +03:00
|
|
|
Msg string `json:"msg"`
|
2014-09-26 13:36:12 +04:00
|
|
|
}
|
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
// WSHandler handles request of creating output channel.
|
2014-08-18 17:45:43 +04:00
|
|
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
2014-09-17 06:54:57 +04:00
|
|
|
sid := r.URL.Query()["sid"][0]
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-09-16 11:06:52 +04:00
|
|
|
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
|
|
|
|
|
2014-08-18 17:45:43 +04:00
|
|
|
ret := map[string]interface{}{"output": "Ouput initialized", "cmd": "init-output"}
|
2014-11-20 08:59:08 +03:00
|
|
|
err := wsChan.WriteJSON(&ret)
|
2014-11-20 06:30:18 +03:00
|
|
|
if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session.OutputWS[sid] = &wsChan
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-12-14 18:05:54 +03:00
|
|
|
logger.Tracef("Open a new [Output] with session [%s], %d", sid, len(session.OutputWS))
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
2014-12-03 10:26:52 +03:00
|
|
|
// parsePath parses file path in the specified outputLine, and returns new line with front-end friendly.
|
|
|
|
func parsePath(curDir, outputLine string) string {
|
|
|
|
index := strings.Index(outputLine, " ")
|
|
|
|
if -1 == index || index >= len(outputLine) {
|
|
|
|
return outputLine
|
|
|
|
}
|
|
|
|
|
|
|
|
pathPart := outputLine[:index]
|
|
|
|
msgPart := outputLine[index:]
|
|
|
|
|
|
|
|
parts := strings.Split(pathPart, ":")
|
|
|
|
if len(parts) < 2 { // no file path info (line & column) found
|
|
|
|
return outputLine
|
|
|
|
}
|
|
|
|
|
|
|
|
file := parts[0]
|
|
|
|
line := parts[1]
|
|
|
|
if _, err := strconv.Atoi(line); nil != err {
|
|
|
|
return outputLine
|
|
|
|
}
|
|
|
|
|
|
|
|
column := "0"
|
|
|
|
hasColumn := 4 == len(parts)
|
|
|
|
if hasColumn {
|
|
|
|
column = parts[2]
|
|
|
|
}
|
|
|
|
|
2014-12-03 12:00:24 +03:00
|
|
|
tagStart := `<span class="path" data-path="` + filepath.Join(curDir, file) + `" data-line="` + line +
|
2014-12-03 10:26:52 +03:00
|
|
|
`" data-column="` + column + `">`
|
|
|
|
text := file + ":" + line
|
|
|
|
if hasColumn {
|
|
|
|
text += ":" + column
|
|
|
|
}
|
|
|
|
tagEnd := "</span>:"
|
|
|
|
|
|
|
|
return tagStart + text + tagEnd + msgPart
|
|
|
|
}
|
|
|
|
|
2014-09-02 20:26:10 +04:00
|
|
|
func setCmdEnv(cmd *exec.Cmd, username string) {
|
2014-12-19 10:43:59 +03:00
|
|
|
userWorkspace := conf.GetUserWorkspace(username)
|
2014-09-02 20:26:10 +04:00
|
|
|
|
|
|
|
cmd.Env = append(cmd.Env,
|
2014-11-12 09:49:14 +03:00
|
|
|
"GOPATH="+userWorkspace,
|
2014-09-04 19:48:49 +04:00
|
|
|
"GOOS="+runtime.GOOS,
|
|
|
|
"GOARCH="+runtime.GOARCH,
|
2014-09-05 08:18:50 +04:00
|
|
|
"GOROOT="+runtime.GOROOT(),
|
|
|
|
"PATH="+os.Getenv("PATH"))
|
2015-01-12 09:49:52 +03:00
|
|
|
|
|
|
|
if util.OS.IsWindows() {
|
|
|
|
// FIXME: for some weird issues on Windows, such as: The requested service provider could not be loaded or initialized.
|
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
|
|
|
}
|
2014-08-28 06:19:00 +04:00
|
|
|
}
|