1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package io.github.tonywasher.joceanus.themis.parser.maven;
19
20 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
21 import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
22 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
23 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
24 import io.github.tonywasher.joceanus.themis.exc.ThemisLogicException;
25 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
26 import io.github.tonywasher.joceanus.themis.parser.maven.ThemisMavenPom.ThemisXMavenControl;
27
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33
34
35
36
37 public class ThemisMavenParser
38 implements ThemisXMavenControl {
39
40
41
42 private static final String POM_NAME = "pom.xml";
43
44
45
46
47 private final Map<ThemisMavenId, ThemisMavenPom> thePoms;
48
49
50
51
52 private final List<ThemisMavenPom> theModules;
53
54
55
56
57 private final List<ThemisMavenId> theDependencies;
58
59
60
61
62 private final ThemisMavenPom thePom;
63
64
65
66
67 private boolean processedLocal;
68
69
70
71
72
73
74
75
76 public ThemisMavenParser(final TethysUIThreadStatusReport pReport,
77 final File pLocation) throws OceanusException {
78
79 OceanusProfile myTask = pReport.getActiveTask();
80 myTask = myTask.startTask(ThemisDataResource.TASK_DISCOVER);
81 pReport.initTask(ThemisDataResource.TASK_DISCOVER);
82 pReport.setNumStages(2);
83
84
85 thePoms = new LinkedHashMap<>();
86 processedLocal = false;
87
88
89 thePom = loadPomAtLocation(pLocation);
90 processedLocal = true;
91
92
93 OceanusProfile mySubTask = myTask.startTask(ThemisDataResource.TASK_DISCOVERLOCAL);
94 final List<ThemisMavenPom> myModules = new ArrayList<>(thePoms.values());
95 pReport.setNewStage(ThemisDataResource.TASK_DISCOVERLOCAL);
96 pReport.setNumSteps(myModules.size());
97 for (ThemisMavenPom myModule : myModules) {
98
99 final String myName = myModule.getId().toString();
100 mySubTask.startTask(myName);
101 pReport.setNextStep();
102 myModule.getVersions();
103 myModule.getDirectDependencies();
104 }
105
106
107 mySubTask = myTask.startTask(ThemisDataResource.TASK_DISCOVERDEPENDENCY);
108 pReport.setNewStage(ThemisDataResource.TASK_DISCOVERDEPENDENCY);
109 pReport.setNumSteps(myModules.size());
110 for (ThemisMavenPom myModule : myModules) {
111
112 final String myName = myModule.getId().toString();
113 mySubTask.startTask(myName);
114 pReport.setNextStep();
115 myModule.getDependencies();
116 }
117
118
119 theModules = getModuleList(myModules);
120 theDependencies = thePom.getDependencies();
121 myTask.end();
122 }
123
124
125
126
127
128
129 public String getName() {
130 return thePom.getArtifactId();
131 }
132
133
134
135
136
137
138 public List<ThemisMavenId> getModules() {
139 return theModules.stream().map(ThemisMavenPom::getId).toList();
140 }
141
142
143
144
145
146
147 public List<ThemisMavenPom> getParsedModules() {
148 return theModules;
149 }
150
151
152
153
154
155
156 public List<ThemisMavenId> getDependencies() {
157 return theDependencies;
158 }
159
160 @Override
161 public ThemisMavenPom loadPomViaId(final ThemisMavenId pId) throws OceanusException {
162
163 final ThemisMavenPom myExisting = thePoms.get(pId);
164 if (myExisting != null) {
165 return myExisting;
166 }
167
168
169 if (!processedLocal) {
170 throw new ThemisLogicException("Processed pom via id before finished local processing");
171 }
172
173
174 final ThemisMavenPom myPom = doLoadPomViaId(pId);
175 thePoms.put(pId, myPom);
176
177
178 if (myPom.isJarPackaging()) {
179
180 ThemisMavenLocation.ensureJarArtifact(pId);
181 }
182
183
184 myPom.getVersions();
185 myPom.getDirectDependencies();
186 myPom.getDependencies();
187
188
189 return myPom;
190 }
191
192
193
194
195
196
197
198
199 private ThemisMavenPom doLoadPomViaId(final ThemisMavenId pId) throws OceanusException {
200
201 if (pId.getClassifier() != null) {
202
203 return new ThemisMavenPom(pId);
204 }
205
206
207 ThemisMavenLocation.ensurePomArtifact(pId);
208
209
210 return new ThemisMavenPom(this, new File(ThemisMavenLocation.getLocalPomFileName(pId)));
211 }
212
213 @Override
214 public ThemisMavenPom loadPomAtLocation(final File pLocation) throws OceanusException {
215
216 final ThemisMavenPom myPom = new ThemisMavenPom(this, new File(pLocation, POM_NAME));
217 final ThemisMavenId myId = myPom.getId();
218
219
220 final ThemisMavenPom myExisting = thePoms.get(myId);
221 if (myExisting != null) {
222 throw new ThemisDataException("Duplicate POM - " + myId);
223 }
224
225
226 thePoms.put(myId, myPom);
227
228
229 myPom.processLocalDetails();
230
231
232 return myPom;
233 }
234
235
236
237
238
239
240
241
242 private List<ThemisMavenPom> getModuleList(final List<ThemisMavenPom> pLocal) throws OceanusException {
243
244 final List<ThemisMavenPom> myModules = new ArrayList<>();
245 for (ThemisMavenPom myModule : pLocal) {
246 if (myModule.isJarPackaging()) {
247 myModules.add(myModule);
248 }
249 }
250 return myModules;
251 }
252
253
254
255
256
257
258
259 public List<ThemisMavenId> getProjectDependencies() throws OceanusException {
260
261 final List<ThemisMavenId> myIds = new ArrayList<>();
262
263
264 for (ThemisMavenPom myModule : theModules) {
265
266 for (ThemisMavenId myDependency : myModule.getDependencies()) {
267
268 ThemisMavenPom.checkDependencyId(myIds, myDependency);
269 }
270 }
271
272
273 return myIds;
274 }
275
276 }