Skip to content

Commit

Permalink
fix #296 - add go modules support
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Kostyaev <feo.me@ya.ru>
  • Loading branch information
Sergey Kostyaev committed Jul 5, 2019
1 parent 54324d6 commit 780b45a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 74 deletions.
76 changes: 6 additions & 70 deletions generator/processor.go
Expand Up @@ -2,15 +2,12 @@ package generator

import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"go/types"
"path/filepath"
"reflect"
"strings"

"golang.org/x/tools/go/packages"
parseutil "gopkg.in/src-d/go-parse-utils.v1"
)

Expand Down Expand Up @@ -59,70 +56,18 @@ func (p *Processor) write(msg string, args ...interface{}) {

// Do performs all the processing and returns the scanned package.
func (p *Processor) Do() (*Package, error) {
files, err := p.getSourceFiles()
if err != nil {
return nil, err
}

p.Package, err = p.parseSourceFiles(files)
pkgs, err := packages.Load(&packages.Config{
Dir: ".",
Mode: packages.NeedImports | packages.NeedTypes | packages.NeedDeps,
}, p.Path)
if err != nil {
return nil, err
}
p.Package = pkgs[0].Types

return p.processPackage()
}

func (p *Processor) getSourceFiles() ([]string, error) {
pkg, err := build.Default.ImportDir(p.Path, 0)
if err != nil {
return nil, fmt.Errorf("kallax: cannot process directory %s: %s", p.Path, err)
}

var files []string
files = append(files, pkg.GoFiles...)
files = append(files, pkg.CgoFiles...)

if len(files) == 0 {
return nil, fmt.Errorf("kallax: %s: no buildable Go files", p.Path)
}

return joinDirectory(p.Path, p.removeIgnoredFiles(files)), nil
}

func (p *Processor) removeIgnoredFiles(filenames []string) []string {
var output []string
for _, filename := range filenames {
if _, ok := p.Ignore[filename]; ok {
continue
}

output = append(output, filename)
}

return output
}

func (p *Processor) parseSourceFiles(filenames []string) (*types.Package, error) {
var files []*ast.File
fs := token.NewFileSet()
for _, filename := range filenames {
file, err := parser.ParseFile(fs, filename, nil, 0)
if err != nil {
return nil, fmt.Errorf("kallax: parsing package: %s: %s", filename, err)
}

files = append(files, file)
}

config := types.Config{
FakeImportC: true,
Error: func(error) {},
Importer: parseutil.NewImporter(),
}

return config.Check(p.Path, fs, files, new(types.Info))
}

func (p *Processor) processPackage() (*Package, error) {
pkg := NewPackage(p.Package)
var ctors []*types.Func
Expand Down Expand Up @@ -465,15 +410,6 @@ func (p *Processor) processBaseField(m *Model, f *Field) {
}
}

func joinDirectory(directory string, files []string) []string {
result := make([]string, len(files))
for i, file := range files {
result[i] = filepath.Join(directory, file)
}

return result
}

func typeName(typ types.Type) string {
return removeGoPath(typ.String())
}
Expand Down
11 changes: 7 additions & 4 deletions generator/template.go
Expand Up @@ -11,8 +11,7 @@ import (
"strings"
"text/template"

parseutil "gopkg.in/src-d/go-parse-utils.v1"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -486,11 +485,15 @@ func printDocumentWithNumbers(code string) {
const pkgPath = "gopkg.in/src-d/go-kallax.v1/generator"

var pkgAbsPath = func() string {
path, err := parseutil.DefaultGoPath.Abs(pkgPath)
pkgs, err := packages.Load(&packages.Config{
Dir: ".",
Mode: packages.NeedFiles,
}, pkgPath)
if err != nil {
panic(err)
}
return path

return filepath.Dir(pkgs[0].GoFiles[0])
}()

func loadTemplateText(filename string) string {
Expand Down

0 comments on commit 780b45a

Please sign in to comment.