-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package packages | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// pkgPath Caches | ||
type Cache struct { | ||
dirCache map[string]bool | ||
dirCacheMutex sync.RWMutex | ||
packageCacheMap map[string]CacheInfo | ||
packageCacheMutex sync.RWMutex | ||
waitCache sync.WaitGroup | ||
} | ||
|
||
type CacheInfo struct { | ||
ImportPath string | ||
PkgDir string | ||
PkgExport string | ||
} | ||
|
||
// https://github.com/goplus/gop/issues/1710 | ||
// Not fully optimized | ||
// Retrieve all imports in the specified directory and cache them | ||
func (c *Cache) goListExportCache(dir string, pkgs ...string) { | ||
c.dirCacheMutex.Lock() | ||
if c.dirCache[dir] { | ||
c.dirCacheMutex.Unlock() | ||
return | ||
} | ||
c.dirCache[dir] = true | ||
c.dirCacheMutex.Unlock() | ||
var stdout, stderr bytes.Buffer | ||
commandStr := []string{"list", "-f", "{{.ImportPath}},{{.Dir}},{{.Export}}", "-export", "-e"} | ||
commandStr = append(commandStr, pkgs...) | ||
commandStr = append(commandStr, "all") | ||
cmd := exec.Command("go", commandStr...) | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
cmd.Dir = dir | ||
err := cmd.Run() | ||
if err == nil { | ||
ret := stdout.String() | ||
for _, v := range strings.Split(ret, "\n") { | ||
s := strings.Split(v, ",") | ||
if len(s) != 3 { | ||
continue | ||
} | ||
c.packageCacheMutex.Lock() | ||
c.packageCacheMap[s[0]] = CacheInfo{s[0], s[1], s[2]} | ||
c.packageCacheMutex.Unlock() | ||
} | ||
} | ||
} | ||
|
||
// Reduce wait time when performing `go list` on multiple pkgDir at the same time | ||
func (c *Cache) GoListExportCacheSync(dir string, pkgs ...string) { | ||
c.waitCache.Add(1) | ||
go func() { | ||
defer c.waitCache.Done() | ||
c.goListExportCache(dir, pkgs...) | ||
}() | ||
} | ||
|
||
func (c *Cache) GetPkgCache(pkgPath string) (ret CacheInfo, ok bool) { | ||
c.waitCache.Wait() | ||
c.packageCacheMutex.RLock() | ||
ret, ok = c.packageCacheMap[pkgPath] | ||
c.packageCacheMutex.RUnlock() | ||
return | ||
} | ||
|
||
func (c *Cache) DelPkgCache(pkgPath string) bool { | ||
_, ok := c.GetPkgCache(pkgPath) | ||
if ok { | ||
c.packageCacheMutex.Lock() | ||
delete(c.packageCacheMap, pkgPath) | ||
c.packageCacheMutex.Unlock() | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func NewGoListCache(dir string) *Cache { | ||
c := &Cache{ | ||
dirCache: make(map[string]bool), | ||
packageCacheMap: make(map[string]CacheInfo), | ||
} | ||
// get the dir pkg cache | ||
c.GoListExportCacheSync(dir) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package packages | ||
|
||
import "testing" | ||
|
||
func TestCacheDelCache(t *testing.T) { | ||
p := NewImporter(nil) | ||
if !p.GetPkgCache().DelPkgCache("fmt") { | ||
t.Fatal("del cache should pass!") | ||
} | ||
if p.GetPkgCache().DelPkgCache("fmt") { | ||
t.Fatal("del cache should fail") | ||
} | ||
} | ||
|
||
func TestRepeatLoadDir(t *testing.T) { | ||
p := NewImporter(nil) | ||
p.GetPkgCache().goListExportCache("", "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters