Skip to content

Commit 2241883

Browse files
authored
feat: add option to embed file system path (#14)
1 parent 7469c94 commit 2241883

File tree

8 files changed

+133
-7
lines changed

8 files changed

+133
-7
lines changed

src/main/java/io/github/algomaster99/maven_module_graph/ModuleGraph.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ private static class Json {
6868
private int indent = 2;
6969
}
7070

71+
@CommandLine.Option(
72+
names = "--fs-path",
73+
description = "Embed file system path in the output."
74+
)
75+
private boolean fsPath = false;
76+
7177
@Override
7278
public Integer call() throws XmlPullParserException, IOException {
7379
validate();
@@ -76,7 +82,7 @@ public Integer call() throws XmlPullParserException, IOException {
7682
printToFile(moduleGraphRoot, plainText.plainText, plainText.indent);
7783
}
7884
if (json != null) {
79-
printToJson(moduleGraphRoot, json.json, json.indent);
85+
printToJson(moduleGraphRoot, json.json, json.indent, fsPath);
8086
}
8187
return 0;
8288
}

src/main/java/io/github/algomaster99/maven_module_graph/Utility.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public static void printToFile(MavenModule root, Path plainText, int indent) {
102102
}
103103
}
104104

105-
public static void printToJson(MavenModule root, Path jsonPath, int indent) {
105+
public static void printToJson(MavenModule root, Path jsonPath, int indent, boolean fsPath) {
106106
ObjectMapper mapper = new ObjectMapper();
107-
ObjectNode rootNode = convertToJson(mapper, root, 0);
107+
ObjectNode rootNode = convertToJson(mapper, root, 0, fsPath);
108108

109109
try {
110110
if (indent > 0) {
@@ -121,19 +121,22 @@ public static void printToJson(MavenModule root, Path jsonPath, int indent) {
121121
}
122122
}
123123

124-
private static ObjectNode convertToJson(ObjectMapper mapper, MavenModule module, int depth) {
124+
private static ObjectNode convertToJson(ObjectMapper mapper, MavenModule module, int depth, boolean fsPath) {
125125
ObjectNode jsonNode = mapper.createObjectNode();
126126
jsonNode.put("depth", depth);
127127
jsonNode.put("groupId", module.getGroupId());
128128
jsonNode.put("artifactId", module.getArtifactId());
129129
jsonNode.put("version", module.getVersion());
130+
if (fsPath) {
131+
jsonNode.put("fsPath", Path.of(System.getProperty("user.dir")).relativize(module.getFileSystemPath()).toString());
132+
}
130133

131134
// Add children with incremented depth
132135
List<MavenModule> submodules = module.getSubmodules();
133136
if (!submodules.isEmpty()) {
134137
ArrayNode childrenArray = jsonNode.putArray("submodules");
135138
for (MavenModule child : submodules) {
136-
childrenArray.add(convertToJson(mapper, child, depth + 1));
139+
childrenArray.add(convertToJson(mapper, child, depth + 1, fsPath));
137140
}
138141
}
139142

src/test/java/io/github/algomaster99/maven_module_graph/MavenModuleTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,42 @@ void bnd_rootVersionIsNull(@TempDir Path temp) throws XmlPullParserException, IO
100100
);
101101
}
102102

103+
@Test
104+
void sampleModule_embedFsPath(@TempDir Path temp) throws XmlPullParserException, IOException {
105+
runTest(
106+
"src/test/resources/sample-module/sample-module",
107+
"src/test/resources/sample-module/output.txt",
108+
"src/test/resources/sample-module/output.json",
109+
temp.resolve("sample-module.txt"),
110+
temp.resolve("sample-module.json"),
111+
2, 2, false, true
112+
);
113+
}
114+
115+
private void runTest(
116+
String modulePath,
117+
String expectedTextPath,
118+
String expectedJsonPath,
119+
Path actualTextPath,
120+
Path actualJsonPath,
121+
int textIndent,
122+
int jsonIndent,
123+
boolean excludeProfiles,
124+
boolean fsPath) throws XmlPullParserException, IOException {
125+
// arrange
126+
MavenModule module = Utility.createMavenModuleGraph(Path.of(modulePath), null, new HashMap<>(), excludeProfiles);
127+
Path expectedPlainText = Path.of(expectedTextPath);
128+
Path expectedJson = Path.of(expectedJsonPath);
129+
130+
// act
131+
Utility.printToFile(module, actualTextPath, textIndent);
132+
Utility.printToJson(module, actualJsonPath, jsonIndent, fsPath);
133+
134+
// assert
135+
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualTextPath)));
136+
assertThat(Files.readString(expectedJson), equalTo(Files.readString(actualJsonPath)));
137+
}
138+
103139
private void runTest(
104140
String modulePath,
105141
String expectedTextPath,
@@ -117,7 +153,7 @@ private void runTest(
117153

118154
// act
119155
Utility.printToFile(module, actualTextPath, textIndent);
120-
Utility.printToJson(module, actualJsonPath, jsonIndent);
156+
Utility.printToJson(module, actualJsonPath, jsonIndent, false);
121157

122158
// assert
123159
assertThat(Files.readString(expectedPlainText), equalTo(Files.readString(actualTextPath)));
@@ -141,7 +177,7 @@ private void runTestWithLineCountCheck(
141177

142178
// act
143179
Utility.printToFile(module, actualTextPath, textIndent);
144-
Utility.printToJson(module, actualJsonPath, jsonIndent);
180+
Utility.printToJson(module, actualJsonPath, jsonIndent, false);
145181

146182
// assert
147183
assertThat(Files.readAllLines(expectedPlainText).size(), equalTo(expectedLineCount));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"depth" : 0,
3+
"groupId" : "org.example",
4+
"artifactId" : "root",
5+
"version" : "1.0.0-SNAPSHOT",
6+
"fsPath" : "src/test/resources/sample-module/sample-module",
7+
"submodules" : [
8+
{
9+
"depth" : 1,
10+
"groupId" : "org.example",
11+
"artifactId" : "a",
12+
"version" : "1.0.0-SNAPSHOT",
13+
"fsPath" : "src/test/resources/sample-module/sample-module/a",
14+
"submodules" : [
15+
{
16+
"depth" : 2,
17+
"groupId" : "org.example",
18+
"artifactId" : "b",
19+
"version" : "1.0.0-SNAPSHOT",
20+
"fsPath" : "src/test/resources/sample-module/sample-module/a/b"
21+
}
22+
]
23+
}
24+
]
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.example:root:1.0.0-SNAPSHOT
2+
org.example:a:1.0.0-SNAPSHOT
3+
org.example:b:1.0.0-SNAPSHOT
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.example</groupId>
6+
<artifactId>a</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<relativePath>../</relativePath>
9+
</parent>
10+
<artifactId>b</artifactId>
11+
<packaging>jar</packaging>
12+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.example</groupId>
6+
<artifactId>root</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<relativePath>../</relativePath>
9+
</parent>
10+
<artifactId>a</artifactId>
11+
<packaging>pom</packaging>
12+
13+
<modules>
14+
<module>b</module>
15+
</modules>
16+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more contributor license
4+
agreements. See the NOTICE file distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file to you under the Apache License,
6+
Version 2.0 (the "License"); you may not use this file except in compliance with the
7+
License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the
10+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
either express or implied. See the License for the specific language governing permissions
12+
and limitations under the License.
13+
-->
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>org.example</groupId>
17+
<artifactId>root</artifactId>
18+
<version>1.0.0-SNAPSHOT</version>
19+
<packaging>pom</packaging>
20+
21+
<modules>
22+
<module>a</module>
23+
</modules>
24+
25+
</project>

0 commit comments

Comments
 (0)