File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 17
17
package gopmod
18
18
19
19
import (
20
+ "log"
20
21
"path/filepath"
22
+ "runtime"
21
23
"sort"
22
24
"strings"
23
25
"syscall"
@@ -84,6 +86,40 @@ func isPkgInMod(pkgPath, modPath string) bool {
84
86
return false
85
87
}
86
88
89
+ type Package struct {
90
+ Type PkgType
91
+ Dir string
92
+ ModDir string
93
+ ModPath string
94
+ }
95
+
96
+ func (p * Module ) Lookup (pkgPath string ) (pkg * Package , err error ) {
97
+ switch pt := p .PkgType (pkgPath ); pt {
98
+ case PkgtStandard :
99
+ modDir := runtime .GOROOT ()
100
+ pkg = & Package {Type : PkgtStandard , ModDir : modDir , Dir : filepath .Join (modDir , pkgPath )}
101
+ case PkgtModule :
102
+ modPath := p .Path ()
103
+ modDir := p .Root ()
104
+ dir := modDir + pkgPath [:len (modPath )]
105
+ pkg = & Package {Type : PkgtModule , ModPath : modPath , ModDir : modDir , Dir : dir }
106
+ case PkgtExtern :
107
+ if modPath , modVer , ok := p .LookupExternPkg (pkgPath ); ok {
108
+ if modDir , e := modcache .Path (modVer ); e == nil {
109
+ dir := modDir + pkgPath [:len (modPath )]
110
+ pkg = & Package {Type : PkgtExtern , ModPath : modPath , ModDir : modDir , Dir : dir }
111
+ } else {
112
+ return nil , e
113
+ }
114
+ } else {
115
+ err = syscall .ENOENT
116
+ }
117
+ default :
118
+ log .Panicln ("Module.Lookup:" , pkgPath , "unsupported pkgType:" , pt )
119
+ }
120
+ return
121
+ }
122
+
87
123
// LookupExternPkg lookups a external package from depended modules.
88
124
// If modVer.Path is replace to be a local path, it will be canonical to an absolute path.
89
125
func (p * Module ) LookupExternPkg (pkgPath string ) (modPath string , modVer module.Version , ok bool ) {
You can’t perform that action at this time.
0 commit comments