d2cli: Accept abspaths again

Fixes https://github.com/Homebrew/homebrew-core/pull/124831/files#r1125738853
This commit is contained in:
Anmol Sethi 2023-03-05 13:45:20 -08:00
parent d028cda9f0
commit 7cd821feb7
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
7 changed files with 46 additions and 10 deletions

View file

@ -3,3 +3,5 @@
#### Improvements 🧹
#### Bugfixes ⛑️
- Accept absolute paths again on the CLI. [#979](https://github.com/terrastruct/d2/pull/979)

View file

@ -154,14 +154,14 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
}
}
if inputPath != "-" {
inputPath = filepath.Join(ms.PWD, inputPath)
inputPath = ms.AbsPath(inputPath)
d, err := os.Stat(inputPath)
if err == nil && d.IsDir() {
inputPath = filepath.Join(inputPath, "index.d2")
}
}
if outputPath != "-" {
outputPath = filepath.Join(ms.PWD, outputPath)
outputPath = ms.AbsPath(outputPath)
}
match := d2themescatalog.Find(*themeFlag)
@ -308,7 +308,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
if filepath.Ext(outputPath) == ".pdf" {
dur := time.Since(start)
ms.Log.Success.Printf("successfully compiled %s to %s in %s", inputPath, outputPath, dur)
ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(outputPath), dur)
}
return svg, true, nil
@ -383,7 +383,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
return svg, err
}
dur := compileDur + time.Since(start)
ms.Log.Success.Printf("successfully compiled %s to %s in %s", inputPath, boardOutputPath, dur)
ms.Log.Success.Printf("successfully compiled %s to %s in %s", ms.HumanPath(inputPath), ms.HumanPath(boardOutputPath), dur)
return svg, nil
}

View file

@ -48,6 +48,7 @@ type watcherOpts struct {
port string
inputPath string
outputPath string
pwd string
bundle bool
forceAppendix bool
pw png.Playwright
@ -216,7 +217,7 @@ func (w *watcher) watchLoop(ctx context.Context) error {
if err != nil {
return err
}
w.ms.Log.Info.Printf("compiling %v...", w.inputPath)
w.ms.Log.Info.Printf("compiling %v...", w.ms.HumanPath(w.inputPath))
w.requestCompile()
eatBurstTimer := time.NewTimer(0)
@ -270,7 +271,7 @@ func (w *watcher) watchLoop(ctx context.Context) error {
// misleading error.
eatBurstTimer.Reset(time.Millisecond * 16)
case <-eatBurstTimer.C:
w.ms.Log.Info.Printf("detected change in %v: recompiling...", w.inputPath)
w.ms.Log.Info.Printf("detected change in %v: recompiling...", w.ms.HumanPath(w.inputPath))
w.requestCompile()
case err, ok := <-w.fw.Errors:
if !ok {
@ -300,7 +301,7 @@ func (w *watcher) ensureAddWatch(ctx context.Context) (time.Time, error) {
return mt, nil
}
if interval >= time.Second {
w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.inputPath, err, interval)
w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.ms.HumanPath(w.inputPath), err, interval)
}
tc.Reset(interval)

View file

@ -161,6 +161,16 @@ layers: {
assert.Testdata(t, ".svg", stdout.Bytes())
},
},
{
name: "abspath",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `x -> y`)
err := runTestMain(t, ctx, dir, env, filepath.Join(dir, "hello-world.d2"))
assert.Success(t, err)
svg := readFile(t, dir, "hello-world.svg")
assert.Testdata(t, ".svg", svg)
},
},
}
ctx := context.Background()

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 328 KiB

2
go.mod generated
View file

@ -23,7 +23,7 @@ require (
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
gonum.org/v1/plot v0.12.0
nhooyr.io/websocket v1.8.7
oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb
oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221
)
require (

4
go.sum generated
View file

@ -283,6 +283,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb h1:Zgb7Wf7mTpXmIiYv0bIJ8IULcdJVIxiRihnUvDP9YMQ=
oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221 h1:LbO79j1CUB/mkmPSzLrA/EL4TRK0rjfAq+CejQapHyw=
oss.terrastruct.com/util-go v0.0.0-20230305220024-ae08a694f221/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=