gomod2nix/generate/generate.go

145 lines
2.6 KiB
Go
Raw Normal View History

2020-07-20 11:38:32 +00:00
package fetch
2020-07-20 10:52:58 +00:00
import (
"bytes"
"crypto/sha256"
"encoding/base64"
2020-07-20 10:52:58 +00:00
"encoding/json"
"io"
2020-07-20 11:29:04 +00:00
"io/ioutil"
2020-07-20 10:52:58 +00:00
"os/exec"
"path/filepath"
"sort"
2022-05-27 17:06:48 +00:00
"sync"
"github.com/nix-community/go-nix/pkg/nar"
log "github.com/sirupsen/logrus"
2022-05-27 17:06:48 +00:00
"github.com/tweag/gomod2nix/lib"
2022-05-27 17:25:35 +00:00
schema "github.com/tweag/gomod2nix/schema"
"golang.org/x/mod/modfile"
2020-07-20 10:52:58 +00:00
)
type goModDownload struct {
Path string
Version string
Info string
GoMod string
Zip string
Dir string
Sum string
GoModSum string
2020-07-20 11:38:32 +00:00
}
func GeneratePkgs(directory string, goMod2NixPath string) ([]*schema.Package, error) {
goModPath := filepath.Join(directory, "go.mod")
2020-07-20 10:52:58 +00:00
2020-07-21 09:42:32 +00:00
log.WithFields(log.Fields{
"modPath": goModPath,
}).Info("Parsing go.mod")
2020-07-20 11:29:04 +00:00
// Read go.mod
data, err := ioutil.ReadFile(goModPath)
if err != nil {
return nil, err
}
// Parse go.mod
mod, err := modfile.Parse(goModPath, data, nil)
if err != nil {
return nil, err
}
// Map repos -> replacement repo
replace := make(map[string]string)
for _, repl := range mod.Replace {
replace[repl.New.Path] = repl.Old.Path
2020-07-20 11:29:04 +00:00
}
var modDownloads []*goModDownload
{
2022-05-27 17:25:35 +00:00
log.Info("Downloading dependencies")
cmd := exec.Command(
"go", "mod", "download", "--json",
)
cmd.Dir = directory
stdout, err := cmd.Output()
if err != nil {
return nil, err
2020-07-20 11:29:04 +00:00
}
dec := json.NewDecoder(bytes.NewReader(stdout))
for {
var dl *goModDownload
err := dec.Decode(&dl)
if err == io.EOF {
break
2020-07-20 11:29:04 +00:00
}
modDownloads = append(modDownloads, dl)
2020-07-20 11:29:04 +00:00
}
2022-05-27 17:25:35 +00:00
log.Info("Done downloading dependencies")
}
executor := lib.NewParallellExecutor()
2022-05-27 17:06:48 +00:00
var mux sync.Mutex
cache := schema.ReadCache(goMod2NixPath)
2022-05-27 17:25:35 +00:00
packages := []*schema.Package{}
addPkg := func(pkg *schema.Package) {
mux.Lock()
packages = append(packages, pkg)
mux.Unlock()
}
for _, dl := range modDownloads {
2022-05-27 17:06:48 +00:00
dl := dl
goPackagePath, hasReplace := replace[dl.Path]
if !hasReplace {
goPackagePath = dl.Path
}
2022-05-27 17:06:48 +00:00
cached, ok := cache[goPackagePath]
if ok && cached.Version == dl.Version {
addPkg(cached)
continue
}
2022-05-27 17:06:48 +00:00
executor.Add(func() error {
h := sha256.New()
err := nar.DumpPath(h, dl.Dir)
if err != nil {
2022-05-27 17:06:48 +00:00
return err
}
digest := h.Sum(nil)
2020-07-21 09:42:32 +00:00
2022-05-27 17:25:35 +00:00
pkg := &schema.Package{
2022-05-27 17:06:48 +00:00
GoPackagePath: goPackagePath,
Version: dl.Version,
Hash: "sha256-" + base64.StdEncoding.EncodeToString(digest),
2022-05-27 17:06:48 +00:00
}
if hasReplace {
pkg.ReplacedPath = dl.Path
}
addPkg(pkg)
2022-05-27 17:06:48 +00:00
return nil
})
}
2022-05-27 17:06:48 +00:00
err = executor.Wait()
if err != nil {
return nil, err
}
sort.Slice(packages, func(i, j int) bool {
return packages[i].GoPackagePath < packages[j].GoPackagePath
})
return packages, nil
2020-07-20 10:52:58 +00:00
}