DEV Community

Simon Brown
Simon Brown

Posted on

Scripting support added to the Structurizr DSL

The Structurizr DSL now provides a way to run scripts written in Groovy, Kotlin, Ruby, and JavaScript, via the new !script keyword. This gives you access to the underlying Structurizr for Java workspace, for when you need to do something not supported by the DSL.

For example, you could use a script to create the default set of views, without automatic layout enabled:

workspace {

    model {
        user = person "User"
        softwareSystem = softwareSystem "Software System"

        user -> softwareSystem "Uses"
    }

    !script groovy {
        workspace.views.createDefaultViews()
        workspace.views.views.each { it.disableAutomaticLayout() }
    }   

}
Enter fullscreen mode Exit fullscreen mode

You could also use a script to programmatically add elements to a view, using more complicated logic than is possible via the DSL alone.

workspace {

    model {
        group "Group 1" {
            a = softwareSystem "A" {
                tags "Tag 1"
            }
            b = softwareSystem "B" {
                tags "Tag 2"
            }
        }
        group "Group 2" {
            c = softwareSystem "C" {
                tags "Tag 1"
            }
            d = softwareSystem "D" {
                tags "Tag 2"
            }
        }
    }

    views {
        systemLandscape "key" {
            !script groovy {
                view = workspace.views.getViewWithKey("key");
                workspace.model.softwareSystems.findAll { it.group == "Group 1" && it.hasTag("Tag 1") }.each{ view.add(it); };
            }

            autolayout
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Scripting support is available now, via the open source Structurizr CLI and the free Structurizr Lite. See the Structurizr DSL language reference - Scripts and the Structurizr DSL cookbook - Scripts for more details.

Top comments (0)