Skip to content

Commit

Permalink
Support third-party sources
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed May 16, 2024
1 parent d88ced0 commit 3a65062
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
48 changes: 32 additions & 16 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (m *MappingOperand) ConfigLoaded(path string) []error {
errs = append(errs, fmt.Errorf("%s:\t%s.name must not be empty", m.SourceFile, path))
}

if !filepath.IsAbs(m.Package) {
if !isModPackage(m.Package) && !filepath.IsAbs(m.Package) {
m.Package = filepath.Join(filepath.Dir(m.SourceFile), m.Package)
}

Expand Down Expand Up @@ -741,24 +741,40 @@ func (g *generator) Generate() error {
}, len(mappings))
i := 0
for _, mapping := range mappings {
LogFunc(LogLevelInfo, "Parse %s#%s", mapping.A.Package, mapping.A.Name)
a, err := ParseStruct(mapping.A.Package, mapping.A.Name, mctx)
if err != nil {
return err
}
LogFunc(LogLevelInfo, "Parse %s#%s", mapping.B.Package, mapping.B.Name)
b, err := ParseStruct(mapping.B.Package, mapping.B.Name, mctx)
err := func() error {
oldCwd, _ := os.Getwd()
rootPath, err := findRootPath(mapping.SourceFile)
if err != nil {
return err
}
_ = os.Chdir(rootPath)
defer func() {
_ = os.Chdir(oldCwd)
}()

LogFunc(LogLevelInfo, "Parse %s#%s", mapping.A.Package, mapping.A.Name)
a, err := ParseStruct(mapping.A.Package, mapping.A.Name, mctx)
if err != nil {
return err
}
LogFunc(LogLevelInfo, "Parse %s#%s", mapping.B.Package, mapping.B.Name)
b, err := ParseStruct(mapping.B.Package, mapping.B.Name, mctx)
if err != nil {
return err
}
if len(pkg) > 0 && pkg != mapping.Package {
return fmt.Errorf("Destination %s have multiple package names", dest)
}
pkg = mapping.Package
lst[i].Mapping = mapping
lst[i].A = a
lst[i].B = b
i++
return nil
}()
if err != nil {
return err
}
if len(pkg) > 0 && pkg != mapping.Package {
return fmt.Errorf("Destination %s have multiple package names", dest)
}
pkg = mapping.Package
lst[i].Mapping = mapping
lst[i].A = a
lst[i].B = b
i++
}
printer.WriteDoNotEdit()
p(`package %s`, pkg)
Expand Down
13 changes: 2 additions & 11 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ func ParseStruct(path string, name string, mctx *MappingContext) (types.Object,
}

// ParseFile parses a given Go source code file.
// Package will be imported as we in working directory.
// You may need to os.Chdir() before this method.
func ParseFile(pkgPath string, mctx *MappingContext) (*types.Package, error) {
oldCwd, _ := os.Getwd()
pkgPath, _ = filepath.Abs(pkgPath)
rootPath, err := findRootPath(pkgPath)
if err != nil {
return nil, err
}
_ = os.Chdir(rootPath)
defer func() {
_ = os.Chdir(oldCwd)
}()

absPkgPath, err := toAbsoluteImportPath(pkgPath)
if err != nil {
return nil, err
Expand Down
36 changes: 36 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package sesame

import (
"bytes"
"fmt"
"go/types"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -263,6 +265,25 @@ func mappersName(sourceType, destType types.Type) string {
var modulePattern = regexp.MustCompile(`^\s*module\s*(.*)`)

func toAbsoluteImportPath(path string) (string, error) {
if isModPackage(path) {
parts := strings.Split(path, "/")
i := len(parts) - 1
for ; i >= 0; i-- {
pkg := strings.Join(parts[:i+1], "/")
cmd := exec.Command("go", "list", "-f", "'{{.Dir}}'", "-m", pkg)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
continue
}
p := strings.Trim(strings.TrimSpace(out.String()), "'")
if i != len(parts)-1 {
p = filepath.Join(p, filepath.Join(parts[i+1:]...))
}
return p, nil
}
return "", fmt.Errorf("Can not resolve qualified package path: %s", path)
}
var buf []string
start := path
for cur := start; cur != filepath.Dir(cur); cur = filepath.Dir(cur) {
Expand All @@ -280,3 +301,18 @@ func toAbsoluteImportPath(path string) (string, error) {
}
return "", fmt.Errorf("Can not resolve qualified package path: %s", path)
}

func isModPackage(pkg string) bool {
if filepath.IsAbs(pkg) {
return false
}
if strings.Contains(pkg, "./") || strings.Contains(pkg, "../") || strings.Contains(pkg, ".\\") || strings.Contains(pkg, "..\\") {
return false
}

parts := strings.Split(pkg, "/")
if len(parts) < 2 {
return false
}
return strings.Contains(parts[0], ".")
}

0 comments on commit 3a65062

Please sign in to comment.