Skip to content

Commit 580b367

Browse files
fix: resolve imports in CLI by using CMLImportResolver and absolute file paths
The CLI project now properly handles imports in CML files by: 1. Using CMLImportResolver to explicitly resolve imports 2. Using absolute file paths to ensure correct URI resolution 3. Updating test files to match DSL project's import syntax This aligns the CLI's import handling with the DSL project's implementation.
1 parent d1b017a commit 580b367

5 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/main/java/org/contextmapper/cli/commands/GenerateCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.contextmapper.dsl.standalone.ContextMapperStandaloneSetup;
2222
import org.contextmapper.dsl.standalone.StandaloneContextMapperAPI;
2323
import org.eclipse.xtext.generator.IGenerator2;
24+
import org.contextmapper.dsl.cml.CMLImportResolver;
2425

2526
import java.io.File;
2627
import java.util.Arrays;
@@ -51,7 +52,12 @@ public void run(String[] args) {
5152

5253
if (doesOutputDirExist(this.outputDir)) {
5354
StandaloneContextMapperAPI cmAPI = ContextMapperStandaloneSetup.getStandaloneAPI();
54-
CMLResource cmlResource = cmAPI.loadCML(inputPath);
55+
File inputFile = new File(inputPath).getAbsoluteFile();
56+
CMLResource cmlResource = cmAPI.loadCML(inputFile);
57+
58+
// Ensure imports are resolved before generation
59+
new CMLImportResolver().resolveImportedResources(cmlResource);
60+
5561
cmAPI.callGenerator(cmlResource, getGenerator(cmd), this.outputDir);
5662
System.out.println("Generated into '" + this.outputDir + "'.");
5763
}

src/test/java/org/contextmapper/cli/commands/GenerateCommandTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ void run_WhenCalledWithContextMapParam_ThenGenerateContextMapFiles() {
137137
assertThat(new File("build/test-out/test_ContextMap.svg").exists()).isTrue();
138138
}
139139

140+
@Test
141+
void run_WhenCalledWithFileContainingImports_ThenGenerateFilesWithImportedContexts() throws IOException {
142+
// given
143+
final GenerateCommand command = spy(new GenerateCommand());
144+
new File("build/test-out").mkdir();
145+
146+
// when
147+
command.run(new String[]{"-i", "src/test/resources/test-with-imports.cml", "-g", "context-map", "-o", "build/test-out"});
148+
149+
// then
150+
assertThat(outContent.toString()).contains("Generated into 'build/test-out'.");
151+
assertThat(new File("build/test-out/test-with-imports_ContextMap.gv").exists()).isTrue();
152+
assertThat(new File("build/test-out/test-with-imports_ContextMap.png").exists()).isTrue();
153+
assertThat(new File("build/test-out/test-with-imports_ContextMap.svg").exists()).isTrue();
154+
}
155+
140156
@Test
141157
void run_WhenCalledWithGenericParam_ThenGenerateGenericOutput() {
142158
// given

src/test/java/org/contextmapper/cli/commands/ValidateCommandTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,17 @@ void run_WhenWithInvalidCMLFile_ThenPrintError() {
8888
assertThat(outContent.toString()).contains("ERROR in null on line 2:mismatched input '<EOF>' expecting RULE_CLOSE");
8989
}
9090

91+
@Test
92+
void run_WhenWithFileContainingImports_ThenValidateWithoutErrors() {
93+
// given
94+
final ValidateCommand command = spy(new ValidateCommand());
95+
96+
// when
97+
command.run(new String[]{"-i src/test/resources/test-with-imports.cml"});
98+
99+
// then
100+
verify(command).printValidationMessages(any(), any());
101+
assertThat(outContent.toString()).contains("The CML file 'src/test/resources/test-with-imports.cml' has been validated without errors.");
102+
}
103+
91104
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* Simple bounded contexts */
2+
BoundedContext anotherContext
3+
BoundedContext yetAnotherContext
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Import other contexts */
2+
import "./other-contexts.cml"
3+
4+
/* Define a simple context map */
5+
ContextMap SimpleMap {
6+
contains anotherContext
7+
contains yetAnotherContext
8+
}

0 commit comments

Comments
 (0)