This commit is contained in:
Alexander Wang 2022-12-30 13:19:48 -08:00
parent 3f540809ac
commit 85e87c8c2c
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 53 additions and 21 deletions

View file

@ -10,6 +10,7 @@ import (
"os/exec"
"oss.terrastruct.com/util-go/xexec"
"oss.terrastruct.com/util-go/xmain"
"oss.terrastruct.com/d2/d2graph"
)
@ -28,6 +29,15 @@ type PluginSpecificFlag struct {
Tag string
}
func (f *PluginSpecificFlag) AddToOpts(opts *xmain.Opts) {
switch f.Type {
case "string":
opts.String("", f.Name, "", f.Default.(string), f.Usage)
case "int64":
opts.Int64("", f.Name, "", f.Default.(int64), f.Usage)
}
}
type Plugin interface {
// Info returns the current info information of the plugin.
Info(context.Context) (*PluginInfo, error)

View file

@ -5,6 +5,7 @@ package d2plugin
import (
"context"
"encoding/json"
"fmt"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
@ -53,18 +54,27 @@ func (p *dagrePlugin) HydrateOpts(opts []byte) error {
return nil
}
func (p dagrePlugin) Info(context.Context) (*PluginInfo, error) {
func (p dagrePlugin) Info(ctx context.Context) (*PluginInfo, error) {
opts := xmain.NewOpts(nil, nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
return nil, err
}
for _, f := range flags {
f.AddToOpts(opts)
}
return &PluginInfo{
Name: "dagre",
ShortHelp: "The directed graph layout library Dagre",
LongHelp: `dagre is a directed graph layout library for JavaScript.
LongHelp: fmt.Sprintf(`dagre is a directed graph layout library for JavaScript.
See https://github.com/dagrejs/dagre
The implementation of this plugin is at: https://github.com/terrastruct/d2/tree/master/d2plugin/d2dagrelayout
note: dagre is the primary layout algorithm for text to diagram generator Mermaid.js.
See https://github.com/mermaid-js/mermaid
We have a useful comparison at https://text-to-diagram.com/?example=basic&a=d2&b=mermaid
`,
Flags correspond to ones found at https://github.com/dagrejs/dagre/wiki. See dagre's reference for more on each.
Flags:
%s
`, opts.Defaults()),
}, nil
}

View file

@ -5,6 +5,7 @@ package d2plugin
import (
"context"
"encoding/json"
"fmt"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2elklayout"
@ -27,35 +28,35 @@ func (p elkPlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
Name: "elk-algorithm",
Type: "string",
Default: d2elklayout.DefaultOpts.Algorithm,
Usage: "layout algorithm. https://www.eclipse.org/elk/reference/options/org-eclipse-elk-algorithm.html",
Usage: "layout algorithm",
Tag: "elk.algorithm",
},
{
Name: "elk-nodeNodeBetweenLayers",
Type: "int64",
Default: int64(d2elklayout.DefaultOpts.NodeSpacing),
Usage: "the spacing to be preserved between any pair of nodes of two adjacent layers. https://www.eclipse.org/elk/reference/options/org-eclipse-elk-layered-spacing-nodeNodeBetweenLayers.html",
Usage: "the spacing to be preserved between any pair of nodes of two adjacent layers",
Tag: "spacing.nodeNodeBetweenLayers",
},
{
Name: "elk-padding",
Type: "string",
Default: d2elklayout.DefaultOpts.Padding,
Usage: "the padding to be left to a parent elements border when placing child elements. https://www.eclipse.org/elk/reference/options/org-eclipse-elk-padding.html",
Usage: "the padding to be left to a parent elements border when placing child elements",
Tag: "elk.padding",
},
{
Name: "elk-edgeNodeBetweenLayers",
Type: "int64",
Default: int64(d2elklayout.DefaultOpts.EdgeNodeSpacing),
Usage: "the spacing to be preserved between nodes and edges that are routed next to the nodes layer. https://www.eclipse.org/elk/reference/options/org-eclipse-elk-layered-spacing-edgeNodeBetweenLayers.html",
Usage: "the spacing to be preserved between nodes and edges that are routed next to the nodes layer",
Tag: "spacing.edgeNodeBetweenLayers",
},
{
Name: "elk-nodeSelfLoop",
Type: "int64",
Default: int64(d2elklayout.DefaultOpts.SelfLoopSpacing),
Usage: "spacing to be preserved between a node and its self loops. https://www.eclipse.org/elk/reference/options/org-eclipse-elk-spacing-nodeSelfLoop.html",
Usage: "spacing to be preserved between a node and its self loops",
Tag: "elk.spacing.nodeSelfLoop",
},
}, nil
@ -75,13 +76,27 @@ func (p *elkPlugin) HydrateOpts(opts []byte) error {
return nil
}
func (p elkPlugin) Info(context.Context) (*PluginInfo, error) {
func (p elkPlugin) Info(ctx context.Context) (*PluginInfo, error) {
opts := xmain.NewOpts(nil, nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
return nil, err
}
for _, f := range flags {
f.AddToOpts(opts)
}
return &PluginInfo{
Name: "elk",
ShortHelp: "Eclipse Layout Kernel (ELK) with the Layered algorithm.",
LongHelp: `ELK is a layout engine offered by Eclipse.
LongHelp: fmt.Sprintf(`ELK is a layout engine offered by Eclipse.
Originally written in Java, it has been ported to Javascript and cross-compiled into D2.
See https://github.com/kieler/elkjs for more.`,
See https://github.com/kieler/elkjs for more.
Flags correspond to ones found in https://www.eclipse.org/elk/reference.html. See ELK's reference for more on each.
Flags:
%s
`, opts.Defaults()),
}, nil
}

View file

@ -307,12 +307,9 @@ func populateLayoutOpts(ctx context.Context, ms *xmain.State) error {
}
for _, f := range pluginFlags {
switch f.Type {
case "string":
ms.Opts.String("", f.Name, "", f.Default.(string), f.Usage)
case "int64":
ms.Opts.Int64("", f.Name, "", f.Default.(int64), f.Usage)
}
f.AddToOpts(ms.Opts)
// Don't pollute the main d2 flagset with these. It'll be a lot
ms.Opts.Flags.MarkHidden(f.Name)
}
return nil