From 6c1b23a5cb94c279422c75c50e9b23720087735c Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 13 Dec 2019 11:24:17 -0500 Subject: [PATCH] new include --- parser/include.go | 31 +++++++++++++++++++++++++++++++ parser/parser.go | 22 ++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/parser/include.go b/parser/include.go index ef5409f..cb95500 100644 --- a/parser/include.go +++ b/parser/include.go @@ -5,6 +5,9 @@ import ( "fmt" "go/token" "os" + "path/filepath" + + "github.com/markbates/pkger/here" ) var _ Decl = IncludeDecl{} @@ -15,6 +18,34 @@ type IncludeDecl struct { 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 { return fmt.Sprintf("pkger.Include(%q)", d.value) } diff --git a/parser/parser.go b/parser/parser.go index 99f6ce2..f64633f 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -226,30 +226,12 @@ func (p *Parser) parse() error { func (p *Parser) parseIncludes() error { for _, i := range p.includes { - pt, err := p.Info.Parse(i) + d, err := NewInclude(p.Info, i) if err != nil { return err } - her := p.Info - 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, - }) + p.decls["Include"] = append(p.decls["Include"], d) } return nil }