From 61952781a699a3e5ae5567130e9ff310cfa16305 Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Tue, 19 Sep 2023 17:10:47 +0100 Subject: [PATCH] Fixes https://github.com/structurizr/dsl/issues/336 --- docs/changelog.md | 1 + .../structurizr/dsl/StructurizrDslParser.java | 68 +++++++++---------- src/test/dsl/script-in-dynamic-view.dsl | 16 +++++ .../java/com/structurizr/dsl/DslTests.java | 8 +++ 4 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 src/test/dsl/script-in-dynamic-view.dsl diff --git a/docs/changelog.md b/docs/changelog.md index 534ae89..26655cb 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ - Adds a `context` variable to inline/external scripts (see https://github.com/structurizr/dsl/issues/332). - Fixes https://github.com/structurizr/dsl/issues/324 (Groups with no curly braces breaks diagrams). - Adds a way to set the character encoding used by the DSL parser (see https://github.com/structurizr/dsl/issues/338). +- Fixes https://github.com/structurizr/dsl/issues/336 (Dynamic View does not allow !script tag). ## 1.32.0 (28th July 2023) diff --git a/src/main/java/com/structurizr/dsl/StructurizrDslParser.java b/src/main/java/com/structurizr/dsl/StructurizrDslParser.java index ea678c9..874e6d7 100644 --- a/src/main/java/com/structurizr/dsl/StructurizrDslParser.java +++ b/src/main/java/com/structurizr/dsl/StructurizrDslParser.java @@ -271,6 +271,40 @@ void parse(List lines, File dslFile) throws StructurizrDslParserExceptio includeInDslSourceLines = false; } + } else if (PLUGIN_TOKEN.equalsIgnoreCase(firstToken)) { + if (!restricted) { + String fullyQualifiedClassName = new PluginParser().parse(getContext(), tokens.withoutContextStartToken()); + startContext(new PluginDslContext(fullyQualifiedClassName, dslFile)); + if (!shouldStartContext(tokens)) { + // run the plugin immediately, without looking for parameters + endContext(); + } + } + + } else if (inContext(PluginDslContext.class)) { + new PluginParser().parseParameter(getContext(PluginDslContext.class), tokens); + + } else if (SCRIPT_TOKEN.equalsIgnoreCase(firstToken)) { + if (!restricted) { + ScriptParser scriptParser = new ScriptParser(); + if (scriptParser.isInlineScript(tokens)) { + String language = scriptParser.parseInline(tokens.withoutContextStartToken()); + startContext(new InlineScriptDslContext(getContext(), dslFile, language)); + } else { + String filename = scriptParser.parseExternal(tokens.withoutContextStartToken()); + startContext(new ExternalScriptDslContext(getContext(), dslFile, filename)); + + if (shouldStartContext(tokens)) { + // we'll wait for parameters before executing the script + } else { + endContext(); + } + } + } + + } else if (inContext(ExternalScriptDslContext.class)) { + new ScriptParser().parseParameter(getContext(ExternalScriptDslContext.class), tokens); + } else if (tokens.size() > 2 && RELATIONSHIP_TOKEN.equals(tokens.get(1)) && (inContext(ModelDslContext.class) || inContext(EnterpriseDslContext.class) || inContext(CustomElementDslContext.class) || inContext(PersonDslContext.class) || inContext(SoftwareSystemDslContext.class) || inContext(ContainerDslContext.class) || inContext(ComponentDslContext.class) || inContext(DeploymentEnvironmentDslContext.class) || inContext(DeploymentNodeDslContext.class) || inContext(InfrastructureNodeDslContext.class) || inContext(SoftwareSystemInstanceDslContext.class) || inContext(ContainerInstanceDslContext.class))) { Relationship relationship = new ExplicitRelationshipParser().parse(getContext(), tokens.withoutContextStartToken()); @@ -857,40 +891,6 @@ void parse(List lines, File dslFile) throws StructurizrDslParserExceptio } else if (IDENTIFIERS_TOKEN.equalsIgnoreCase(firstToken) && inContext(WorkspaceDslContext.class)) { setIdentifierScope(new IdentifierScopeParser().parse(getContext(), tokens)); - } else if (PLUGIN_TOKEN.equalsIgnoreCase(firstToken)) { - if (!restricted) { - String fullyQualifiedClassName = new PluginParser().parse(getContext(), tokens.withoutContextStartToken()); - startContext(new PluginDslContext(fullyQualifiedClassName, dslFile)); - if (!shouldStartContext(tokens)) { - // run the plugin immediately, without looking for parameters - endContext(); - } - } - - } else if (inContext(PluginDslContext.class)) { - new PluginParser().parseParameter(getContext(PluginDslContext.class), tokens); - - } else if (SCRIPT_TOKEN.equalsIgnoreCase(firstToken)) { - if (!restricted) { - ScriptParser scriptParser = new ScriptParser(); - if (scriptParser.isInlineScript(tokens)) { - String language = scriptParser.parseInline(tokens.withoutContextStartToken()); - startContext(new InlineScriptDslContext(getContext(), dslFile, language)); - } else { - String filename = scriptParser.parseExternal(tokens.withoutContextStartToken()); - startContext(new ExternalScriptDslContext(getContext(), dslFile, filename)); - - if (shouldStartContext(tokens)) { - // we'll wait for parameters before executing the script - } else { - endContext(); - } - } - } - - } else if (inContext(ExternalScriptDslContext.class)) { - new ScriptParser().parseParameter(getContext(ExternalScriptDslContext.class), tokens); - } else { String[] expectedTokens; if (getContext() == null) { diff --git a/src/test/dsl/script-in-dynamic-view.dsl b/src/test/dsl/script-in-dynamic-view.dsl new file mode 100644 index 0000000..f29f4e9 --- /dev/null +++ b/src/test/dsl/script-in-dynamic-view.dsl @@ -0,0 +1,16 @@ +workspace { + + model { + user = person "User" + softwareSystem = softwareSystem "Software System" + } + + views { + dynamic * "key" { + !script groovy { + view.description = "Groovy" + } + } + } + +} \ No newline at end of file diff --git a/src/test/java/com/structurizr/dsl/DslTests.java b/src/test/java/com/structurizr/dsl/DslTests.java index 321db56..1c11091 100644 --- a/src/test/java/com/structurizr/dsl/DslTests.java +++ b/src/test/java/com/structurizr/dsl/DslTests.java @@ -1038,4 +1038,12 @@ void test_ISO8859Encoding() throws Exception { assertNotNull(parser.getWorkspace().getModel().getSoftwareSystemWithName("Namé")); } + @Test + void test_ScriptInDynamicView() throws Exception { + File dslFile = new File("src/test/dsl/script-in-dynamic-view.dsl"); + + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(dslFile); + } + } \ No newline at end of file