This commit is contained in:
Liang Ding 2014-09-05 12:18:50 +08:00
parent e5af5494f4
commit b5d5658cd5
5 changed files with 101 additions and 7 deletions

View File

@ -9,6 +9,7 @@
"delete": "删除", "delete": "删除",
"save": "保存", "save": "保存",
"format": "格式化", "format": "格式化",
"goget": "go get",
"build_n_run": "构建 & 运行", "build_n_run": "构建 & 运行",
"full_screen": "全屏" "full_screen": "全屏"
} }

View File

@ -76,6 +76,7 @@ func main() {
// 运行相关 // 运行相关
http.HandleFunc("/build", output.BuildHandler) http.HandleFunc("/build", output.BuildHandler)
http.HandleFunc("/run", output.RunHandler) http.HandleFunc("/run", output.RunHandler)
http.HandleFunc("/go/get", output.GoGetHandler)
http.HandleFunc("/output/ws", output.WSHandler) http.HandleFunc("/output/ws", output.WSHandler)
// 文件树 // 文件树

View File

@ -155,7 +155,6 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("go", argv...) cmd := exec.Command("go", argv...)
cmd.Dir = curDir cmd.Dir = curDir
// 设置环境变量(设置当前用户的 GOPATH 等)
setCmdEnv(cmd, username) setCmdEnv(cmd, username)
glog.Infof("go build -o %s %s", executable, filePath) glog.Infof("go build -o %s %s", executable, filePath)
@ -233,6 +232,82 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func GoGetHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
session, _ := user.Session.Get(r, "wide-session")
sid := session.Values["id"].(string)
username := session.Values["username"].(string)
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
data["succ"] = false
return
}
filePath := args["file"].(string)
curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))]
cmd := exec.Command("go", "get")
cmd.Dir = curDir
setCmdEnv(cmd, username)
stdout, err := cmd.StdoutPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
stderr, err := cmd.StderrPipe()
if nil != err {
glog.Error(err)
data["succ"] = false
return
}
reader := io.MultiReader(stdout, stderr)
cmd.Start()
channelRet := map[string]interface{}{}
go func(runningId int) {
glog.Infof("Session [%s] is running [go get] [runningId=%d]", sid, runningId)
for {
buf := make([]byte, 1024)
count, err := reader.Read(buf)
if nil != err || 0 == count {
glog.Infof("Session [%s] 's running [go get] [runningId=%d] has done", sid, runningId)
break
} else {
channelRet["output"] = string(buf[:count])
channelRet["cmd"] = "run"
if nil != outputWS[sid] {
err := outputWS[sid].WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
}
}
}
}
}(rand.Int())
}
func setCmdEnv(cmd *exec.Cmd, username string) { func setCmdEnv(cmd *exec.Cmd, username string) {
userWorkspace := conf.Wide.GetUserWorkspace(username) userWorkspace := conf.Wide.GetUserWorkspace(username)
@ -240,5 +315,9 @@ func setCmdEnv(cmd *exec.Cmd, username string) {
"GOPATH="+userWorkspace, "GOPATH="+userWorkspace,
"GOOS="+runtime.GOOS, "GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH, "GOARCH="+runtime.GOARCH,
"GOROOT="+runtime.GOROOT()) "GOROOT="+runtime.GOROOT(),
"PATH="+os.Getenv("PATH"))
//"TERM="+os.Getenv("COMSPEC"),
//"ComSpec="+os.Getenv("ComSpec")
} }

View File

@ -126,11 +126,23 @@ var wide = {
$('#output').text(''); $('#output').text('');
}, },
success: function(data) { success: function(data) {
executable = data.executable;
if (data.succ) {
} }
});
},
goget: function() {
var request = {
"file": wide.curNode.path
};
$.ajax({
type: 'POST',
url: '/go/get',
data: JSON.stringify(request),
dataType: "json",
beforeSend: function(data) {
$('#output').text('');
},
success: function(data) {
} }
}); });
}, },

View File

@ -23,6 +23,7 @@
<button onclick="wide.run()">{{.i18n.build_n_run}}</button> <button onclick="wide.run()">{{.i18n.build_n_run}}</button>
<button onclick="wide.save()">{{.i18n.save}}</button> <button onclick="wide.save()">{{.i18n.save}}</button>
<button onclick="wide.fmt()">{{.i18n.format}}</button> <button onclick="wide.fmt()">{{.i18n.format}}</button>
<button onclick="wide.goget()">{{.i18n.goget}}</button>
</div> </div>
</div> </div>