forked from b3log/wide
/
go.go
133 lines (108 loc) · 3.2 KB
/
go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2014-2017, b3log.org & hacpai.com
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
package util
import (
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
)
const (
pathSeparator = string(os.PathSeparator) // OS-specific path separator
pathListSeparator = string(os.PathListSeparator) // OS-specific path list separator
)
type mygo struct{}
// Go utilities.
var Go = mygo{}
func (*mygo) GetCrossPlatforms() []string {
ret := []string{}
toolDir := runtime.GOROOT() + "/pkg/tool"
f, _ := os.Open(toolDir)
names, _ := f.Readdirnames(-1)
f.Close()
for _, name := range names {
subDir, _ := os.Open(toolDir + "/" + name)
tools, _ := subDir.Readdirnames(10)
subDir.Close()
if len(tools) > 5 {
ret = append(ret, name)
}
}
return ret
}
// 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
}
return filepath.FromSlash(path.Clean(ret))
}
// IsAPI determines whether the specified path belongs to Go API.
func (*mygo) IsAPI(path string) bool {
apiPath := Go.GetAPIPath()
return strings.HasPrefix(filepath.FromSlash(path), apiPath)
}
// GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].
func (*mygo) GetGoFormats() []string {
ret := []string{"gofmt"}
p := Go.GetExecutableInGOBIN("goimports")
if File.IsExist(p) {
ret = append(ret, "goimports")
}
sort.Strings(ret)
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
ret := gopath + pathSeparator + "bin" + pathSeparator +
os.Getenv("GOOS") + "_" + os.Getenv("GOARCH") + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/{runtime.GOOS}_{runtime.GOARCH}/executable
ret = gopath + pathSeparator + "bin" + pathSeparator +
runtime.GOOS + "_" + runtime.GOARCH + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
// $GOPATH/bin/executable
ret = gopath + pathSeparator + "bin" + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
}
// $GOBIN/executable
gobin := os.Getenv("GOBIN")
if "" != gobin {
ret := gobin + pathSeparator + executable
if File.IsExist(ret) {
return ret
}
}
return "./" + executable
}