This commit is contained in:
Liang Ding 2014-10-26 15:41:34 +08:00
parent 3b8b8ced46
commit 107b499f1a
4 changed files with 53 additions and 38 deletions

View File

@ -83,7 +83,7 @@ func FixedTimeCheckEnv() {
os.Exit(-1)
}
gocode := Wide.GetGocode()
gocode := Wide.GetExecutableInGOBIN("gocode")
cmd := exec.Command(gocode, "close")
_, err := cmd.Output()
if nil != err {
@ -91,7 +91,7 @@ func FixedTimeCheckEnv() {
glog.Warningf("Not found gocode [%s]", gocode)
}
ide_stub := Wide.GetIDEStub()
ide_stub := Wide.GetExecutableInGOBIN("ide_stub")
cmd = exec.Command(ide_stub, "version")
_, err = cmd.Output()
if nil != err {
@ -149,40 +149,40 @@ func (*conf) GetUser(username string) *User {
return nil
}
// 获取 gocode 路径.
func (*conf) GetGocode() string {
return getGOBIN() + "gocode"
}
// 获取 ide_stub 路径.
func (*conf) GetIDEStub() string {
return getGOBIN() + "ide_stub"
}
// 获取 GOBIN 路径,末尾带路径分隔符.
func getGOBIN() string {
// $GOBIN/
ret := os.Getenv("GOBIN")
if "" != ret {
return ret + PathSeparator
// 获取 GOBIN 中 executable 指定的文件路径.
//
// 函数内部会判断操作系统,如果是 Windows 则在 executable 实参后加入 .exe 后缀.
func (*conf) GetExecutableInGOBIN(executable string) string {
if util.OS.IsWindows() {
executable += ".exe"
}
// $GOPATH/bin/$GOOS_$GOARCH/
ret = os.Getenv("GOPATH") + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH")
gopaths := strings.Split(os.Getenv("GOPATH"), PathListSeparator)
for _, gopath := range gopaths {
// $GOPATH/bin/$GOOS_$GOARCH/executable
ret := gopath + PathSeparator + "bin" + PathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + PathSeparator + executable
if isExist(ret) {
return ret + PathSeparator
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/
ret = os.Getenv("GOPATH") + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + PathSeparator + "bin" + PathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable
if isExist(ret) {
return ret + PathSeparator
return ret
}
// $GOPATH/bin/
return os.Getenv("GOPATH") + PathSeparator + "bin" + PathSeparator
// $GOPATH/bin/executable
ret = gopath + PathSeparator + "bin" + PathSeparator + executable
if isExist(ret) {
return ret
}
}
// $GOBIN/executable
return os.Getenv("GOBIN") + PathSeparator + executable
}
// 保存 Wide 配置.

View File

@ -56,7 +56,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset: %d", offset)
gocode := conf.Wide.GetGocode()
gocode := conf.Wide.GetExecutableInGOBIN("gocode")
argv := []string{"-f=json", "autocomplete", strconv.Itoa(offset)}
var output bytes.Buffer
@ -135,7 +135,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
glog.V(5).Infof("gocode set lib-path %s", libPath)
// FIXME: 使用 gocode set lib-path 在多工作空间环境下肯定是有问题的,需要考虑其他实现方式
gocode := conf.Wide.GetGocode()
gocode := conf.Wide.GetExecutableInGOBIN("gocode")
argv := []string{"set", "lib-path", libPath}
exec.Command(gocode, argv...).Run()
@ -207,7 +207,7 @@ func GetExprInfoHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset [%d]", offset)
// TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现
ide_stub := conf.Wide.GetIDEStub()
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-info", "."}
cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir
@ -281,7 +281,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset [%d]", offset)
// TODO: 目前是调用 liteide_stub 工具来查找声明,后续需要重新实现
ide_stub := conf.Wide.GetIDEStub()
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-def", "."}
cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir
@ -363,7 +363,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
// glog.Infof("offset [%d]", offset)
// TODO: 目前是调用 liteide_stub 工具来查找使用,后续需要重新实现
ide_stub := conf.Wide.GetIDEStub()
ide_stub := conf.Wide.GetExecutableInGOBIN("ide_stub")
argv := []string{"type", "-cursor", filename + ":" + strconv.Itoa(offset), "-use", "."}
cmd := exec.Command(ide_stub, argv...)
cmd.Dir = curDir

View File

@ -225,7 +225,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
}
suffix := ""
if "windows" == runtime.GOOS {
if util.OS.IsWindows() {
suffix = ".exe"
}
executable := "main" + suffix

15
util/os.go Normal file
View File

@ -0,0 +1,15 @@
package util
import (
"runtime"
)
type myos struct{}
// 操作系统工具.
var OS = myos{}
// 判断是否是 Windows 操作系统.
func (*myos) IsWindows() bool {
return "windows" == runtime.GOOS
}