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

relocate plugins to internal #1753

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ lint: fmt
prefix "$@" go vet --composites=false ./...
.PHONY: build
build: fmt
prefix "$@" go build ./...
prefix "$@" go build -tags plugins_embed,plugins_embed_dagre,plugins_embed_elk ./...
.PHONY: test
test: fmt
prefix "$@" ./ci/test.sh
.PHONY: race
race: fmt
prefix "$@" ./ci/test.sh --race ./...

.PHONY: build-noembed
prefix "$@" go build ./...
2 changes: 1 addition & 1 deletion ci/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -eu
cd -- "$(dirname "$0")/.."
. ./ci/sub/lib.sh

sh_c go build --tags=dev -o=bin/d2 .
sh_c go build --tags=dev,plugins_embed,plugins_embed_dagre,plugins_embed_elk -o=bin/d2 .
sh_c ./bin/d2 "$@"
9 changes: 5 additions & 4 deletions ci/release/_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ ensure_goos
ensure_goarch
sh_c mkdir -p "$HW_BUILD_DIR/bin"
sh_c GOOS="$GOOS" GOARCH="$GOARCH" CGO_ENABLED=0 go build -trimpath \
-ldflags "'-X oss.terrastruct.com/d2/lib/version.Version=$VERSION'" \
-o "$HW_BUILD_DIR/bin/d2" .
-tags plugins_embed,plugins_embed_dagre,plugins_embed_elk \
-ldflags "'-X oss.terrastruct.com/d2/lib/version.Version=$VERSION'" \
-o "$HW_BUILD_DIR/bin/d2" .

if [ "$GOOS" = windows ]; then
sh_c mv "$HW_BUILD_DIR/bin/d2" "$HW_BUILD_DIR/bin/d2.exe"
sh_c mv "$HW_BUILD_DIR/bin/d2" "$HW_BUILD_DIR/bin/d2.exe"
fi

ARCHIVE=$PWD/$ARCHIVE
cd "$(dirname "$HW_BUILD_DIR")"
sh_c tar -czf "$ARCHIVE" "$(basename "$HW_BUILD_DIR")"
cd ->/dev/null
cd - >/dev/null
4 changes: 2 additions & 2 deletions ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -eu
cd "$(dirname "$0")/.."

if [ "$*" = "" ]; then
set ./...
set ./...
fi

go test --timeout=30m "$@"
go test -tags plugins_embed,plugins_embed_dagre,plugins_embed_elk --timeout=30m "$@"
5 changes: 3 additions & 2 deletions cmd/d2plugin-dagre/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//go:build !nodagre
//go:build !plugins_embed

package main

import (
"oss.terrastruct.com/util-go/xmain"

"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/d2/internal/embeddedplugin/dagre"
)

func main() {
xmain.Main(d2plugin.Serve(&d2plugin.DagrePlugin))
xmain.Main(d2plugin.Serve(&dagre.DagrePlugin{}))
}
12 changes: 12 additions & 0 deletions d2cli/embed_plugin_dagre.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build plugins_embed && plugins_embed_dagre

package d2cli

import (
"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/d2/internal/embeddedplugin/dagre"
)

func init() {
d2plugin.RegisterEmbeddedPlugin(&dagre.DagrePlugin{})
}
12 changes: 12 additions & 0 deletions d2cli/embed_plugin_elk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build plugins_embed && plugins_embed_elk

package d2cli

import (
"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/d2/internal/embeddedplugin/elk"
)

func init() {
d2plugin.RegisterEmbeddedPlugin(&elk.ELKPlugin{})
}
14 changes: 8 additions & 6 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = os.MkdirAll(filepath.Dir(outputPath), 0755)
err = os.MkdirAll(filepath.Dir(outputPath), 0o755)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = os.MkdirAll(filepath.Dir(outputPath), 0755)
err = os.MkdirAll(filepath.Dir(outputPath), 0o755)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -899,7 +899,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
}

