Drop support for buildGoPackage format

It was buggy at best and will never really work out because of semantic differences, mainly around nested package handling.

We're better off focusing on a single format which can be well supported.
This commit is contained in:
adisbladis 2022-05-26 19:06:01 +08:00
parent c88d540afd
commit 4ffc0dfd48
3 changed files with 7 additions and 139 deletions

View file

@ -12,7 +12,6 @@ import (
"strings" "strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tweag/gomod2nix/formats/buildgopackage"
"github.com/tweag/gomod2nix/formats/gomod2nix" "github.com/tweag/gomod2nix/formats/gomod2nix"
"github.com/tweag/gomod2nix/types" "github.com/tweag/gomod2nix/types"
"golang.org/x/mod/modfile" "golang.org/x/mod/modfile"
@ -48,7 +47,7 @@ func worker(id int, caches []map[string]*types.Package, jobs <-chan *packageJob,
} }
} }
func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, depsNixPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) { func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, numWorkers int, keepGoing bool) ([]*types.Package, error) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"modPath": goModPath, "modPath": goModPath,
@ -71,10 +70,6 @@ func FetchPackages(goModPath string, goSumPath string, goMod2NixPath string, dep
if len(goModCache) > 0 { if len(goModCache) > 0 {
caches = append(caches, goModCache) caches = append(caches, goModCache)
} }
buildGoCache := buildgopackage.LoadDepsNix(depsNixPath)
if len(buildGoCache) > 0 {
caches = append(caches, buildGoCache)
}
// Map repos -> replacement repo // Map repos -> replacement repo
replace := make(map[string]string) replace := make(map[string]string)

View file

@ -1,99 +0,0 @@
package buildgopackage
import (
"fmt"
"github.com/orivej/go-nix/nix/eval"
"github.com/orivej/go-nix/nix/parser"
"github.com/tweag/gomod2nix/types"
"log"
"os"
"strings"
)
const depNixFormat = ` {
goPackagePath = "%s";
fetch = {
type = "%s";
url = "%s";
rev = "%s";
sha256 = "%s";
};
}`
func Marshal(pkgs []*types.Package) ([]byte, error) {
var result []string
result = append(result, "[")
for _, pkg := range pkgs {
result = append(result,
fmt.Sprintf(depNixFormat,
pkg.GoPackagePath, "git", pkg.URL,
pkg.Rev, pkg.Sha256))
}
result = append(result, "]")
return []byte(strings.Join(result, "\n")), nil
}
// Load the contents of deps.nix into a struct
// This is mean to achieve re-use of previous invocations using the deps.nix (buildGoPackage) output format
func LoadDepsNix(filePath string) map[string]*types.Package {
ret := make(map[string]*types.Package)
if filePath == "" {
return ret
}
stat, err := os.Stat(filePath)
if err != nil {
return ret
}
if stat.Size() == 0 {
return ret
}
p, err := parser.ParseFile(filePath)
if err != nil {
log.Println("Failed reading deps.nix")
return ret
}
evalResult := eval.ParseResult(p)
for _, pkgAttrsExpr := range evalResult.(eval.List) {
pkgAttrs, ok := pkgAttrsExpr.Eval().(eval.Set)
if !ok {
continue
}
fetch, ok := pkgAttrs[eval.Intern("fetch")].Eval().(eval.Set)
if !ok {
continue
}
goPackagePath, ok := pkgAttrs[eval.Intern("goPackagePath")].Eval().(string)
if !ok {
continue
}
url, ok := fetch[eval.Intern("url")].Eval().(string)
if !ok {
continue
}
rev, ok := fetch[eval.Intern("rev")].Eval().(string)
if !ok {
continue
}
sha256, ok := fetch[eval.Intern("sha256")].Eval().(string)
if !ok {
continue
}
ret[goPackagePath] = &types.Package{
GoPackagePath: goPackagePath,
URL: url,
Rev: rev,
Sha256: sha256,
}
}
return ret
}

36
main.go
View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tweag/gomod2nix/fetch" "github.com/tweag/gomod2nix/fetch"
"github.com/tweag/gomod2nix/formats/buildgopackage"
"github.com/tweag/gomod2nix/formats/gomod2nix" "github.com/tweag/gomod2nix/formats/gomod2nix"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
@ -17,7 +16,6 @@ func main() {
var directory = flag.String("dir", "./", "Go project directory") var directory = flag.String("dir", "./", "Go project directory")
var maxJobs = flag.Int("jobs", 10, "Number of max parallel jobs") var maxJobs = flag.Int("jobs", 10, "Number of max parallel jobs")
var outDirFlag = flag.String("outdir", "", "output directory (if different from project directory)") var outDirFlag = flag.String("outdir", "", "output directory (if different from project directory)")
var format = flag.String("format", "gomod2nix", "output format (gomod2nix, buildgopackage)")
flag.Parse() flag.Parse()
outDir := *outDirFlag outDir := *outDirFlag
@ -28,43 +26,17 @@ func main() {
goSumPath := filepath.Join(*directory, "go.sum") goSumPath := filepath.Join(*directory, "go.sum")
goModPath := filepath.Join(*directory, "go.mod") goModPath := filepath.Join(*directory, "go.mod")
wrongFormatError := fmt.Errorf("Format not supported") goMod2NixPath := filepath.Join(outDir, "gomod2nix.toml")
outFile := goMod2NixPath
goMod2NixPath := "" pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, *maxJobs, *keepGoing)
depsNixPath := ""
outFile := ""
switch *format {
case "gomod2nix":
goMod2NixPath = filepath.Join(outDir, "gomod2nix.toml")
outFile = goMod2NixPath
case "buildgopackage":
depsNixPath = filepath.Join(outDir, "deps.nix")
outFile = depsNixPath
default:
panic(wrongFormatError)
}
log.Info(fmt.Sprintf("Using output format '%s'", *format))
pkgs, err := fetch.FetchPackages(goModPath, goSumPath, goMod2NixPath, depsNixPath, *maxJobs, *keepGoing)
if err != nil { if err != nil {
panic(err) panic(err)
} }
var output []byte output, err := gomod2nix.Marshal(pkgs)
switch *format {
case "gomod2nix":
output, err = gomod2nix.Marshal(pkgs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
case "buildgopackage":
output, err = buildgopackage.Marshal(pkgs)
if err != nil {
panic(err)
}
default:
panic(wrongFormatError)
}
err = ioutil.WriteFile(outFile, output, 0644) err = ioutil.WriteFile(outFile, output, 0644)
if err != nil { if err != nil {