workspace

This commit is contained in:
Liang Ding 2014-09-03 00:26:10 +08:00
parent 77afacc3b1
commit ad49df807c
2 changed files with 43 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package editor
import (
"bytes"
"encoding/json"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
@ -137,6 +138,9 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
return
}
//session, _ := user.Session.Get(r, "wide-session")
//username := session.Values["username"].(string)
code := args["code"].(string)
line := int(args["cursorLine"].(float64))
ch := int(args["cursorCh"].(float64))
@ -150,6 +154,10 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
var output bytes.Buffer
cmd := exec.Command("gocode", argv...)
// 设置环境变量(设置当前用户的 GOPATH 等)
// FIXME: setCmdEnv(cmd, username)
cmd.Stdout = &output
stdin, _ := cmd.StdinPipe()
@ -173,3 +181,22 @@ func getCursorOffset(code string, line, ch int) (offset int) {
return
}
func setCmdEnv(cmd *exec.Cmd, username string) {
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)
userWorkspace := userRepos[:strings.LastIndex(userRepos, "/src")]
glog.Infof("User [%s] workspace [%s]", username, userWorkspace)
masterWorkspace := conf.Wide.Repos[:strings.LastIndex(conf.Wide.Repos, "/src")]
glog.Infof("Master workspace [%s]", masterWorkspace)
GOPATH := os.Getenv("GOPATH")
glog.Infof("Env GOPATH [%s]", GOPATH)
cmd.Env = append(cmd.Env,
"GOPATH="+userWorkspace+string(os.PathListSeparator)+
masterWorkspace+string(os.PathListSeparator)+
GOPATH,
"GOROOT="+os.Getenv("GOROOT"))
}

View File

@ -109,6 +109,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
session, _ := user.Session.Get(r, "wide-session")
sid := session.Values["id"].(string)
username := session.Values["username"].(string)
decoder := json.NewDecoder(r.Body)
@ -155,7 +156,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
cmd.Dir = curDir
// 设置环境变量(设置当前用户的 GOPATH 等)
setCmdEnv(cmd)
setCmdEnv(cmd, username)
glog.Infof("go build -o %s %s", executable, filePath)
@ -218,7 +219,18 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
}
}
func setCmdEnv(cmd *exec.Cmd) {
// TODO: 使用用户自己的仓库路径设置 GOPATH
cmd.Env = append(cmd.Env, "GOPATH="+conf.Wide.Repos, "GOROOT="+os.Getenv("GOROOT"))
func setCmdEnv(cmd *exec.Cmd, username string) {
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)
userWorkspace := userRepos[:strings.LastIndex(userRepos, "/src")]
// glog.Infof("User [%s] workspace [%s]", username, userWorkspace)
masterWorkspace := conf.Wide.Repos[:strings.LastIndex(conf.Wide.Repos, "/src")]
// glog.Infof("Master workspace [%s]", masterWorkspace)
cmd.Env = append(cmd.Env,
"GOPATH="+userWorkspace+string(os.PathListSeparator)+
masterWorkspace+string(os.PathListSeparator)+
os.Getenv("GOPATH"),
"GOROOT="+os.Getenv("GOROOT"))
}