if opts.MasterID == "" {
err = os.MkdirAll(filepath.Dir(outputPath), 0755)
err = os.MkdirAll(filepath.Dir(outputPath), 0o755)
if err != nil {
return svg, err
}
Expand Down Expand Up @@ -1245,9 +1245,11 @@ func loadFonts(ms *xmain.State, pathToRegular, pathToItalic, pathToBold, pathToS
return d2fonts.AddFontFamily("custom", regularTTF, italicTTF, boldTTF, semiboldTTF)
}

const LAYERS = "layers"
const STEPS = "steps"
const SCENARIOS = "scenarios"
const (
LAYERS = "layers"
STEPS = "steps"
SCENARIOS = "scenarios"
)

// buildBoardIDToIndex returns a map from board path to page int
// To map correctly, it must follow the same traversal of pdf/pptx building
Expand Down
7 changes: 7 additions & 0 deletions d2plugin/embedded_plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build plugins_embed

package d2plugin

func RegisterEmbeddedPlugin(p Plugin) {
plugins = append(plugins, p)
}
10 changes: 6 additions & 4 deletions e2etests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"oss.terrastruct.com/d2/d2renderers/d2animate"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/internal/embeddedplugin/dagre"
"oss.terrastruct.com/d2/internal/embeddedplugin/elk"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)
Expand Down Expand Up @@ -174,13 +176,13 @@ func run(t *testing.T, tc testCase) {
for _, layoutName := range layoutsTested {
var plugin d2plugin.Plugin
if layoutName == "dagre" {
plugin = &d2plugin.DagrePlugin
plugin = &dagre.DagrePlugin{}
} else if layoutName == "elk" {
// If measured texts exists, we are specifically exercising text measurements, no need to run on both layouts
if tc.mtexts != nil {
continue
}
plugin = &d2plugin.ELKPlugin
plugin = &elk.ELKPlugin{}
}

compileOpts := &d2lib.CompileOptions{
Expand Down Expand Up @@ -250,9 +252,9 @@ func run(t *testing.T, tc testCase) {
assert.Success(t, err)
}

err = os.MkdirAll(dataPath, 0755)
err = os.MkdirAll(dataPath, 0o755)
assert.Success(t, err)
err = os.WriteFile(pathGotSVG, svgBytes, 0600)
err = os.WriteFile(pathGotSVG, svgBytes, 0o600)
assert.Success(t, err)

// Check that it's valid SVG
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//go:build !nodagre

package d2plugin
package dagre

import (
"context"
Expand All @@ -10,22 +8,17 @@ import (

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/util-go/xmain"
)

var DagrePlugin = dagrePlugin{}

func init() {
plugins = append(plugins, &DagrePlugin)
}

type dagrePlugin struct {
type DagrePlugin struct {
mu sync.Mutex
opts *d2dagrelayout.ConfigurableOpts
}

func (p *dagrePlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
return []PluginSpecificFlag{
func (p *DagrePlugin) Flags(context.Context) ([]d2plugin.PluginSpecificFlag, error) {
return []d2plugin.PluginSpecificFlag{
{
Name: "dagre-nodesep",
Type: "int64",
Expand All @@ -43,7 +36,7 @@ func (p *dagrePlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
}, nil
}

func (p *dagrePlugin) HydrateOpts(opts []byte) error {
func (p *DagrePlugin) HydrateOpts(opts []byte) error {
p.mu.Lock()
defer p.mu.Unlock()
if opts != nil {
Expand All @@ -58,7 +51,7 @@ func (p *dagrePlugin) HydrateOpts(opts []byte) error {
return nil
}

func (p *dagrePlugin) Info(ctx context.Context) (*PluginInfo, error) {
func (p *DagrePlugin) Info(ctx context.Context) (*d2plugin.PluginInfo, error) {
p.mu.Lock()
defer p.mu.Unlock()
opts := xmain.NewOpts(nil, nil)
Expand All @@ -70,10 +63,10 @@ func (p *dagrePlugin) Info(ctx context.Context) (*PluginInfo, error) {
f.AddToOpts(opts)
}

return &PluginInfo{
return &d2plugin.PluginInfo{
Name: "dagre",
Type: "bundled",
Features: []PluginFeature{},
Features: []d2plugin.PluginFeature{},
ShortHelp: "The directed graph layout library Dagre",
LongHelp: fmt.Sprintf(`dagre is a directed graph layout library for JavaScript.
See https://d2lang.com/tour/dagre for more.
Expand All @@ -86,13 +79,13 @@ Flags:
}, nil
}

func (p *dagrePlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
func (p *DagrePlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
p.mu.Lock()
optsCopy := *p.opts
p.mu.Unlock()
return d2dagrelayout.Layout(ctx, g, &optsCopy)
}

func (p *dagrePlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
func (p *DagrePlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
return in, nil
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//go:build !noelk

package d2plugin
package elk

import (
"context"
Expand All @@ -9,21 +7,16 @@ import (

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2elklayout"
"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/util-go/xmain"
)

var ELKPlugin = elkPlugin{}

func init() {
plugins = append(plugins, &ELKPlugin)
}

type elkPlugin struct {
type ELKPlugin struct {
opts *d2elklayout.ConfigurableOpts
}

func (p elkPlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
return []PluginSpecificFlag{
func (p ELKPlugin) Flags(context.Context) ([]d2plugin.PluginSpecificFlag, error) {
return []d2plugin.PluginSpecificFlag{
{
Name: "elk-algorithm",
Type: "string",
Expand Down Expand Up @@ -62,7 +55,7 @@ func (p elkPlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
}, nil
}

func (p *elkPlugin) HydrateOpts(opts []byte) error {
func (p *ELKPlugin) HydrateOpts(opts []byte) error {
if opts != nil {
var elkOpts d2elklayout.ConfigurableOpts
err := json.Unmarshal(opts, &elkOpts)
Expand All @@ -75,7 +68,7 @@ func (p *elkPlugin) HydrateOpts(opts []byte) error {
return nil
}

func (p elkPlugin) Info(ctx context.Context) (*PluginInfo, error) {
func (p ELKPlugin) Info(ctx context.Context) (*d2plugin.PluginInfo, error) {
opts := xmain.NewOpts(nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
Expand All @@ -84,12 +77,12 @@ func (p elkPlugin) Info(ctx context.Context) (*PluginInfo, error) {
for _, f := range flags {
f.AddToOpts(opts)
}
return &PluginInfo{
return &d2plugin.PluginInfo{
Name: "elk",
Type: "bundled",
Features: []PluginFeature{
CONTAINER_DIMENSIONS,
DESCENDANT_EDGES,
Features: []d2plugin.PluginFeature{
d2plugin.CONTAINER_DIMENSIONS,
d2plugin.DESCENDANT_EDGES,
},
ShortHelp: "Eclipse Layout Kernel (ELK) with the Layered algorithm.",
LongHelp: fmt.Sprintf(`ELK is a layout engine offered by Eclipse.
Expand All @@ -104,10 +97,10 @@ Flags:
}, nil
}

func (p elkPlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
func (p ELKPlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
return d2elklayout.Layout(ctx, g, p.opts)
}

func (p elkPlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
func (p ELKPlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
return in, nil
}
Loading