Skip to content

Commit

Permalink
pkgtool: Fix PkgPathFromDir and improve PkgNameFromDir
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLuo committed May 5, 2022
1 parent 8bd4856 commit 7e9b8d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
5 changes: 2 additions & 3 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ func (g *Generator) generateGRPC(data *ifacetool.Data) (files []*generator.File,
if err = ensureDir(pbOutDir); err != nil {
return files, err
}
pbPkgPath := pkgtool.PkgPathFromDir(pbOutDir)
f, err := g.proto.Generate(pbPkgPath, data, service)
f, err := g.proto.Generate(pbOutDir, data, service)
if err != nil {
return files, err
}
Expand All @@ -273,7 +272,7 @@ func (g *Generator) generateGRPC(data *ifacetool.Data) (files []*generator.File,

// Generate the glue code for adapting the gRPC definition to Go kit.
pkgInfo := g.getPkgInfo(outDir)
f, err = g.grpc.Generate(pkgInfo, pbPkgPath, data, service)
f, err = g.grpc.Generate(pkgInfo, pbOutDir, data, service)
if err != nil {
return files, err
}
Expand Down
6 changes: 3 additions & 3 deletions gen/grpc/grpc/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ func New(opts *Options) *Generator {
return &Generator{opts: opts}
}

func (g *Generator) Generate(pkgInfo *generator.PkgInfo, pbPkgPath string, ifaceData *ifacetool.Data, service *parser.Service) (*generator.File, error) {
func (g *Generator) Generate(pkgInfo *generator.PkgInfo, pbOutDir string, ifaceData *ifacetool.Data, service *parser.Service) (*generator.File, error) {
data := struct {
PBPkgPath string
PBPkgPrefix string
Data *ifacetool.Data
PkgInfo *generator.PkgInfo
Service *parser.Service
}{
PBPkgPath: pbPkgPath,
PBPkgPrefix: pkgtool.PkgNameFromDir(pbPkgPath) + ".",
PBPkgPath: pkgtool.PkgPathFromDir(pbOutDir),
PBPkgPrefix: pkgtool.PkgNameFromDir(pbOutDir) + ".",
Data: ifaceData,
PkgInfo: pkgInfo,
Service: service,
Expand Down
6 changes: 3 additions & 3 deletions gen/grpc/proto/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func New(opts *Options) *Generator {
return &Generator{opts: opts}
}

func (g *Generator) Generate(pkgPath string, ifaceData *ifacetool.Data, service *parser.Service) (*generator.File, error) {
func (g *Generator) Generate(outDir string, ifaceData *ifacetool.Data, service *parser.Service) (*generator.File, error) {
data := struct {
PkgPath string
PkgName string
Service *parser.Service
Messages map[string]*parser.Type
}{
PkgPath: pkgPath,
PkgName: pkgtool.PkgNameFromDir(pkgPath),
PkgPath: pkgtool.PkgPathFromDir(outDir),
PkgName: pkgtool.PkgNameFromDir(outDir),
Service: service,
Messages: getMessages(service),
}
Expand Down
53 changes: 29 additions & 24 deletions pkg/pkgtool/pkgtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"errors"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"path/filepath"
"strings"

"github.com/RussellLuo/kun/pkg/ifacetool"
"github.com/RussellLuo/kun/pkg/ifacetool/moq"
Expand Down Expand Up @@ -128,39 +126,46 @@ func getInterfaceDoc(ts *ast.TypeSpec, gd *ast.GenDecl) *ast.CommentGroup {
return nil
}

func PkgPathFromDir(dir string) (pkgPath string) {
func PkgPathFromDir(dir string) string {
abs, err := filepath.Abs(dir)
if err == nil {
pkgPath = stripGopath(abs)
if err != nil {
panic(err)
}
return
}

func PkgNameFromDir(dir string) string {
pkg, _ := pkgInfoFromPath(dir, packages.NeedName)
if pkg != nil && pkg.Name != "" {
return pkg.Name
pkg, err := pkgInfoFromPath(abs, packages.NeedModule)
if err != nil {
panic(err)
}

// Default to the directory name.
abs, err := filepath.Abs(dir)
if pkg == nil || pkg.Module == nil {
return ""
}

// Remove the module root directory.
rel, err := filepath.Rel(pkg.Module.Dir, abs)
if err != nil {
panic(err)
}
return filepath.Base(abs)
// Add the module path prefix.
return filepath.Join(pkg.Module.Path, rel)
}

// stripGopath takes the directory to a package and removes the
// $GOPATH/src path to get the canonical package name.
func stripGopath(p string) string {
for _, srcDir := range build.Default.SrcDirs() {
rel, err := filepath.Rel(srcDir, p)
if err != nil || strings.HasPrefix(rel, "..") {
continue
}
return filepath.ToSlash(rel)
func PkgNameFromDir(dir string) string {
abs, err := filepath.Abs(dir)
if err != nil {
panic(err)
}

pkg, err := pkgInfoFromPath(abs, packages.NeedName)
if err != nil {
panic(err)
}
if pkg != nil && pkg.Name != "" {
return pkg.Name
}
return p

// Default to the directory name.
return filepath.Base(abs)
}

func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package, error) {
Expand Down

0 comments on commit 7e9b8d4

Please sign in to comment.