From 5125e0aa978124f7074d86358b6888f1cc788c32 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sat, 28 May 2022 01:53:43 +0800 Subject: [PATCH] Add back internal caching for previously generated packages --- generate/generate.go | 34 +++++++++++++++++++++++++--------- schema/schema.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/generate/generate.go b/generate/generate.go index 3a3e713..df3a913 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os/exec" "path" + "sort" "strings" "sync" @@ -79,17 +80,30 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc executor := lib.NewParallellExecutor(numWorkers) var mux sync.Mutex + cache := schema.ReadCache(goMod2NixPath) + packages := []*schema.Package{} + addPkg := func(pkg *schema.Package) { + mux.Lock() + packages = append(packages, pkg) + mux.Unlock() + } + for _, dl := range modDownloads { 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 { - - goPackagePath, hasReplace := replace[dl.Path] - if !hasReplace { - goPackagePath = dl.Path - } - var storePath string { stdout, err := exec.Command( @@ -119,9 +133,7 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc pkg.ReplacedPath = dl.Path } - mux.Lock() - packages = append(packages, pkg) - mux.Unlock() + addPkg(pkg) return nil }) @@ -132,6 +144,10 @@ func GeneratePkgs(goModPath string, goMod2NixPath string, numWorkers int) ([]*sc return nil, err } + sort.Slice(packages, func(i, j int) bool { + return packages[i].GoPackagePath < packages[j].GoPackagePath + }) + return packages, nil } diff --git a/schema/schema.go b/schema/schema.go index 831a2ee..652a7f4 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -3,6 +3,7 @@ package types import ( "bytes" "github.com/BurntSushi/toml" + "os" ) const SchemaVersion = 1 @@ -38,3 +39,33 @@ func Marshal(pkgs []*Package) ([]byte, error) { 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 +}