Skip to content

Commit

Permalink
refactor: simplify node build
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Dec 24, 2022
1 parent baac492 commit 096c187
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 60 deletions.
43 changes: 24 additions & 19 deletions internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ import (
)

type NodeParser[T any] interface {
Display(node *node.Node[T]) string
Parse(id string) (*node.Node[T], error)
Deps(node *node.Node[T]) []string
Display(node *node.Node[T], root *node.Node[T]) string
Entrypoint(entrypoint string) (*node.Node[T], error)
Deps(node *node.Node[T]) ([]*node.Node[T], error)
}

func makeNodes[T any](
entrypoint string,
func computeDeps[T any](
root *node.Node[T],
parser NodeParser[T],
seen map[string]*node.Node[T],
) (*node.Node[T], error) {
root, err := parser.Parse(entrypoint)
if err != nil {
return nil, err
} else if cached, ok := seen[root.Id]; ok {
return cached, nil
) error {
if _, ok := seen[root.Id]; ok {
return nil
} else {
seen[root.Id] = root
}

deps := parser.Deps(root)
deps, err := parser.Deps(root)
if err != nil {
return err
}
for _, dep := range deps {
child, err := makeNodes(dep, parser, seen)
root.AddChild(dep)
err = computeDeps(dep, parser, seen)
if err != nil {
return nil, err
return err
}
root.AddChild(child)
}
return root, nil
return nil
}

type graphNode[T any] struct {
Expand Down Expand Up @@ -70,6 +70,7 @@ const indent = 2

func renderGraph[T any](
parser NodeParser[T],
root *node.Node[T],
nodes []graphNode[T],
) (string, error) {
b := board.MakeBoard()
Expand All @@ -93,7 +94,7 @@ func renderGraph[T any](

err := b.AddBlock(
n.node.Id,
prefix+parser.Display(n.node),
prefix+parser.Display(n.node, root),
indent*n.level+xOffset,
i,
)
Expand All @@ -119,10 +120,14 @@ func RenderGraph[T any](
entrypoint string,
parser NodeParser[T],
) (string, error) {
root, err := makeNodes(entrypoint, parser, map[string]*node.Node[T]{})
root, err := parser.Entrypoint(entrypoint)
if err != nil {
return "", err
}
err = computeDeps(root, parser, map[string]*node.Node[T]{})
if err != nil {
return "", err
}
nodes := sortNodes(root)
return renderGraph(parser, nodes)
return renderGraph(parser, root, nodes)
}
13 changes: 7 additions & 6 deletions internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TestGraph struct {

var _ NodeParser[[]int] = &TestGraph{}

func (t *TestGraph) Parse(id string) (*node.Node[[]int], error) {
func (t *TestGraph) Entrypoint(id string) (*node.Node[[]int], error) {
idInt, err := strconv.Atoi(id)
if err != nil {
return nil, err
Expand All @@ -34,15 +34,16 @@ func (t *TestGraph) Parse(id string) (*node.Node[[]int], error) {
return node.MakeNode(id, id, children), nil
}

func (t *TestGraph) Deps(n *node.Node[[]int]) []string {
result := make([]string, 0)
func (t *TestGraph) Deps(n *node.Node[[]int]) ([]*node.Node[[]int], error) {
result := make([]*node.Node[[]int], 0)
for _, child := range n.Data {
result = append(result, strconv.Itoa(child))
c, _ := t.Entrypoint(strconv.Itoa(child))
result = append(result, c)
}
return result
return result, nil
}

func (t *TestGraph) Display(n *node.Node[[]int]) string {
func (t *TestGraph) Display(n *node.Node[[]int], _ *node.Node[[]int]) string {
return n.Id
}

Expand Down
30 changes: 22 additions & 8 deletions internal/js/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func normalizeId(id string) (string, error) {
return absPath, nil
}

func (p *parser) Parse(id string) (*node.Node[Data], error) {
func (p *parser) Entrypoint(id string) (*node.Node[Data], error) {
absPath, err := normalizeId(id)
if err != nil {
return nil, err
Expand All @@ -90,21 +90,35 @@ func (p *parser) Parse(id string) (*node.Node[Data], error) {
}), nil
}

func (p *parser) Deps(n *node.Node[Data]) []string {
func (p *parser) Deps(n *node.Node[Data]) ([]*node.Node[Data], error) {
matched := importRegex.FindAll(n.Data.content, -1)
deps := make([]string, 0)
deps := make([]*node.Node[Data], 0)
for _, importMatch := range matched {
importPathMatched := importPathRegex.Find(importMatch)
match := strings.Trim(string(importPathMatched), "'\" \n")
if match[:1] != "." {
continue
}
match = path.Join(n.Data.dirname, match)
deps = append(deps, match)
entrypoint := path.Join(n.Data.dirname, match)
dep, err := p.Entrypoint(entrypoint)
if err == nil {
deps = append(deps, dep)
} else {
deps = append(deps, node.MakeNode(entrypoint, n.Data.dirname, Data{
dirname: "",
content: []byte{},
}))
}
}
return deps
return deps, nil
}

func (p *parser) Display(n *node.Node[Data]) string {
return path.Join(path.Base(path.Dir(n.Id)), path.Base(n.Id))
func (p *parser) Display(n *node.Node[Data], root *node.Node[Data]) string {
base := root.Data.dirname
rel, err := filepath.Rel(base, n.Id)
if err != nil {
return n.Id
} else {
return rel
}
}
49 changes: 22 additions & 27 deletions internal/js/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ import (
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

const testFolder = "parser_test"

func TestParser_Parse(t *testing.T) {
func TestParser_Entrypoint(t *testing.T) {
a := require.New(t)
id := path.Join(testFolder, t.Name()+".js")
absPath, err := filepath.Abs(id)
a.NoError(err)

node, err := Parser.Parse(id)
node, err := Parser.Entrypoint(id)
a.NoError(err)
a.Equal(node.Id, absPath)
a.Equal(node.Data.dirname, path.Dir(absPath))
a.Equal(node.Data.content, []byte("console.log(\"hello world!\")\n"))
}

func TestParser_Deps(t *testing.T) {
thisDir, _ := os.Getwd()

tests := []struct {
Name string
Expected []string
Expand All @@ -36,45 +33,45 @@ func TestParser_Deps(t *testing.T) {
{
Name: "deps",
Expected: []string{
"geometries/Geometries.js",
"geometries/Geometries.js",
"parser_test/.export",
"parser_test/views/ListView",
"parser_test/views/AddView",
"parser_test/views/EditView",
"parser_test/views/ListView",
"parser_test/views/AddView",
"parser_test/views/EditView",
"../geometries/Geometries.js",
"../geometries/Geometries.js",
".export",
"views/ListView",
"views/AddView",
"views/EditView",
"views/ListView",
"views/AddView",
"views/EditView",
},
},
{
Name: "with-imports",
Normalize: true,
Expected: []string{
"parser_test/with-imports-imported/imported.js",
"with-imports-imported/imported.js",
},
},
{
Name: "with-imports-index",
Normalize: true,
Expected: []string{
"parser_test/with-imports-index-imported/other.js",
"parser_test/with-imports-index-imported/one.js",
"parser_test/with-imports-index-imported/index.js",
"with-imports-index-imported/other.js",
"with-imports-index-imported/one.js",
"with-imports-index-imported/index.js",
},
},
{
Name: "with-imports-nested",
Normalize: true,
Expected: []string{
"parser_test/with-imports-nested/generated/generated.js",
"generated/generated.js",
},
},

{
Name: "custom-1",
Expected: []string{
"@parsers/DateSchema",
"../@parsers/DateSchema",
},
},
}
Expand All @@ -87,16 +84,14 @@ func TestParser_Deps(t *testing.T) {
id = path.Join(testFolder, path.Base(t.Name()), "index.js")
}

node, err := Parser.Parse(id)
node, err := Parser.Entrypoint(id)
a.NoError(err)
deps, err := Parser.Deps(node)
a.NoError(err)
deps := Parser.Deps(node)
result := make([]string, 0)
for _, dep := range deps {
if tt.Normalize {
dep, err = normalizeId(dep)
a.NoError(err)
}
result = append(result, strings.ReplaceAll(dep, thisDir+"/", ""))
display := Parser.Display(dep, node)
result = append(result, display)
}

a.Equal(tt.Expected, result)
Expand Down
File renamed without changes.

0 comments on commit 096c187

Please sign in to comment.