diff --git a/costello/parser/_fixtures/new_from_roots/e/e.go b/costello/parser/_fixtures/new_from_roots/e/e.go new file mode 100644 index 0000000..991a1f6 --- /dev/null +++ b/costello/parser/_fixtures/new_from_roots/e/e.go @@ -0,0 +1,8 @@ +package q + +import "github.com/gobuffalo/packr" + +func init() { + packr.New("tom", "petty") + packr.NewBox("./heartbreakers") +} diff --git a/costello/parser/_fixtures/new_from_roots/q.go b/costello/parser/_fixtures/new_from_roots/q.go new file mode 100644 index 0000000..32fa130 --- /dev/null +++ b/costello/parser/_fixtures/new_from_roots/q.go @@ -0,0 +1,7 @@ +package q + +import "github.com/gobuffalo/packr" + +func init() { + packr.New("aretha", "franklin") +} diff --git a/costello/parser/_fixtures/new_from_roots/w/w.go b/costello/parser/_fixtures/new_from_roots/w/w.go new file mode 100644 index 0000000..7905807 --- /dev/null +++ b/costello/parser/_fixtures/new_from_roots/w/w.go @@ -0,0 +1,5 @@ +package main + +func main() { + +} diff --git a/costello/parser/box.go b/costello/parser/box.go new file mode 100644 index 0000000..cf63332 --- /dev/null +++ b/costello/parser/box.go @@ -0,0 +1,34 @@ +package parser + +import ( + "encoding/json" + "os" + "strings" +) + +type Box struct { + Name string + Path string + Package string + PWD string + PackageDir string +} + +func (b Box) String() string { + x, _ := json.Marshal(b) + return string(x) +} + +func NewBox(name string, path string) *Box { + if len(name) == 0 { + name = path + } + name = strings.Replace(name, "\"", "", -1) + pwd, _ := os.Getwd() + box := &Box{ + Name: name, + Path: path, + PWD: pwd, + } + return box +} diff --git a/parser/file.go b/costello/parser/file.go similarity index 100% rename from parser/file.go rename to costello/parser/file.go diff --git a/costello/parser/parser.go b/costello/parser/parser.go new file mode 100644 index 0000000..0a52807 --- /dev/null +++ b/costello/parser/parser.go @@ -0,0 +1,62 @@ +package parser + +import ( + "bytes" + "fmt" + "io/ioutil" + + "github.com/karrick/godirwalk" + "github.com/pkg/errors" +) + +type Parser struct { + Prospects []*File +} + +func (p *Parser) Run() ([]*Box, error) { + var boxes []*Box + for _, pros := range p.Prospects { + fmt.Println("Parser: parsing", pros.Name()) + v := NewVisitor(pros) + pbr, err := v.Run() + if err != nil { + return boxes, errors.WithStack(err) + } + for _, b := range pbr { + boxes = append(boxes, b) + } + } + return boxes, nil +} + +func New(prospects ...*File) *Parser { + return &Parser{ + Prospects: prospects, + } +} + +func NewFromRoots(roots []string, ignore ...string) (*Parser, error) { + p := New() + callback := func(path string, de *godirwalk.Dirent) error { + if !IsProspect(path, ignore...) || de.IsDir() { + return nil + } + b, err := ioutil.ReadFile(path) + if err != nil { + return errors.WithStack(err) + } + p.Prospects = append(p.Prospects, NewFile(path, bytes.NewReader(b))) + return nil + } + opts := &godirwalk.Options{ + FollowSymbolicLinks: true, + Callback: callback, + } + for _, root := range roots { + err := godirwalk.Walk(root, opts) + if err != nil { + return p, errors.WithStack(err) + } + } + return p, nil +} diff --git a/parser/parser_test.go b/costello/parser/parser_test.go similarity index 71% rename from parser/parser_test.go rename to costello/parser/parser_test.go index 1694b70..1fe2e9b 100644 --- a/parser/parser_test.go +++ b/costello/parser/parser_test.go @@ -21,6 +21,17 @@ func Test_Parser_Run(t *testing.T) { r.Len(boxes, 4) } +func Test_NewFrom_Roots(t *testing.T) { + r := require.New(t) + p, err := NewFromRoots([]string{"../parser/_fixtures/new_from_roots"}) + r.NoError(err) + + boxes, err := p.Run() + r.NoError(err) + + r.Len(boxes, 3) +} + const basicGoTmpl = `package %s import "github.com/gobuffalo/packr" diff --git a/parser/prospect.go b/costello/parser/prospect.go similarity index 100% rename from parser/prospect.go rename to costello/parser/prospect.go diff --git a/parser/prospect_test.go b/costello/parser/prospect_test.go similarity index 100% rename from parser/prospect_test.go rename to costello/parser/prospect_test.go diff --git a/parser/visitor.go b/costello/parser/visitor.go similarity index 98% rename from parser/visitor.go rename to costello/parser/visitor.go index 83b6c62..006903e 100644 --- a/parser/visitor.go +++ b/costello/parser/visitor.go @@ -268,18 +268,14 @@ func (v *Visitor) addBox(name string, path string) { } name = strings.Replace(name, "\"", "", -1) if _, ok := v.boxes[name]; !ok { - pwd, _ := os.Getwd() + box := NewBox(name, path) pd := filepath.Dir(v.File.Name()) + pwd, _ := os.Getwd() if !filepath.IsAbs(pd) { pd = filepath.Join(pwd, pd) } - box := &Box{ - Name: name, - Path: path, - Package: v.Package, - PackageDir: pd, - PWD: pwd, - } + box.PackageDir = pd + box.Package = v.Package v.boxes[name] = box } } diff --git a/parser/visitor_test.go b/costello/parser/visitor_test.go similarity index 100% rename from parser/visitor_test.go rename to costello/parser/visitor_test.go diff --git a/go.mod b/go.mod index 9a926ad..d1c9a0d 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/stretchr/testify v1.2.2 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 // indirect golang.org/x/net v0.0.0-20180906233101-161cd47e91fd // indirect - golang.org/x/sys v0.0.0-20180907202204-917fdcba135d // indirect + golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect golang.org/x/text v0.3.0 // indirect gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect diff --git a/go.sum b/go.sum index c88913b..be3dbc4 100644 --- a/go.sum +++ b/go.sum @@ -78,8 +78,8 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180907202204-917fdcba135d h1:kWn1hlsqeUrk6JsLJO0ZFyz9bMg8u85voZlIuc68ZU4= -golang.org/x/sys v0.0.0-20180907202204-917fdcba135d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48 h1:PIz+xUHW4G/jqfFWeKhQ96ZV/t2HDsXfWj923rV0bZY= diff --git a/parser/box.go b/parser/box.go deleted file mode 100644 index c921442..0000000 --- a/parser/box.go +++ /dev/null @@ -1,16 +0,0 @@ -package parser - -import "encoding/json" - -type Box struct { - Name string - Path string - Package string - PWD string - PackageDir string -} - -func (b Box) String() string { - x, _ := json.Marshal(b) - return string(x) -} diff --git a/parser/parser.go b/parser/parser.go deleted file mode 100644 index e885922..0000000 --- a/parser/parser.go +++ /dev/null @@ -1,33 +0,0 @@ -package parser - -import ( - "fmt" - - "github.com/pkg/errors" -) - -type Parser struct { - Prospects []*File -} - -func (p *Parser) Run() ([]*Box, error) { - var boxes []*Box - for _, pros := range p.Prospects { - fmt.Println("Parser: parsing", pros.Name()) - v := NewVisitor(pros) - pbr, err := v.Run() - if err != nil { - return boxes, errors.WithStack(err) - } - for _, b := range pbr { - boxes = append(boxes, b) - } - } - return boxes, nil -} - -func New(prospects ...*File) *Parser { - return &Parser{ - Prospects: prospects, - } -}