Skip to content

Commit

Permalink
feat: Support cytospace.js for graph visualisation
Browse files Browse the repository at this point in the history
  • Loading branch information
taichimaeda committed Nov 24, 2023
1 parent d0cdb76 commit 3d6e950
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 123 deletions.
43 changes: 9 additions & 34 deletions cmd/wireplus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
"go/types"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -695,13 +693,13 @@ func (cmd *detailCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...inte
}

type graphCmd struct {
tags string
browser bool
tags string
format string
}

func (*graphCmd) Name() string { return "graph" }
func (*graphCmd) Synopsis() string {
return "visualize providers as graph using grpahviz"
return "visualize providers as graph using grpahviz or cytospace"
}
func (*graphCmd) Usage() string {
return `graph [package] [name]
Expand All @@ -711,7 +709,7 @@ func (*graphCmd) Usage() string {
}
func (cmd *graphCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.tags, "tags", "", "append build tags to the default wirebuild")
f.BoolVar(&cmd.browser, "browser", false, "show generated graph in browser")
f.StringVar(&cmd.format, "format", "graphviz", "specify the output format (graphviz or cytospace)")
}
func (cmd *graphCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
wd, err := os.Getwd()
Expand All @@ -725,42 +723,17 @@ func (cmd *graphCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...inter
}
pattern := []string{f.Args()[0]}
name := f.Args()[1]
gviz, errs := wire.Graph(ctx, wd, os.Environ(), pattern, name, cmd.tags)
data, errs := wire.Graph(ctx, wd, os.Environ(), pattern, name, cmd.tags, cmd.format)
if len(errs) > 0 {
logErrors(errs)
log.Println("graph failed")
return subcommands.ExitFailure
}
if cmd.browser {
if err := showGraphInBrowser(gviz); err != nil {
log.Println("failed to show graph in browser: ", err)
return subcommands.ExitFailure
} else {
return subcommands.ExitSuccess
}
}
// Print data to stdout as output
fmt.Println(gviz.String())
// Print the graph data to stdout as output
fmt.Println(data)
return subcommands.ExitSuccess
}

func showGraphInBrowser(gviz *wire.Graphviz) error {
data := gviz.String()
dot := strings.Replace(url.QueryEscape(data), "+", "%20", -1)
// TODO: Make this customisable
url := "https://edotor.net/#" + dot
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
return exec.Command("open", url).Start()
default:
return fmt.Errorf("unsupported platform")
}
}

type lspCmd struct {
tags string
}
Expand Down Expand Up @@ -894,6 +867,8 @@ func (cmd *lspCmd) handleShutdownRequest(req *lsp.ShutdownRequest, resCh chan in
resCh <- res
}

// This is a temporary implementation and this is not supported by the extension client.
// TODO: Fix inconsistent definition jumps
func (cmd *lspCmd) handleDefinitionRequest(ctx context.Context, req *lsp.DefinitionRequest, resCh chan interface{}) {
res := &lsp.DefinitionResponse{
Jsonrpc: "2.0",
Expand Down
Loading

0 comments on commit 3d6e950

Please sign in to comment.