new include

This commit is contained in:
Mark Bates 2019-12-13 11:24:17 -05:00
parent 5497054b42
commit 6c1b23a5cb
2 changed files with 33 additions and 20 deletions

View File

@ -5,6 +5,9 @@ import (
"fmt" "fmt"
"go/token" "go/token"
"os" "os"
"path/filepath"
"github.com/markbates/pkger/here"
) )
var _ Decl = IncludeDecl{} var _ Decl = IncludeDecl{}
@ -15,6 +18,34 @@ type IncludeDecl struct {
value string value string
} }
func NewInclude(her here.Info, inc string) (IncludeDecl, error) {
var id IncludeDecl
pt, err := her.Parse(inc)
if err != nil {
return id, err
}
if pt.Pkg != her.ImportPath {
her, err = here.Package(pt.Pkg)
if err != nil {
return id, err
}
}
abs := filepath.Join(her.Module.Dir, pt.Name)
f := &File{
Abs: abs,
Path: pt,
Here: her,
}
return IncludeDecl{
value: inc,
file: f,
}, nil
}
func (d IncludeDecl) String() string { func (d IncludeDecl) String() string {
return fmt.Sprintf("pkger.Include(%q)", d.value) return fmt.Sprintf("pkger.Include(%q)", d.value)
} }

View File

@ -226,30 +226,12 @@ func (p *Parser) parse() error {
func (p *Parser) parseIncludes() error { func (p *Parser) parseIncludes() error {
for _, i := range p.includes { for _, i := range p.includes {
pt, err := p.Info.Parse(i) d, err := NewInclude(p.Info, i)
if err != nil { if err != nil {
return err return err
} }
her := p.Info p.decls["Include"] = append(p.decls["Include"], d)
if pt.Pkg != her.ImportPath {
her, err = here.Package(pt.Pkg)
if err != nil {
return err
}
}
abs := filepath.Join(her.Module.Dir, pt.Name)
f := &File{
Abs: abs,
Path: pt,
Here: her,
}
p.decls["Include"] = append(p.decls["Include"], IncludeDecl{
value: i,
file: f,
})
} }
return nil return nil
} }