Skip to content

Commit

Permalink
Merge pull request #1219 from xushiwei/q
Browse files Browse the repository at this point in the history
gop mod tidy
  • Loading branch information
xushiwei committed May 29, 2022
2 parents 3ef896b + 1cc47dc commit 351ddc1
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 143 deletions.
22 changes: 7 additions & 15 deletions ast/mod/deps.go
Expand Up @@ -29,13 +29,11 @@ import (

// ----------------------------------------------------------------------------

type none struct{}

type Deps struct {
Pkgs map[string]none
HandlePkg func(pkgPath string)
}

func (p *Deps) Load(pkg *ast.Package, withGopStd bool) {
func (p Deps) Load(pkg *ast.Package, withGopStd bool) {
for _, f := range pkg.Files {
p.LoadFile(f, withGopStd)
}
Expand All @@ -44,27 +42,21 @@ func (p *Deps) Load(pkg *ast.Package, withGopStd bool) {
}
}

func (p *Deps) LoadGoFile(f *goast.File) {
if p.Pkgs == nil {
p.Pkgs = make(map[string]none)
}
func (p Deps) LoadGoFile(f *goast.File) {
for _, imp := range f.Imports {
path := imp.Path
if path.Kind == gotoken.STRING {
if s, err := strconv.Unquote(path.Value); err == nil {
if s == "C" {
continue
}
p.Pkgs[s] = none{}
p.HandlePkg(s)
}
}
}
}

func (p *Deps) LoadFile(f *ast.File, withGopStd bool) {
if p.Pkgs == nil {
p.Pkgs = make(map[string]none)
}
func (p Deps) LoadFile(f *ast.File, withGopStd bool) {
for _, imp := range f.Imports {
path := imp.Path
if path.Kind == token.STRING {
Expand All @@ -75,7 +67,7 @@ func (p *Deps) LoadFile(f *ast.File, withGopStd bool) {
}
}

func (p *Deps) gopPkgPath(s string, withGopStd bool) {
func (p Deps) gopPkgPath(s string, withGopStd bool) {
if strings.HasPrefix(s, "gop/") {
if !withGopStd {
return
Expand All @@ -91,7 +83,7 @@ func (p *Deps) gopPkgPath(s string, withGopStd bool) {
}
}
}
p.Pkgs[s] = none{}
p.HandlePkg(s)
}

// ----------------------------------------------------------------------------
27 changes: 0 additions & 27 deletions cl/builtin.go
Expand Up @@ -19,7 +19,6 @@ package cl
import (
"go/token"
"go/types"
"strings"

"github.com/goplus/gox"
)
Expand Down Expand Up @@ -69,29 +68,3 @@ func newBuiltinDefault(pkg gox.PkgImporter, conf *gox.Config) *types.Package {
}

// -----------------------------------------------------------------------------

type gopImporter struct {
gopRoot string
impFrom types.ImporterFrom
}

func newGopImporter(gopRoot string, imp types.Importer) types.Importer {
if impFrom, ok := imp.(types.ImporterFrom); ok && gopRoot != "" {
return &gopImporter{gopRoot: gopRoot, impFrom: impFrom}
}
return imp
}

func (p *gopImporter) Import(pkgPath string) (pkg *types.Package, err error) {
const (
gop = "github.com/goplus/gop"
)
if strings.HasPrefix(pkgPath, gop) {
if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
return p.impFrom.ImportFrom(pkgPath, p.gopRoot, 0)
}
}
return p.impFrom.Import(pkgPath)
}

// -----------------------------------------------------------------------------
6 changes: 0 additions & 6 deletions cl/builtin_test.go
Expand Up @@ -131,12 +131,6 @@ func lookupClass(ext string) (c *gopmod.Class, ok bool) {
return
}

func TestImporter(t *testing.T) {
if newGopImporter("", nil) != nil {
t.Fatal("TestImporter failed")
}
}

func TestGetGoFile(t *testing.T) {
if f := getGoFile("a_test.gop", true); f != testingGoFile {
t.Fatal("TestGetGoFile:", f)
Expand Down
10 changes: 1 addition & 9 deletions cl/compile.go
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/gox/cpackages"
"github.com/goplus/gox/packages"
"github.com/goplus/mod/modfile"
)

Expand Down Expand Up @@ -88,9 +87,6 @@ type Config struct {
// TargetDir is the directory in which to generate Go files.
TargetDir string

// GopRoot specifies the Go+ root directory.
GopRoot string

// C2goBase specifies base of standard c2go packages.
// Default is github.com/goplus/.
C2goBase string
Expand Down Expand Up @@ -385,10 +381,6 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
targetDir = workingDir
}
fset := conf.Fset
imp := conf.Importer
if imp == nil {
imp = packages.NewImporter(fset, workingDir)
}
files := pkg.Files
interp := &nodeInterp{
fset: fset, files: files, workingDir: workingDir,
Expand All @@ -398,7 +390,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
}
confGox := &gox.Config{
Fset: fset,
Importer: newGopImporter(conf.GopRoot, imp),
Importer: conf.Importer,
LoadNamed: ctx.loadNamed,
HandleErr: ctx.handleErr,
NodeInterpreter: interp,
Expand Down
1 change: 0 additions & 1 deletion cl/compile_test.go
Expand Up @@ -52,7 +52,6 @@ func init() {
LookupClass: lookupClass,
LookupPub: lookupPub,
C2goBase: "github.com/goplus/gop/cl/internal",
GopRoot: gopRootDir,
NoFileLine: true,
NoAutoGenMain: true,
}
Expand Down
36 changes: 22 additions & 14 deletions cmd/internal/gopget/get.go
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
)
Expand Down Expand Up @@ -60,29 +61,36 @@ func runCmd(cmd *base.Command, args []string) {
}

func get(pkgPath string) {
modBase := ""
mod, err := modload.Load(".", 0)
hasMod := (err != syscall.ENOENT)
if hasMod {
check(err)
check(mod.UpdateGoMod(gopenv.Get(), true))
modBase = mod.Path()
}
modPath, _ := splitPkgPath(pkgPath)
modVer, isClass, err := modfetch.Get(gopenv.Get(), modPath)

pkgModVer, _, err := modfetch.GetPkg(pkgPath, modBase)
check(err)
if hasMod {
if isClass {
mod.AddRegister(modVer.Path)
fmt.Fprintf(os.Stderr, "gop get: registered %s\n", modVer.Path)
}
check(mod.AddRequire(modVer.Path, modVer.Version))
fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", modVer.Path, modVer.Version)
check(mod.Save())
check(mod.UpdateGoMod(gopenv.Get(), false))
if !hasMod {
return
}
}

func splitPkgPath(pkgPath string) (modPathWithVer string, pkgPathNoVer string) {
return pkgPath, pkgPath
pkgModRoot, err := modcache.Path(pkgModVer)
check(err)

pkgMod, err := modload.Load(pkgModRoot, 0)
check(err)
if pkgMod.Classfile != nil {
mod.AddRegister(pkgModVer.Path)
fmt.Fprintf(os.Stderr, "gop get: registered %s\n", pkgModVer.Path)
}

check(mod.AddRequire(pkgModVer.Path, pkgModVer.Version))
fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", pkgModVer.Path, pkgModVer.Version)

check(mod.Save())
check(mod.UpdateGoMod(gopenv.Get(), false))
}

func check(err error) {
Expand Down
17 changes: 4 additions & 13 deletions cmd/internal/mod/init.go
Expand Up @@ -17,8 +17,6 @@
package mod

import (
"fmt"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -49,15 +47,13 @@ Run 'gop help mod init' for more information.`)
default:
fatal("gop mod init: too many arguments")
}

modPath := args[0]
mod, err := modload.Create(".", modPath, goMainVer(), env.MainVersion)
if err != nil {
fatal(err)
}
check(err)

err = mod.Save()
if err != nil {
fatal(err)
}
check(err)
}

func goMainVer() string {
Expand All @@ -70,8 +66,3 @@ func goMainVer() string {
}
return ver
}

func fatal(msg interface{}) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
15 changes: 15 additions & 0 deletions cmd/internal/mod/mod.go
Expand Up @@ -16,6 +16,10 @@
package mod

import (
"fmt"
"log"
"os"

"github.com/goplus/gop/cmd/internal/base"
)

Expand All @@ -29,3 +33,14 @@ var Cmd = &base.Command{
cmdTidy,
},
}

func check(err error) {
if err != nil {
log.Panicln(err)
}
}

func fatal(msg interface{}) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
58 changes: 52 additions & 6 deletions cmd/internal/mod/tidy.go
Expand Up @@ -17,11 +17,18 @@
package mod

import (
"fmt"
"log"
"os"
"os/exec"
"syscall"

"github.com/goplus/gop"
"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modload"
"github.com/goplus/mod"
"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modfetch"
)

// gop mod tidy
Expand All @@ -35,13 +42,52 @@ func init() {
}

func runTidy(cmd *base.Command, args []string) {
mod, err := modload.Load(".", 0)
mod, err := gopmod.Load(".", mod.GopModOnly)
if err != nil {
if err == syscall.ENOENT {
fmt.Fprintln(os.Stderr, "gop.mod not found")
os.Exit(1)
}
log.Panicln(err)
}

depMods, err := gop.GenDepMods(mod, mod.Root(), true)
check(err)
check(mod.Tidy(gopenv.Get()))

tidy(mod, depMods)
}

func check(err error) {
if err != nil {
log.Fatalln(err)
func tidy(mod *gopmod.Module, depMods map[string]struct{}) {
old := mod.DepMods()
for modPath := range old {
if _, ok := depMods[modPath]; !ok { // removed
mod.DropRequire(modPath)
}
}
for modPath := range depMods {
if _, ok := old[modPath]; !ok { // added
if newMod, err := modfetch.Get(modPath); err != nil {
log.Fatalln(err)
} else {
mod.AddRequire(newMod.Path, newMod.Version)
}
}
}

err := mod.Save()
check(err)

_, _, err = gop.GenGo(mod.Root()+"/...", &gop.Config{DontUpdateGoMod: true})
check(err)

err = mod.UpdateGoMod(gopenv.Get(), true)
check(err)

cmd := exec.Command("go", "mod", "tidy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Dir = mod.Root()
err = cmd.Run()
check(err)
}

0 comments on commit 351ddc1

Please sign in to comment.