Did you ever wish you had better Diagrams?
Let’s recap what we did so far: in order to kickstart a documentation project, we’ve created a dual gradle / maven asciidoc build with included arc42 template in two languages and easy textual diagram drawing with planUML. That’s already quite a feature list!
But what if plantUML diagrams are not enough for your needs? What if you can’t rely on the auto-layout feature of plantUML?
One solution would be to just start up your favourite diagramming tool, draw your diagrams, export them and reference them from asciidoc. No problem. But what if you need to update one of the diagrams? Or you just worked the whole day on some diagrams and you don’t know anymore which ones to re-export?
Wouldn’t it be nice if your build just takes care of it?
It could just search for your diagrams files and export them.
That’s exactly what we will add today. Since there are two many diagramming tools on the market to include them all in one post, I will stick to Sparx Enterprise Architect. Enterprise Architect - or short jsut EA - is a powerful but yet affordable tool which can be easily scripted.
There are several options for scripting like internal scripts written in JavaScript, JScript and VBScript. These scripts have to be invoked manually from within the application. Another option is to use the automation interface and “remote control” the application from outside. This can be done in various languages (all which have access to the ActiveX COM interface). There is a java bridge included and VBS would be the native approach.
In the past I already played around with the included java interface and had the feeling that it is a little bit incomplete or buggy. That’s why I soon switched to VisualBasic as scripting for EA - this avoids an additional layer between me and the application.
Features
The script I want to include into our build will have several tasks:
- find all EA repositories within the project
- open the found repositories through EA
- export all diagrams and save them with their given name as file name
- as bonus, check all element notes and export those which start with {adoc: filename}
I’ve chose to use the given name of a diagram for the file name because I think it is easier to handle and speaks for itself. Drawback is that you have to make sure that the name is a valid file name and it is unique. Another aproach would be to use the quite cryptic object ID (GUID)
What’s up with the bonus tasks? Working with EA, I like to directly write my comments and descriptions in the note fields of the diagram elements. By prefixing them with {adoc: filename}, I can decide to export and easily include them into my documentation. This way, the written documentation for a diagram can be directly maintained within EA.
Code
The following is the VB-Script code which does all the hard work:
add it to the Build
Gradle uses Groovy and Groovy is able to easily execute shell scripts. But the standard execution mechanism has some drawbacks (regarding getting the output/error messages from the shell script and also regarding parameters). So I wrote my own streaming execute code which I add through meta programming to the string class. The code which adds the method has also to be executed, so I wrote a special task for this:
Windows? Linux?
This code fragment currently only works for Windows, but I guess if you use EA, you are not running Linux or MacOS, so I guess this will be fine. If you use Ea on Linux or MaxOS, please contact me - it would be great to make this work on Linux!
Now you might think, that if this only runs on Windows, I can’t use it on a build server. Take a closer look at the VBScript above. I’ve configured it in such a way that it exports everything to /src/docs/es
and not to the /build
folder. This is because I normally export the EA content on my dev machine and check in the results. This makes it even easier to get a diff of the work you’ve done in EA. The remaining build process can rely on the exported data and thus even runs on Linux.
final task
In order to use the streaming execute and run the VBScript, I just added another small task to the Gradle build file:
So, to export all diagrams and notes from EA, you simply run gradle exportEA
in the shell: (I’ve included a small sample EA file)
> gradle exportEA
:streamingExecute
:exportEA
Image extractor
looking for .eap files in .\docToolchain/src
found .\src\docs\Models.eap
opening Enterprise Architect. This might take a moment...
extracted image to ./src/docs/images/ea/Use Cases.png
finished exporting images
exported notes .\src\docs\ea\UseCases.ad
BUILD SUCCESSFUL
Total time: 14.228 secs
Give it a Try…
So, if you want to test it, you can use the sample EA file included in the docToolchain project and add a small asciidoc file like the following:
image::ea/Use{sp}Cases.png[width=25%]
include::ea/UseCases.ad[]
Notice the {sp}
in the filename? This is in asciidoc the workaround for spaces in a filename.
The EA sample diagram looks like this:
and our build turns renders it like this:
Conclusion
Today we extended our build with the feature to extract Diagrams and Notes from the Sparxsystems Enterprise Architect UML Modeller. This feature is quite helpful when you have to deal with Diagrams where plantUML is not enough but it does not replace plantUML. PlantUML is still quite useful when you need a simple diagram or a diagram - like a sequence diagram - where the auto-layout of plantUML is quite useful.
The updated docTool project can be found here: https://github.com/rdmueller/docToolchain