wide/output/outputs.go

133 lines
3.6 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, 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
//
2018-03-12 07:28:33 +03:00
// https://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"
"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
2019-05-24 16:04:25 +03:00
"github.com/b3log/gulu"
2014-09-08 07:37:34 +04:00
"github.com/b3log/wide/conf"
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.
2019-05-24 16:04:25 +03:00
var logger = gulu.Log.NewLogger(os.Stdout)
2014-12-13 13:47:41 +03:00
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"`
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]
}
tagStart := `<span class="path" data-path="` + filepath.ToSlash(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
}
2019-05-16 18:17:25 +03:00
func setCmdEnv(cmd *exec.Cmd, uid string) {
userWorkspace := conf.GetUserWorkspace(uid)
2019-05-16 07:41:39 +03:00
cache, err := os.UserCacheDir()
if nil != err {
logger.Warnf("Get user cache dir failed [" + err.Error() + "]")
cache = os.TempDir()
}
2014-09-02 20:26:10 +04:00
cmd.Env = append(cmd.Env,
2019-05-20 12:59:10 +03:00
"GOPROXY=https://goproxy.cn",
2019-05-20 12:56:58 +03:00
"GO111MODULE=on",
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(),
2019-05-16 07:41:39 +03:00
"GOCACHE="+cache,
2014-09-05 08:18:50 +04:00
"PATH="+os.Getenv("PATH"))
2015-01-12 09:49:52 +03:00
2019-05-24 16:04:25 +03:00
if gulu.OS.IsWindows() {
2015-01-12 09:49:52 +03:00
// 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()...)
} else {
2018-12-30 09:57:43 +03:00
// 编译链接时找不到依赖的动态库 https://github.com/b3log/wide/issues/352
cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+os.Getenv("LD_LIBRARY_PATH"))
2015-01-12 09:49:52 +03:00
}
2014-08-28 06:19:00 +04:00
}