Skip to content

Commit

Permalink
gop go pkgPath/...
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 18, 2022
1 parent 6bf71fe commit 9d5eca3
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cmd/internal/gengo/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ func runCmd(cmd *base.Command, args []string) {
case *gopprojs.DirProj:
err = gop.GenGo(v.Dir, nil)
if err == syscall.ENOENT {
fmt.Fprintf(os.Stderr, "gop.GenGo %v: no source files\n", v.Dir)
fmt.Fprintf(os.Stderr, "gop go %v: no source files\n", v.Dir)
err = nil
}
case *gopprojs.PkgPathProj:
err = gop.GenGoPkgPath(v.Path, nil)
if err == syscall.ENOENT {
fmt.Fprintf(os.Stderr, "gop go %v: no source files\n", v.Path)
err = nil
}
default:
Expand Down
68 changes: 65 additions & 3 deletions gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ const (
autoGen2TestFile = "gop_autogen2_test.go"
)

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

func GenGo(dir string, conf *Config) (err error) {
recursively := strings.HasSuffix(dir, "/...")
if recursively {
dir = dir[:len(dir)-4]
}
return genGoDir(dir, conf, recursively)
}

func genGoDir(dir string, conf *Config, recursively bool) (err error) {
if recursively {
return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err == nil && d.IsDir() {
err = genGoDir(path, conf, true)
err = genGoIn(path, conf, true)
if err != nil {
if err == syscall.ENOENT {
err = nil
Expand All @@ -50,10 +58,10 @@ func GenGo(dir string, conf *Config) (err error) {
return err
})
}
return genGoDir(dir, conf, false)
return genGoIn(dir, conf, false)
}

func genGoDir(dir string, conf *Config, prompt bool) (err error) {
func genGoIn(dir string, conf *Config, prompt bool) (err error) {
out, test, err := LoadDir(dir, conf)
if err != nil {
return
Expand Down Expand Up @@ -82,3 +90,57 @@ func genGoDir(dir string, conf *Config, prompt bool) (err error) {
}
return
}

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

func GenGoPkgPath(pkgPath string, conf *Config) (err error) {
recursively := strings.HasSuffix(pkgPath, "/...")
if recursively {
pkgPath = pkgPath[:len(pkgPath)-4]
}
getPkgPathDo(pkgPath, gopEnv(conf), func(dir string) {
err = genGoDir(dir, conf, recursively)
}, func(e error) {
err = e
})
return
}

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

func GenGoFiles(autogen string, files []string, conf *Config) (err error) {
if autogen == "" {
autogen = "gop_autogen.go"
if len(files) == 1 {
file := files[0]
srcDir, fname := filepath.Split(file)
if hasMultiFiles(srcDir, ".gop") {
autogen = filepath.Join(srcDir, "gop_autogen_"+fname+".go")
}
}
}
out, err := LoadFiles(files, conf)
if err != nil {
return
}
return out.WriteFile(autogen)
}

func hasMultiFiles(srcDir string, ext string) bool {
var has bool
if f, err := os.Open(srcDir); err == nil {
defer f.Close()
fis, _ := f.ReadDir(-1)
for _, fi := range fis {
if !fi.IsDir() && filepath.Ext(fi.Name()) == ext {
if has {
return true
}
has = true
}
}
}
return false
}

// -----------------------------------------------------------------------------
49 changes: 48 additions & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import (
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/gox"
"github.com/goplus/gox/packages"
"github.com/goplus/mod/env"
"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
)

type Config struct {
Gop *gopmod.GopEnv
Gop *env.Gop
Fset *token.FileSet
Filter func(fs.FileInfo) bool
Importer types.Importer
Expand All @@ -43,6 +46,8 @@ type Config struct {
CheckModChanged bool
}

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

func LoadDir(dir string, conf *Config) (out, test *gox.Package, err error) {
if conf == nil {
conf = new(Config)
Expand Down Expand Up @@ -121,6 +126,44 @@ func LoadDir(dir string, conf *Config) (out, test *gox.Package, err error) {
return
}

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

func LoadPkgPath(pkgPath string, conf *Config) (out, test *gox.Package, err error) {
getPkgPathDo(pkgPath, gopEnv(conf), func(dir string) {
out, test, err = LoadDir(dir, conf)
}, func(e error) {
err = e
})
return
}

func getPkgPathDo(pkgPath string, gop *env.Gop, doSth func(dir string), onErr func(e error)) {
modPath, leftPart := splitPkgPath(pkgPath)
modVer, _, err := modfetch.Get(gop, modPath)
if err != nil {
onErr(err)
} else if dir, err := modcache.Path(modVer); err != nil {
onErr(err)
} else {
doSth(dir + leftPart)
}
}

func splitPkgPath(pkgPath string) (modPath, leftPart string) {
return pkgPath, ""
}

func gopEnv(conf *Config) *env.Gop {
if conf != nil {
if gop := conf.Gop; gop != nil {
return gop
}
}
return gopenv.Get()
}

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

func LoadFiles(files []string, conf *Config) (out *gox.Package, err error) {
if conf == nil {
conf = new(Config)
Expand All @@ -147,7 +190,11 @@ func LoadFiles(files []string, conf *Config) (out *gox.Package, err error) {
return
}

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

var (
errMultiPackges = errors.New("multiple packages")
errMultiTestPackges = errors.New("multiple test packages")
)

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

0 comments on commit 9d5eca3

Please sign in to comment.