Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
getting a parser from files now works
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 9, 2018
1 parent 576cd2c commit 283d9a1
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 60 deletions.
8 changes: 8 additions & 0 deletions costello/parser/_fixtures/new_from_roots/e/e.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package q

import "github.com/gobuffalo/packr"

func init() {
packr.New("tom", "petty")
packr.NewBox("./heartbreakers")
}
7 changes: 7 additions & 0 deletions costello/parser/_fixtures/new_from_roots/q.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package q

import "github.com/gobuffalo/packr"

func init() {
packr.New("aretha", "franklin")
}
5 changes: 5 additions & 0 deletions costello/parser/_fixtures/new_from_roots/w/w.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {

}
34 changes: 34 additions & 0 deletions costello/parser/box.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
62 changes: 62 additions & 0 deletions costello/parser/parser.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions parser/parser_test.go → costello/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 4 additions & 8 deletions parser/visitor.go → costello/parser/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 0 additions & 16 deletions parser/box.go

This file was deleted.

33 changes: 0 additions & 33 deletions parser/parser.go

This file was deleted.

0 comments on commit 283d9a1

Please sign in to comment.