Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Fixes #336
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Sep 19, 2023
1 parent d47c315 commit 6195278
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
68 changes: 34 additions & 34 deletions src/main/java/com/structurizr/dsl/StructurizrDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,40 @@ void parse(List<String> 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());

Expand Down Expand Up @@ -857,40 +891,6 @@ void parse(List<String> 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) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/dsl/script-in-dynamic-view.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
workspace {

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

views {
dynamic * "key" {
!script groovy {
view.description = "Groovy"
}
}
}

}
8 changes: 8 additions & 0 deletions src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 6195278

Please sign in to comment.