Skip to content

Commit

Permalink
gopmod: PkgId
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Feb 12, 2024
1 parent 9c8c25b commit 4176bf4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
22 changes: 22 additions & 0 deletions gopmod/gopmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ import (
"golang.org/x/mod/module"
)

func TestPkgId(t *testing.T) {
mod := New(modtest.GopClass(t))
if id, err := mod.PkgId(""); err != ErrInvalidPkgPath || id != "" {
t.Fatal("mod.PkgId:", id, err)
}
if id, err := mod.PkgId("."); err != ErrInvalidPkgPath || id != "" {
t.Fatal("mod.PkgId:", id, err)
}
if id, err := mod.PkgId("fmt"); err != nil || id != "fmt" {
t.Fatal("mod.PkgId fmt:", id, err)
}
if id, err := mod.PkgId("github.com/goplus/community/bar"); err != nil || id != "/foo/bar" {
t.Fatal("mod.PkgId github.com/goplus/community/bar:", id, err)
}
if _, err := mod.PkgId("github.com/qiniu/x/mockhttp"); err != nil {
t.Fatal("mod.PkgId github.com/qiniu/x/mockhttp:", err)
}
if _, err := mod.PkgId("github.com/qiniu/y/mockhttp"); err == nil {
t.Fatal("mod.PkgId github.com/qiniu/y/mockhttp: no error?")
}
}

func TestLookup(t *testing.T) {
mod := New(modtest.GopClass(t))
if modv, ok := mod.LookupDepMod("github.com/qiniu/x"); !ok || modv.Version != "v1.13.2" {
Expand Down
34 changes: 33 additions & 1 deletion gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
"golang.org/x/mod/module"
)

var (
ErrInvalidPkgPath = errors.New("invalid package path")
)

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

type Module struct {
Expand Down Expand Up @@ -84,6 +88,32 @@ func (p *Module) PkgType(pkgPath string) PkgType {
return PkgtStandard
}

// PkgId returns an unique package id of specified package.
func (p *Module) PkgId(pkgPath string) (string, error) {
if pkgPath == "" {
return "", ErrInvalidPkgPath
}
modPath := p.Path()
if isPkgInMod(pkgPath, modPath) {
return p.Root() + pkgPath[len(modPath):], nil
}
if pkgPath[0] == '.' { // local package: please convert it first
return "", ErrInvalidPkgPath
}
domain := pkgPath
if pos := strings.Index(pkgPath, "/"); pos > 0 {
domain = pkgPath[:pos]
}
if strings.Contains(domain, ".") {
pkg, err := p.lookupExternPkg(pkgPath)
if err != nil {
return "", err
}
return pkg.Dir, nil
}
return pkgPath, nil
}

func isPkgInMod(pkgPath, modPath string) bool {
if modPath != "" && strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
Expand All @@ -103,7 +133,7 @@ type Package struct {
func (p *Module) Lookup(pkgPath string) (pkg *Package, err error) {
switch pt := p.PkgType(pkgPath); pt {
case PkgtStandard:
modDir := runtime.GOROOT() + "/src"
modDir := goroot + "/src"
pkg = &Package{Type: PkgtStandard, ModDir: modDir, Dir: filepath.Join(modDir, pkgPath)}
case PkgtModule:
modPath := p.Path()
Expand Down Expand Up @@ -204,4 +234,6 @@ func (e *MissingError) Error() string {
// Default represents the default gop.mod object.
var Default = New(modload.Default)

var goroot = runtime.GOROOT()

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

0 comments on commit 4176bf4

Please sign in to comment.