wide/util/go.go

105 lines
2.7 KiB
Go
Raw Normal View History

2014-11-24 12:24:35 +03:00
// Copyright (c) 2014, B3log
2014-12-07 06:42:34 +03:00
//
2014-11-24 12:24:35 +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-12-07 06:42:34 +03:00
//
2014-11-24 12:24:35 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-12-07 06:42:34 +03:00
//
2014-11-24 12:24:35 +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-11-21 12:41:51 +03:00
package util
import (
2014-12-07 06:42:34 +03:00
"os"
2014-11-21 12:41:51 +03:00
"path"
2014-12-07 06:42:34 +03:00
"path/filepath"
2014-11-21 12:41:51 +03:00
"runtime"
2014-12-08 12:43:25 +03:00
"sort"
2014-11-21 12:41:51 +03:00
"strings"
2014-11-26 06:54:01 +03:00
)
const (
2014-12-07 06:42:34 +03:00
pathSeparator = string(os.PathSeparator) // OS-specific path separator
pathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
2014-11-21 12:41:51 +03:00
)
type mygo struct{}
// Go utilities.
var Go = mygo{}
// GetAPIPath gets the Go source code path.
//
// 1. before Go 1.4: $GOROOT/src/pkg
// 2. Go 1.4 and after: $GOROOT/src
func (*mygo) GetAPIPath() string {
ret := runtime.GOROOT() + "/src/pkg" // before Go 1.4
if !File.IsExist(ret) {
ret = runtime.GOROOT() + "/src" // Go 1.4 and after
}
2014-11-24 12:20:43 +03:00
return filepath.FromSlash(path.Clean(ret))
2014-11-21 12:41:51 +03:00
}
// IsAPI determines whether the specified path belongs to Go API.
func (*mygo) IsAPI(path string) bool {
apiPath := Go.GetAPIPath()
return strings.HasPrefix(path, apiPath)
}
2014-11-26 06:54:01 +03:00
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
func (*mygo) GetGoFormats() []string {
2014-12-07 06:42:34 +03:00
ret := []string{"gofmt"}
2014-11-26 06:54:01 +03:00
p := Go.GetExecutableInGOBIN("goimports")
if File.IsExist(p) {
ret = append(ret, "goimports")
}
2014-12-07 06:42:34 +03:00
2014-12-08 12:43:25 +03:00
sort.Strings(ret)
2014-11-26 06:54:01 +03:00
return ret
}
// GetExecutableInGOBIN gets executable file under GOBIN path.
//
// The specified executable should not with extension, this function will append .exe if on Windows.
func (*mygo) GetExecutableInGOBIN(executable string) string {
if OS.IsWindows() {
executable += ".exe"
}
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
for _, gopath := range gopaths {
// $GOPATH/bin/$GOOS_$GOARCH/executable
2014-12-07 06:42:34 +03:00
ret := gopath + pathSeparator + "bin" + pathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + pathSeparator + executable
2014-11-26 06:54:01 +03:00
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
2014-12-07 06:42:34 +03:00
ret = gopath + pathSeparator + "bin" + pathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + pathSeparator + executable
2014-11-26 06:54:01 +03:00
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/executable
2014-12-07 06:42:34 +03:00
ret = gopath + pathSeparator + "bin" + pathSeparator + executable
2014-11-26 06:54:01 +03:00
if File.IsExist(ret) {
return ret
}
}
// $GOBIN/executable
2014-12-07 06:42:34 +03:00
return os.Getenv("GOBIN") + pathSeparator + executable
2014-11-26 06:54:01 +03:00
}