Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out graphviz into its own package #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
438 changes: 219 additions & 219 deletions .makesense/make.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ assets:
mkdir -p assets

assets/basic.svg: assets makesense
@MAKE -C testdata/basic -Bnd | ./makesense --type gv > assets/basic.svg
make -C testdata/basic -Bnd | ./bin/makesense --type gv > assets/basic.svg

assets/c.svg: assets makesense
make -C testdata/c -Bnd | ./makesense --type gv > assets/c.svg
make -C testdata/c -Bnd | ./bin/makesense --type gv > assets/c.svg

assets/basic.png: assets makesense
make -C testdata/basic -Bnd | ./makesense --type dot | dot -Tpng -o assets/basic.png
make -C testdata/basic -Bnd | ./bin/makesense --type dot | dot -Tpng -o assets/basic.png

assets/c.png: assets makesense
make -C testdata/c -Bnd | ./makesense --type dot | dot -Tpng -o assets/c.png
make -C testdata/c -Bnd | ./bin/makesense --type dot | dot -Tpng -o assets/c.png

assets/this.png: assets makesense
make -Bnd | ./makesense --type dot | dot -Tpng -o assets/this.png
make -Bnd | ./bin/makesense --type dot | dot -Tpng -o assets/this.png

build: makesense

makesense: makesense.go makesense_test.go
go build ./...
go build -o ./bin ./...

test: makesense_test.go
go test ./...
Expand Down
Binary file modified assets/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 59 additions & 59 deletions assets/basic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 23 additions & 23 deletions assets/c.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/this.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/blevy/makesense
module github.com/blevz/makesense

go 1.15
go 1.16

require (
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb
Expand Down
21 changes: 21 additions & 0 deletions graphutil/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package graphutil

import (
"bytes"

"github.com/goccy/go-graphviz"
"github.com/goccy/go-graphviz/cgraph"
)

var (
gv = graphviz.New()
)

func ToSvg(graph *cgraph.Graph, layoutType, renderFormat string) ([]byte, error) {
gv.SetLayout(graphviz.Layout(layoutType))
var buf bytes.Buffer
if err := gv.Render(graph, graphviz.Format(renderFormat), &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
11 changes: 5 additions & 6 deletions makesense.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
Expand All @@ -12,6 +11,8 @@ import (
"strings"

svg "github.com/ajstarks/svgo"
"github.com/blevz/makesense/graphutil"
_ "github.com/blevz/makesense/graphutil"
"github.com/goccy/go-graphviz"
"github.com/goccy/go-graphviz/cgraph"
)
Expand Down Expand Up @@ -224,13 +225,11 @@ func (m MakesenseGraph) dumpGraphViz(w io.Writer) {
}
}
}
g.SetLayout(graphviz.Layout(*layoutType))
renderFormat := graphviz.Format(*renderType)
var buf bytes.Buffer
if err := g.Render(graph, renderFormat, &buf); err != nil {
b, err := graphutil.ToSvg(graph, *layoutType, *renderType)
if err != nil {
log.Fatal(err)
}
w.Write(buf.Bytes())
w.Write(b)
}

func (g MakesenseGraph) dumpList(w io.Writer) {
Expand Down
2 changes: 1 addition & 1 deletion makesense.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ all:: .makesense/make.svg
mkdir -p .makesense

.makesense/make.svg: .makesense makesense
make -Bnd | ./makesense --type gv > .makesense/make.svg
make -Bnd | ./bin/makesense --type gv > .makesense/make.svg

.makesense_clean:
rm -rf .makesense
Expand Down