From 2b3fe4733334eb860a386bcd727f09d5c67b39cd Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 1 Dec 2022 16:04:04 +0300 Subject: [PATCH] d2: bump to latest master - d2/patches: expose autoformat subcommand - d2/patches: update self referencing connection to latest master --- patches/d2/expose_autoformat_subcommand.patch | 264 ++++++++++++++++++ patches/d2/self_referencing_connections.patch | 8 +- pkgs/d2.nix | 30 +- 3 files changed, 292 insertions(+), 10 deletions(-) create mode 100644 patches/d2/expose_autoformat_subcommand.patch diff --git a/patches/d2/expose_autoformat_subcommand.patch b/patches/d2/expose_autoformat_subcommand.patch new file mode 100644 index 0000000..79a4f49 --- /dev/null +++ b/patches/d2/expose_autoformat_subcommand.patch @@ -0,0 +1,264 @@ +diff --git a/.gitignore b/.gitignore +index 67d0886..7af7b35 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -1,6 +1,7 @@ + .make-log + .changed-files + .make-log.txt + *.got.json + *.got.svg + e2e_report.html ++bin +diff --git a/autofmt.go b/autofmt.go +new file mode 100644 +index 0000000..749c079 +--- /dev/null ++++ b/autofmt.go +@@ -0,0 +1,36 @@ ++package main ++ ++import ( ++ "bytes" ++ "context" ++ ++ "oss.terrastruct.com/xdefer" ++ ++ "oss.terrastruct.com/d2/d2format" ++ "oss.terrastruct.com/d2/d2parser" ++ "oss.terrastruct.com/d2/lib/xmain" ++) ++ ++func autofmt(ctx context.Context, ms *xmain.State) (err error) { ++ defer xdefer.Errorf(&err, "autofmt failed") ++ ++ ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:]) ++ if len(ms.Opts.Args) == 0 { ++ return xmain.UsageErrorf("fmt must be passed the file to be formatted") ++ } else if len(ms.Opts.Args) > 1 { ++ return xmain.UsageErrorf("fmt only accepts one argument for the file to be formatted") ++ } ++ ++ inputPath := ms.Opts.Args[0] ++ input, err := ms.ReadPath(inputPath) ++ if err != nil { ++ return err ++ } ++ ++ m, err := d2parser.Parse(inputPath, bytes.NewReader(input), nil) ++ if err != nil { ++ return err ++ } ++ ++ return ms.WritePath(inputPath, []byte(d2format.Format(m))) ++} +diff --git a/ci/dev.sh b/ci/dev.sh +new file mode 100755 +index 0000000..cbc71bf +--- /dev/null ++++ b/ci/dev.sh +@@ -0,0 +1,7 @@ ++#!/bin/sh ++set -eu ++cd -- "$(dirname "$0")/.." ++. ./ci/sub/lib.sh ++ ++sh_c go build --tags=dev -o=bin/d2 . ++sh_c ./bin/d2 "$@" +diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md +index 25d9930..5fdc99d 100644 +--- a/ci/release/changelogs/next.md ++++ b/ci/release/changelogs/next.md +@@ -1,10 +1,12 @@ + #### Features 🚀 + ++- autoformat is supported on the CLI with the `fmt` subcommand. ++ [#292](https://github.com/terrastruct/d2/pull/292) + - Latex is now supported. See [docs](https://d2lang.com/tour/text) for more. + [#229](https://github.com/terrastruct/d2/pull/229) + - `direction` keyword is now supported to specify `up`, `down`, `right`, `left` layouts. See + [docs](https://d2lang.com/tour/layouts) for more. + [#251](https://github.com/terrastruct/d2/pull/251) + - Arrowhead labels are now supported. [#182](https://github.com/terrastruct/d2/pull/182) + - `stroke-dash` on shapes is now supported. [#188](https://github.com/terrastruct/d2/issues/188) + - `font-color` is now supported on shapes and connections. [#215](https://github.com/terrastruct/d2/pull/215) +diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 +index d46ca20..8347bd8 100644 +--- a/ci/release/template/man/d2.1 ++++ b/ci/release/template/man/d2.1 +@@ -7,16 +7,18 @@ + .Sh SYNOPSIS + .Nm d2 + .Op Fl -watch Ar false + .Op Fl -theme Em 0 + .Ar file.d2 + .Op Ar file.svg | file.png + .Nm d2 + .Ar layout Op Ar name ++.Nm d2 ++.Ar fmt Ar file.d2 + .Sh DESCRIPTION + .Nm + compiles and renders + .Ar file.d2 + to + .Ar file.svg + | + .Ar file.png +@@ -70,13 +72,17 @@ Print usage information and exit. + Print version information and exit. + .El + .Sh SUBCOMMANDS + .Bl -tag -width Fl + .It Ar layout + Lists available layout engine options with short help. + .It Ar layout Op Ar name + Display long help for a particular layout engine. ++.It Ar fmt Ar file.d2 ++Autoformat ++.Ar file.d2 ++.Ns . + .El + .Sh SEE ALSO + .Xr d2plugin-tala 1 + .Sh AUTHORS + Terrastruct Inc. +diff --git a/help.go b/help.go +index 6c1cec8..aff1a72 100644 +--- a/help.go ++++ b/help.go +@@ -10,34 +10,37 @@ import ( + "strings" + + "oss.terrastruct.com/d2/d2plugin" + "oss.terrastruct.com/d2/lib/xmain" + ) + + func help(ms *xmain.State) { + fmt.Fprintf(ms.Stdout, `Usage: +- %s [--watch=false] [--theme=0] file.d2 [file.svg | file.png] ++ %[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png] ++ %[1]s layout [name] ++ %[1]s fmt file.d2 + + %[1]s compiles and renders file.d2 to file.svg | file.png + It defaults to file.svg if an output path is not provided. + + Use - to have d2 read from stdin or write to stdout. + + See man d2 for more detailed docs. + + Flags: + %s + + Subcommands: + %[1]s layout - Lists available layout engine options with short help +- %[1]s layout [layout name] - Display long help for a particular layout engine ++ %[1]s layout [name] - Display long help for a particular layout engine ++ %[1]s fmt file.d2 - Autoformat file.d2 + + See more docs and the source code at https://oss.terrastruct.com/d2 +-`, ms.Name, ms.Opts.Defaults()) ++`, filepath.Base(ms.Name), ms.Opts.Defaults()) + } + + func layoutHelp(ctx context.Context, ms *xmain.State) error { + if len(ms.Opts.Flags.Args()) == 1 { + return shortLayoutHelp(ctx, ms) + } else if len(ms.Opts.Flags.Args()) == 2 { + return longLayoutHelp(ctx, ms) + } else { +diff --git a/lib/xmain/xmain.go b/lib/xmain/xmain.go +index 80de101..b0a1aeb 100644 +--- a/lib/xmain/xmain.go ++++ b/lib/xmain/xmain.go +@@ -61,20 +61,20 @@ func Main(run RunFunc) { + } else if errors.As(err, &uerr) { + msg = err.Error() + usage = true + } else { + msg = err.Error() + } + + if msg != "" { ++ ms.Log.Error.Print(msg) + if usage { +- msg = fmt.Sprintf("%s\n%s", msg, "Run with --help to see usage.") ++ ms.Log.Error.Print("Run with --help to see usage.") + } +- ms.Log.Error.Print(msg) + } + os.Exit(code) + } + } + + type State struct { + Name string + +diff --git a/main.go b/main.go +index 7586d08..bc183e4 100644 +--- a/main.go ++++ b/main.go +@@ -60,28 +60,36 @@ func run(ctx context.Context, ms *xmain.State) (err error) { + return err + } + + err = ms.Opts.Flags.Parse(ms.Opts.Args) + if !errors.Is(err, pflag.ErrHelp) && err != nil { + return xmain.UsageErrorf("failed to parse flags: %v", err) + } + ++ if errors.Is(err, pflag.ErrHelp) { ++ help(ms) ++ return nil ++ } ++ + if len(ms.Opts.Flags.Args()) > 0 { + switch ms.Opts.Flags.Arg(0) { + case "layout": + return layoutHelp(ctx, ms) ++ case "fmt": ++ return autofmt(ctx, ms) ++ case "version": ++ if len(ms.Opts.Flags.Args()) > 1 { ++ return xmain.UsageErrorf("version subcommand accepts no arguments") ++ } ++ fmt.Println(version.Version) ++ return nil + } + } + +- if errors.Is(err, pflag.ErrHelp) { +- help(ms) +- return nil +- } +- + if *debugFlag { + ms.Env.Setenv("DEBUG", "1") + } + + var inputPath string + var outputPath string + + if len(ms.Opts.Flags.Args()) == 0 { +@@ -91,20 +99,16 @@ func run(ctx context.Context, ms *xmain.State) (err error) { + } + help(ms) + return nil + } else if len(ms.Opts.Flags.Args()) >= 3 { + return xmain.UsageErrorf("too many arguments passed") + } + + if len(ms.Opts.Flags.Args()) >= 1 { +- if ms.Opts.Flags.Arg(0) == "version" { +- fmt.Println(version.Version) +- return nil +- } + inputPath = ms.Opts.Flags.Arg(0) + } + if len(ms.Opts.Flags.Args()) >= 2 { + outputPath = ms.Opts.Flags.Arg(1) + } else { + if inputPath == "-" { + outputPath = "-" + } else { diff --git a/patches/d2/self_referencing_connections.patch b/patches/d2/self_referencing_connections.patch index c4b3428..e007baa 100644 --- a/patches/d2/self_referencing_connections.patch +++ b/patches/d2/self_referencing_connections.patch @@ -1,16 +1,16 @@ diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go -index 7bc2ba1..750eb17 100644 +index 5e3f5e5..c2d5511 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go -@@ -710,20 +710,16 @@ func (obj *Object) Connect(srcID, dstID []string, srcArrow, dstArrow bool, label +@@ -721,20 +721,16 @@ func (obj *Object) Connect(srcID, dstID []string, srcArrow, dstArrow bool, label return nil, errors.New("cannot connect to reserved keyword") } } } - + src := srcObj.EnsureChild(srcID) dst := dstObj.EnsureChild(dstID) - + - if src == dst { - return nil, errors.New("self-referencing connection") - } diff --git a/pkgs/d2.nix b/pkgs/d2.nix index 4636f68..6c06c02 100644 --- a/pkgs/d2.nix +++ b/pkgs/d2.nix @@ -4,7 +4,7 @@ , installShellFiles }: -let version = "2022-11-30"; in +let version = "2022-12-01"; in buildGoModule { pname = "d2"; @@ -13,11 +13,11 @@ buildGoModule { src = fetchFromGitHub { owner = "terrastruct"; repo = "d2"; - rev = "e6c7d066164040098d294c00779a2248c402c864"; - sha256 = "sha256-Qlm5i7l4/ZdVulXVOyry3cwZp7bIDbCXUZ5PngpD9bM="; + rev = "264a164cf6ed89e27b2720e600c744ef8ae92ed5"; + sha256 = "sha256-nMaAkOhz4CuVg5PKZUFIQaNEdHU7vouXP+ayul6YdeA="; }; - vendorSha256 = "sha256-yNaocc1iaOHlLqDLql+3XWL9j0RoxBXSWATi7QCNgJI="; + vendorSha256 = "sha256-uOdUQaUuL48ltJl6YTg1a8v8GmacZpW5OtZafXqjQUo="; ldflags = [ "-s" @@ -27,9 +27,27 @@ buildGoModule { nativeBuildInputs = [ installShellFiles ]; - patches = [ ../patches/d2/self_referencing_connections.patch ]; + patches = [ + ../patches/d2/self_referencing_connections.patch + ../patches/d2/expose_autoformat_subcommand.patch + ]; postInstall = "installManPage ci/release/template/man/d2.1"; - subPackages = [ "cmd/d2" ]; + excludedPackages = [ + "d2compiler" + "d2ast" + "d2exporter" + "d2format" + "d2graph" + "d2layouts" + "d2lib" + "d2oracle" + "d2parser" + "d2plugin" + "d2renderers" + "d2target" + "d2theme" + "e2etests" + ]; }