Skip to content

Commit 279ba60

Browse files
committed
Refactor package path extraction
In preparation for pulling all package information at once.
1 parent d285700 commit 279ba60

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

go/extractor/extractor.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
103103
extractUniverseScope()
104104
log.Println("Done extracting universe scope.")
105105

106-
// a map of package path to package root directory (currently the module root or the source directory)
107-
pkgRoots := make(map[string]string)
108-
// a map of package path to source code directory
109-
pkgDirs := make(map[string]string)
106+
// a map of package path to source directory and module root directory
107+
pkgInfos := make(map[string]util.PkgInfo)
110108
// root directories of packages that we want to extract
111109
wantedRoots := make(map[string]bool)
112110

@@ -116,16 +114,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
116114
}, func(pkg *packages.Package) {
117115
log.Printf("Processing package %s.", pkg.PkgPath)
118116

119-
if _, ok := pkgRoots[pkg.PkgPath]; !ok {
120-
mdir := util.GetModDir(pkg.PkgPath, modFlags...)
121-
pdir := util.GetPkgDir(pkg.PkgPath, modFlags...)
122-
// GetModDir returns the empty string if the module directory cannot be determined, e.g. if the package
123-
// is not using modules. If this is the case, fall back to the package directory
124-
if mdir == "" {
125-
mdir = pdir
126-
}
127-
pkgRoots[pkg.PkgPath] = mdir
128-
pkgDirs[pkg.PkgPath] = pdir
117+
if _, ok := pkgInfos[pkg.PkgPath]; !ok {
118+
pkgInfos[pkg.PkgPath] = util.GetPkgInfo(pkg.PkgPath, modFlags...)
129119
}
130120

131121
log.Printf("Extracting types for package %s.", pkg.PkgPath)
@@ -152,11 +142,14 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
152142
})
153143

154144
for _, pkg := range pkgs {
155-
if pkgRoots[pkg.PkgPath] == "" {
145+
pkgInfo, ok := pkgInfos[pkg.PkgPath]
146+
if !ok || pkgInfo.PkgDir == "" {
156147
log.Fatalf("Unable to get a source directory for input package %s.", pkg.PkgPath)
157148
}
158-
wantedRoots[pkgRoots[pkg.PkgPath]] = true
159-
wantedRoots[pkgDirs[pkg.PkgPath]] = true
149+
wantedRoots[pkgInfo.PkgDir] = true
150+
if pkgInfo.ModDir != "" {
151+
wantedRoots[pkgInfo.ModDir] = true
152+
}
160153
}
161154

162155
log.Println("Done processing dependencies.")
@@ -174,16 +167,17 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
174167
return true
175168
}, func(pkg *packages.Package) {
176169
for root, _ := range wantedRoots {
177-
relDir, err := filepath.Rel(root, pkgDirs[pkg.PkgPath])
170+
pkgInfo := pkgInfos[pkg.PkgPath]
171+
relDir, err := filepath.Rel(root, pkgInfo.PkgDir)
178172
if err != nil || noExtractRe.MatchString(relDir) {
179173
// if the path can't be made relative or matches the noExtract regexp skip it
180174
continue
181175
}
182176

183177
extraction.extractPackage(pkg)
184178

185-
if pkgRoots[pkg.PkgPath] != "" {
186-
modPath := filepath.Join(pkgRoots[pkg.PkgPath], "go.mod")
179+
if pkgInfo.ModDir != "" {
180+
modPath := filepath.Join(pkgInfo.ModDir, "go.mod")
187181
if util.FileExists(modPath) {
188182
log.Printf("Extracting %s", modPath)
189183
start := time.Now()

go/extractor/util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ func runGoListWithEnv(format string, pkgpath string, additionalEnv []string, fla
5454
return strings.TrimSpace(string(out)), nil
5555
}
5656

57+
// PkgInfo holds package directory and module directory (if any) for a package
58+
type PkgInfo struct {
59+
PkgDir string // the directory directly containing source code of this package
60+
ModDir string // the module directory containing this package, empty if not a module
61+
}
62+
63+
// GetPkgInfo fills the package info structure for the specified package path.
64+
// It passes the `go list` the flags specified by `flags`.
65+
func GetPkgInfo(pkgpath string, flags ...string) PkgInfo {
66+
return PkgInfo{
67+
PkgDir: GetModDir(pkgpath, flags...),
68+
ModDir: GetPkgDir(pkgpath, flags...),
69+
}
70+
}
71+
5772
// GetModDir gets the absolute directory of the module containing the package with path
5873
// `pkgpath`. It passes the `go list` the flags specified by `flags`.
5974
func GetModDir(pkgpath string, flags ...string) string {

0 commit comments

Comments
 (0)