terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
16.74k stars 420 forks source link

feature: Lua? #1599

Open alixander opened 1 year ago

alixander commented 1 year ago

What if we allow embedding Lua for scripting inside D2.

x
y

# this declares shapes 1-10
script: |lua
for i=1,10 do declare(i) end
|
alixander commented 1 year ago

or just use go

mrsaiz commented 1 year ago

I think that embedding scripting in the language is a great idea. In my opinion it really doesn't matter if the language is GO or LUA.

This will help a lot and the code will be cleaner and shorter in some cases. For example, when creating several identical elements we could use a for loop.

bo-ku-ra commented 1 year ago

i am confused. https://github.com/terrastruct/d2/issues/1539#issuecomment-1668900191

bo-ku-ra commented 1 year ago

foo.d2

x -> y
x:{
`perl.exe bar.pl` #execute
}

bar.pl

printf STDOUT "foobar\n";

= foo.d2 (capture STDOUT of execution results)

x->y
x:{
foobar

}
bo-ku-ra commented 1 year ago

more example

foobar
footer:`d2.exe -v` {shape: text; near: bottom-right; style.underline: true}

https://play.d2lang.com/?l=&script=SsvPT0os4krLzy9JLbIqM9Az0zNQqAa7y0oBZKS1Asj5VgpJ-SUl-bm6RaAwsFYAu0APbrSVQklRaWotFxcgAAD__w%3D%3D&

alixander commented 1 year ago

@bo-ku-ra right that original comment I'm saying I'm against adding language features like "if" and "for". Anything that D2 does will fall short of actual scripting. E.g. as soon as we add "for", someone will want "for each", as soon as we add "if", someone will want "switch". We don't want to invent a scripting language, but embedding one is a different story.

I've thought about shell commands as well, but that feels dangerous. Suddenly any D2 script now has a huge security surface area, and would make me hesitant to run arbitrary D2 scripts.

bo-ku-ra commented 1 year ago

i do not think "shell commands are dangerous, lua and go are not."

bo-ku-ra commented 1 year ago

d2 user can choose.

d2.exe foobar.d2 default: execute shell commands = false

d2.exe -x foobar.d2 execute shell commands = true

mrsaiz commented 1 year ago

What if we allow embedding Lua for scripting inside D2.

x
y

# this declares shapes 1-10
script: |lua
for i=1,10 do declare(i) end
|

It will be very interesting if we can use values from d2 vars section in the script, for example:

vars: {
  n=10
}

x
y 

# this declares shapes 1-valueOf(n)
script: |lua
for i=1,${n} do declare(i) end
|
DW-42 commented 7 months ago

I understand that the "best" approach will depend on your specific requirements and the complexity of your diagrams. However, from the D2 developers point of view a more straightforward approach would be to just generate the entire D2 file programmatically using your preferred language such as Lua, PowerShell, or Python. This would allow you to have full control over the file's structure and content, without the effort of trying to engineer the D2 format to accommodate scripts or macros. You would be able to generate complex D2 diagrams the file directly from your data sources or business logic.