Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #296 - add go modules support #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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