|
1 | 1 | package io.github.algomaster99.maven_module_graph; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.core.util.DefaultIndenter; |
4 | | -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
5 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
6 | | -import com.fasterxml.jackson.databind.node.ArrayNode; |
7 | | -import com.fasterxml.jackson.databind.node.ObjectNode; |
8 | | - |
| 3 | +import io.github.algomaster99.maven_module_graph.visitor.JsonVisitor; |
| 4 | +import io.github.algomaster99.maven_module_graph.visitor.MavenModuleProcessor; |
| 5 | +import io.github.algomaster99.maven_module_graph.visitor.PlainTextVisitor; |
| 6 | +import org.apache.maven.model.Model; |
| 7 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 8 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 9 | + |
| 10 | +import java.io.FileReader; |
9 | 11 | import java.io.IOException; |
10 | 12 | import java.nio.file.Files; |
11 | 13 | import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
12 | 15 | import java.util.List; |
13 | 16 | import java.util.Map; |
14 | | -import java.util.Stack; |
15 | 17 | import java.util.regex.Matcher; |
16 | 18 | import java.util.regex.Pattern; |
17 | 19 |
|
18 | 20 | public class Utility { |
19 | 21 | private Utility() { |
20 | 22 | } |
21 | 23 |
|
22 | | - public static void printToFile(MavenModule root, Path plainText, int indent) { |
23 | | - StringBuilder sb = new StringBuilder(); |
24 | | - Stack<Pair<Integer, MavenModule>> levelToModule = new Stack<>(); |
25 | | - levelToModule.add(Pair.of(0, root)); |
26 | | - |
27 | | - String indentString = " ".repeat(indent); |
28 | | - |
29 | | - while (!levelToModule.isEmpty()) { |
30 | | - Pair<Integer, MavenModule> current = levelToModule.pop(); |
31 | | - int level = current.first(); |
32 | | - sb.append(indentString.repeat(level)); |
33 | | - |
34 | | - MavenModule currentModule = current.second(); |
35 | | - sb.append(String.format("%s:%s:%s", getGroupId(currentModule), currentModule.getSelf().getArtifactId(), getVersion(currentModule))); |
36 | | - List<MavenModule> children = currentModule.getSubmodules(); |
37 | | - for (MavenModule child : children) { |
38 | | - levelToModule.add(Pair.of(level + 1, child)); |
39 | | - } |
40 | | - sb.append("\n"); |
| 24 | + public static MavenModule createMavenModuleGraph(Path projectRoot, MavenModule parent, Map<Object, Object> properties) throws IOException, XmlPullParserException { |
| 25 | + Path rootPom = projectRoot.resolve("pom.xml"); |
| 26 | + Model rootModel = readPomModel(rootPom); |
| 27 | + |
| 28 | + properties.putAll(rootModel.getProperties()); |
| 29 | + |
| 30 | + String rootGroupId = parseProperty(getGroupId(rootModel), properties); |
| 31 | + String rootArtifactId = rootModel.getArtifactId(); |
| 32 | + String rootVersion = parseProperty(getVersion(rootModel), properties); |
| 33 | + |
| 34 | + MavenModule root = new MavenModule(rootModel, rootGroupId, rootArtifactId, rootVersion, properties, projectRoot.toAbsolutePath(), parent); |
| 35 | + |
| 36 | + List<String> submodules = getAllModules(rootModel); |
| 37 | + |
| 38 | + for (String module : submodules) { |
| 39 | + Path modulePath = projectRoot.resolve(module); |
| 40 | + MavenModule mavenModule = createMavenModuleGraph(modulePath, root, properties); |
| 41 | + root.addSubmodule(mavenModule); |
41 | 42 | } |
42 | 43 |
|
43 | | - // Write to file |
| 44 | + return root; |
| 45 | + } |
| 46 | + |
| 47 | + private static Model readPomModel(Path pomPath) throws IOException, XmlPullParserException { |
| 48 | + MavenXpp3Reader reader = new MavenXpp3Reader(); |
| 49 | + return reader.read(new FileReader(pomPath.toFile())); |
| 50 | + } |
| 51 | + |
| 52 | + private static List<String> getAllModules(Model model) { |
| 53 | + List<String> modules = new ArrayList<>(model.getModules()); |
| 54 | + model.getProfiles().forEach(profile -> modules.addAll(profile.getModules())); |
| 55 | + return modules; |
| 56 | + } |
| 57 | + |
| 58 | + public static void printToFile(MavenModule root, Path plainText, int indent) { |
| 59 | + PlainTextVisitor plainTextVisitor = new PlainTextVisitor(indent); |
| 60 | + MavenModuleProcessor.process(root, plainTextVisitor); |
| 61 | + |
44 | 62 | try { |
45 | | - Files.writeString(plainText, sb.toString()); |
| 63 | + Files.writeString(plainText, plainTextVisitor.getResult()); |
46 | 64 | } catch (IOException e) { |
47 | 65 | e.printStackTrace(); |
48 | 66 | } |
49 | 67 | } |
50 | 68 |
|
51 | 69 | public static void printToJson(MavenModule root, Path jsonPath, int indent) { |
52 | | - ObjectMapper mapper = new ObjectMapper(); |
53 | | - ObjectNode rootNode = convertToJson(mapper, root, 0); |
54 | | - |
| 70 | + JsonVisitor jsonVisitor = new JsonVisitor(indent); |
| 71 | + MavenModuleProcessor.process(root, jsonVisitor); |
55 | 72 | try { |
56 | | - if (indent > 0) { |
57 | | - DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ".repeat(indent), DefaultIndenter.SYS_LF); |
58 | | - DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); |
59 | | - printer.indentObjectsWith(indenter); |
60 | | - printer.indentArraysWith(indenter); |
61 | | - Files.writeString(jsonPath, mapper.writer(printer).writeValueAsString(rootNode)); |
62 | | - } else { |
63 | | - Files.writeString(jsonPath, mapper.writeValueAsString(rootNode)); |
64 | | - } |
| 73 | + jsonVisitor.writeToFile(jsonPath); |
65 | 74 | } catch (IOException e) { |
66 | 75 | e.printStackTrace(); |
67 | 76 | } |
68 | 77 | } |
69 | 78 |
|
70 | | - private static ObjectNode convertToJson(ObjectMapper mapper, MavenModule module, int depth) { |
71 | | - ObjectNode jsonNode = mapper.createObjectNode(); |
72 | | - jsonNode.put("depth", depth); |
73 | | - jsonNode.put("groupId", getGroupId(module)); |
74 | | - jsonNode.put("artifactId", module.getSelf().getArtifactId()); |
75 | | - jsonNode.put("version", getVersion(module)); |
76 | | - |
77 | | - // Add children with incremented depth |
78 | | - List<MavenModule> submodules = module.getSubmodules(); |
79 | | - if (!submodules.isEmpty()) { |
80 | | - ArrayNode childrenArray = jsonNode.putArray("submodules"); |
81 | | - for (MavenModule child : submodules) { |
82 | | - childrenArray.add(convertToJson(mapper, child, depth + 1)); |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - return jsonNode; |
87 | | - } |
88 | | - |
89 | | - private static String getGroupId(MavenModule module) { |
90 | | - String groupId = module.getSelf().getGroupId(); |
| 79 | + private static String getGroupId(Model module) { |
| 80 | + String groupId = module.getGroupId(); |
91 | 81 | if (groupId == null) { |
92 | | - if (module.getSelf().getParent() != null) { |
93 | | - return module.getSelf().getParent().getGroupId(); |
94 | | - } |
95 | | - if (module.getSelf().getParent() != null) { |
96 | | - return getGroupId(module.getParent()); |
| 82 | + if (module.getParent() != null) { |
| 83 | + return module.getParent().getGroupId(); |
97 | 84 | } |
98 | 85 | } |
99 | 86 | return groupId; |
100 | 87 | } |
101 | 88 |
|
102 | | - private static String getVersion(MavenModule module) { |
103 | | - String version = module.getSelf().getVersion(); |
104 | | - Map<Object, Object> properties = MavenModule.getProperties(); |
| 89 | + private static String getVersion(Model module) { |
| 90 | + String version = module.getVersion(); |
105 | 91 | if (version == null) { |
106 | | - if (module.getSelf().getParent() != null) { |
107 | | - return parseProperty(module.getSelf().getParent().getVersion(), properties); |
108 | | - } |
109 | | - if (module.getSelf().getParent() != null) { |
110 | | - return parseProperty(getVersion(module.getParent()), properties); |
| 92 | + if (module.getParent() != null) { |
| 93 | + return module.getParent().getVersion(); |
111 | 94 | } |
112 | 95 | } |
113 | | - return parseProperty(version, properties); |
| 96 | + return version; |
114 | 97 | } |
115 | 98 |
|
116 | 99 | private static String parseProperty(String value, Map<Object, Object> properties) { |
|
0 commit comments