Add back internal caching for previously generated packages

This commit is contained in:
adisbladis 2022-05-28 01:53:43 +08:00
parent de2aeaeb0b
commit 5125e0aa97
2 changed files with 56 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
"path" "path"
"sort"
"strings" "strings"
"sync" "sync"
@ -79,17 +80,30 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc
executor := lib.NewParallellExecutor(numWorkers) executor := lib.NewParallellExecutor(numWorkers)
var mux sync.Mutex var mux sync.Mutex
cache := schema.ReadCache(goMod2NixPath)
packages := []*schema.Package{} packages := []*schema.Package{}
addPkg := func(pkg *schema.Package) {
mux.Lock()
packages = append(packages, pkg)
mux.Unlock()
}
for _, dl := range modDownloads { for _, dl := range modDownloads {
dl := dl dl := dl
goPackagePath, hasReplace := replace[dl.Path]
if !hasReplace {
goPackagePath = dl.Path
}
cached, ok := cache[goPackagePath]
if ok && cached.Version == dl.Version {
addPkg(cached)
continue
}
executor.Add(func() error { executor.Add(func() error {
goPackagePath, hasReplace := replace[dl.Path]
if !hasReplace {
goPackagePath = dl.Path
}
var storePath string var storePath string
{ {
stdout, err := exec.Command( stdout, err := exec.Command(
@ -119,9 +133,7 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc
pkg.ReplacedPath = dl.Path pkg.ReplacedPath = dl.Path
} }
mux.Lock() addPkg(pkg)
packages = append(packages, pkg)
mux.Unlock()
return nil return nil
}) })
@ -132,6 +144,10 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc
return nil, err return nil, err
} }
sort.Slice(packages, func(i, j int) bool {
return packages[i].GoPackagePath < packages[j].GoPackagePath
})
return packages, nil return packages, nil
} }

View file

@ -3,6 +3,7 @@ package types
import ( import (
"bytes" "bytes"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"os"
) )
const SchemaVersion = 1 const SchemaVersion = 1
@ -38,3 +39,33 @@ func Marshal(pkgs []*Package) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func ReadCache(filePath string) map[string]*Package {
ret := make(map[string]*Package)
if filePath == "" {
return ret
}
b, err := os.ReadFile(filePath)
if err != nil {
return ret
}
var output Output
_, err = toml.Decode(string(b), &output)
if err != nil {
return ret
}
if output.SchemaVersion != SchemaVersion {
return ret
}
for k, v := range output.Mod {
v.GoPackagePath = k
ret[k] = v
}
return ret
}