2022-11-29 12:54:30PM

This commit is contained in:
Alexander Wang 2022-11-29 12:54:30 -08:00
parent 38cedb21cb
commit d7fb9c16b8
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 24 additions and 44 deletions

View file

@ -169,7 +169,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
_ = 343 _ = 343
} }
_, err = compile(ctx, ms, plugin, *themeFlag, inputPath, outputPath, pw.Page) _, err = compile(ctx, ms, false, plugin, *themeFlag, inputPath, outputPath, pw.Page)
if err != nil { if err != nil {
return err return err
} }
@ -177,7 +177,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
return nil return nil
} }
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) { func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) {
input, err := ms.ReadPath(inputPath) input, err := ms.ReadPath(inputPath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -205,16 +205,24 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, theme
if err != nil { if err != nil {
return nil, err return nil, err
} }
svg, err = imgbundler.InlineLocal(ms, svg) svg, err = imgbundler.InlineLocal(svg)
if err != nil { if err != nil {
return nil, err // Missing/broken images are fine during watch mode, as the user is likely building up a diagram.
// Otherwise, the assumption is that this diagram is building for production, and broken images are not okay.
if !isWatching {
return nil, err
}
ms.Log.Debug.Printf("ignoring missing/broken local image in watch mode: %v", err)
} }
out := svg out := svg
if filepath.Ext(outputPath) == ".png" { if filepath.Ext(outputPath) == ".png" {
svg, err = imgbundler.InlineRemote(ms, svg) svg, err = imgbundler.InlineRemote(svg)
if err != nil { if err != nil {
return nil, err if !isWatching {
return nil, err
}
ms.Log.Debug.Printf("ignoring missing/broken remote image in watch mode: %v", err)
} }
out, err = png.ConvertSVG(ms, page, svg) out, err = png.ConvertSVG(ms, page, svg)

View file

@ -345,7 +345,7 @@ func (w *watcher) compileLoop(ctx context.Context) error {
w.pw = newPW w.pw = newPW
} }
b, err := compile(ctx, w.ms, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page) b, err := compile(ctx, w.ms, true, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page)
if err != nil { if err != nil {
err = fmt.Errorf("failed to %scompile: %w", recompiledPrefix, err) err = fmt.Errorf("failed to %scompile: %w", recompiledPrefix, err)
w.ms.Log.Error.Print(err) w.ms.Log.Error.Print(err)

View file

@ -16,8 +16,6 @@ import (
"go.uber.org/multierr" "go.uber.org/multierr"
"oss.terrastruct.com/xdefer" "oss.terrastruct.com/xdefer"
"oss.terrastruct.com/d2/lib/xmain"
) )
var imageRe = regexp.MustCompile(`<image href="([^"]+)"`) var imageRe = regexp.MustCompile(`<image href="([^"]+)"`)
@ -28,15 +26,15 @@ type resp struct {
err error err error
} }
func InlineLocal(ms *xmain.State, in []byte) ([]byte, error) { func InlineLocal(in []byte) ([]byte, error) {
return inline(ms, in, false) return inline(in, false)
} }
func InlineRemote(ms *xmain.State, in []byte) ([]byte, error) { func InlineRemote(in []byte) ([]byte, error) {
return inline(ms, in, true) return inline(in, true)
} }
func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) { func inline(svg []byte, isRemote bool) (_ []byte, err error) {
defer xdefer.Errorf(&err, "failed to bundle images") defer xdefer.Errorf(&err, "failed to bundle images")
imgs := imageRe.FindAllSubmatch(svg, -1) imgs := imageRe.FindAllSubmatch(svg, -1)
@ -95,7 +93,7 @@ func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for imgbundler workers: %w", ctx.Err()) return nil, fmt.Errorf("failed waiting for imgbundler workers: %w", ctx.Err())
case resp, ok := <-respChan: case resp, ok := <-respChan:
if !ok { if !ok {
return svg, nil return svg, nil
@ -112,7 +110,7 @@ func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) {
var transport = http.DefaultTransport var transport = http.DefaultTransport
func fetch(ctx context.Context, href string) (string, error) { func fetch(ctx context.Context, href string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel() defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", href, nil) req, err := http.NewRequestWithContext(ctx, "GET", href, nil)

View file

@ -5,15 +5,9 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"oss.terrastruct.com/cmdlog"
"oss.terrastruct.com/xos"
"oss.terrastruct.com/d2/lib/xmain"
) )
//go:embed test_png.png //go:embed test_png.png
@ -90,17 +84,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
return respRecorder.Result() return respRecorder.Result()
}) })
ms := &xmain.State{ out, err := InlineRemote([]byte(sampleSVG))
Name: "test",
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: xos.NewEnv(os.Environ()),
}
ms.Log = cmdlog.Log(ms.Env, os.Stderr)
out, err := InlineRemote(ms, []byte(sampleSVG))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -151,17 +135,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
}]]></style></svg> }]]></style></svg>
`, svgURL, pngURL) `, svgURL, pngURL)
ms := &xmain.State{ out, err := InlineLocal([]byte(sampleSVG))
Name: "test",
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: xos.NewEnv(os.Environ()),
}
ms.Log = cmdlog.Log(ms.Env, os.Stderr)
out, err := InlineLocal(ms, []byte(sampleSVG))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }