diff --git a/conf/wide.go b/conf/wide.go index 9c735b5..ce361a8 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -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") - if isExist(ret) { - return ret + PathSeparator + 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 + } + + // $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable + ret = gopath + PathSeparator + "bin" + PathSeparator + + runtime.GOOS + "_" + runtime.GOARCH + PathSeparator + executable + if isExist(ret) { + return ret + } + + // $GOPATH/bin/executable + ret = gopath + PathSeparator + "bin" + PathSeparator + executable + if isExist(ret) { + return ret + } } - // $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/ - ret = os.Getenv("GOPATH") + PathSeparator + "bin" + PathSeparator + - runtime.GOOS + "_" + runtime.GOARCH - if isExist(ret) { - return ret + PathSeparator - } - - // $GOPATH/bin/ - return os.Getenv("GOPATH") + PathSeparator + "bin" + PathSeparator + // $GOBIN/executable + return os.Getenv("GOBIN") + PathSeparator + executable } // 保存 Wide 配置. diff --git a/editor/editors.go b/editor/editors.go index c22be21..85514b0 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -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 diff --git a/output/outputs.go b/output/outputs.go index 8017307..c1038ed 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -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 diff --git a/util/os.go b/util/os.go new file mode 100644 index 0000000..7350aeb --- /dev/null +++ b/util/os.go @@ -0,0 +1,15 @@ +package util + +import ( + "runtime" +) + +type myos struct{} + +// 操作系统工具. +var OS = myos{} + +// 判断是否是 Windows 操作系统. +func (*myos) IsWindows() bool { + return "windows" == runtime.GOOS +}