Skip to content

Commit

Permalink
Merge pull request #1721 from xushiwei/q
Browse files Browse the repository at this point in the history
saveWithGopMod
  • Loading branch information
xushiwei committed Feb 11, 2024
2 parents 5de05db + f76a5cb commit a461aff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.21
github.com/goplus/gox v1.14.9
github.com/goplus/mod v0.13.0
github.com/goplus/gox v1.14.9-p1
github.com/goplus/mod v0.13.1
github.com/qiniu/x v1.13.3
golang.org/x/tools v0.17.0
)
Expand All @@ -15,7 +15,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
)

Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/goplus/c2go v0.7.21 h1:bRWda6KtQdY0ph+tnPEOGzZMq3OUkAI9cJBzrr5N6AM=
github.com/goplus/c2go v0.7.21/go.mod h1:45zjbWGW8NDuGfTOrFchCIpH9TbiH7TPg8/zF6EdEyk=
github.com/goplus/gox v1.14.2/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/gox v1.14.9 h1:BXbO79ac0t5jPZ1NVAShPwl+UKVkKyM0cue2Y8idFyY=
github.com/goplus/gox v1.14.9/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/mod v0.13.0 h1:M/VsI/NWTcs2+vtWBG7793zmlCimKlSMY5LXaiztXV8=
github.com/goplus/gox v1.14.9-p1 h1:aJZJBG1fcg96kOFQ03GsEyw2fRQIMvEu/A/6jxhVmQQ=
github.com/goplus/gox v1.14.9-p1/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/mod v0.13.0/go.mod h1:Qin2SxKoK+3cNFwtg4Jnk1F9Qn5vR6Kwlvr6lTCRJNI=
github.com/goplus/mod v0.13.1 h1:nASVYN3WVyNQkPQjLW5V9W+Pd/caRNKtGGXpa7RCVRw=
github.com/goplus/mod v0.13.1/go.mod h1:goxk0bKESjL1Nk6t/JydJoPfJzQJfU4FomlUzLyBheA=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand All @@ -32,8 +33,9 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
11 changes: 11 additions & 0 deletions imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
if mod := p.mod; hasModfile(mod) {
ret, e := mod.Lookup(pkgPath)
if e != nil {
if isPkgInMod(pkgPath, "github.com/qiniu/x") {
return p.impFrom.ImportFrom(pkgPath, p.gop.Root, 0)
}
return nil, e
}
switch ret.Type {
Expand Down Expand Up @@ -125,6 +128,14 @@ func (p *Importer) genGoExtern(dir string, isExtern bool) (err error) {
return
}

func isPkgInMod(pkgPath, modPath string) bool {
if strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
return suffix == "" || suffix[0] == '/'
}
return false
}

func defaultGoMod(modPath string) []byte {
return []byte(`module ` + modPath + `
Expand Down
37 changes: 29 additions & 8 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io/fs"
"os"
"strings"
"syscall"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
Expand All @@ -37,13 +36,13 @@ import (
)

var (
ErrNotFound = syscall.ENOENT
ErrNotFound = gopmod.ErrNotFound
ErrIgnoreNotated = errors.New("notated error ignored")
)

// NotFound returns if cause err is ErrNotFound or not
func NotFound(err error) bool {
return errors.Err(err) == ErrNotFound
return gopmod.IsNotFound(err)
}

// IgnoreNotated returns if cause err is ErrIgnoreNotated or not.
Expand Down Expand Up @@ -122,6 +121,7 @@ type Config struct {
Importer types.Importer

IgnoreNotatedError bool
DontUpdateGoMod bool
}

func LoadMod(dir string) (mod *gopmod.Module, err error) {
Expand Down Expand Up @@ -172,12 +172,12 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
return nil, nil, ErrNotFound
}

gop := conf.Gop
if gop == nil {
gop = gopenv.Get()
}
imp := conf.Importer
if imp == nil {
gop := conf.Gop
if gop == nil {
gop = gopenv.Get()
}
imp = NewImporter(mod, gop, fset)
}

Expand Down Expand Up @@ -218,9 +218,29 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
if out == nil {
return nil, nil, ErrNotFound
}
if pkgTest != nil && genTestPkg {
if genTestPkg && pkgTest != nil {
test, err = cl.NewPackage("", pkgTest, clConf)
}
saveWithGopMod(mod, gop, out, test, conf)
return
}

func saveWithGopMod(mod *gopmod.Module, gop *env.Gop, out, test *gox.Package, conf *Config) {
if !conf.DontUpdateGoMod && mod.HasModfile() {
flags := checkGopDeps(out)
if test != nil {
flags |= checkGopDeps(test)
}
if flags != 0 {
mod.SaveWithGopMod(gop, flags)
}
}
}

func checkGopDeps(pkg *gox.Package) (flags int) {
pkg.ForEachFile(func(fname string, file *gox.File) {
flags |= file.CheckGopDeps(pkg)
})
return
}

Expand Down Expand Up @@ -281,6 +301,7 @@ func LoadFiles(dir string, files []string, conf *Config) (out *gox.Package, err
}
break
}
saveWithGopMod(mod, gop, out, nil, conf)
return
}

Expand Down

0 comments on commit a461aff

Please sign in to comment.