Open alixander opened 1 year ago
or just use go
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.
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
}
more example
foobar
footer:`d2.exe -v` {shape: text; near: bottom-right; style.underline: true}
@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.
i do not think "shell commands are dangerous, lua and go are not."
d2 user can choose.
d2.exe foobar.d2 default: execute shell commands = false
d2.exe -x foobar.d2 execute shell commands = true
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
|
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.
What if we allow embedding Lua for scripting inside D2.