forked from KurokuLabs/margo
/
pkg.go
70 lines (60 loc) · 1.55 KB
/
pkg.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
package gopkg
import (
"path/filepath"
"strings"
)
type Pkg struct {
ImportablePfx string
// The following fields are a subset of build.Package
Dir string
Name string
ImportPath string
Goroot bool
}
type PkgPath struct {
Dir string
ImportPath string
Goroot bool
}
var (
internalSepDir = filepath.FromSlash("/internal/")
vendorSepDir = filepath.FromSlash("/vendor/")
)
func (p *Pkg) IsCommand() bool { return p.Name == "main" }
func (p *Pkg) Importable(srcDir string) bool {
if p.ImportPath == "." || p.IsCommand() {
return false
}
if s := p.ImportablePfx; s != "" {
return strings.HasPrefix(srcDir, s) || srcDir == s[:len(s)-1]
}
if p.Dir == srcDir {
return false
}
return true
}
func (p *Pkg) dirPfx(dir, slash string) string {
if i := strings.LastIndex(dir, slash); i >= 0 {
return filepath.Dir(dir[:i+len(slash)-1]) + string(filepath.Separator)
}
if d := strings.TrimSuffix(dir, slash[:len(slash)-1]); d != dir {
return filepath.Dir(d) + string(filepath.Separator)
}
return ""
}
func (p *Pkg) Finalize() {
p.Dir = filepath.Clean(p.Dir)
// does importing from the 'vendor' and 'internal' dirs work the same?
// who cares... I'm the supreme, I make the rules in this outpost...
p.ImportablePfx = p.dirPfx(p.Dir, internalSepDir)
if p.ImportablePfx == "" {
p.ImportablePfx = p.dirPfx(p.Dir, vendorSepDir)
}
s := p.ImportPath
switch i := strings.LastIndex(s, "/vendor/"); {
case i >= 0:
p.ImportPath = s[i+len("/vendor/"):]
case strings.HasPrefix(s, "vendor/"):
p.ImportPath = s[len("vendor/"):]
}
}