1 package net.avcompris.devops;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.collect.Lists.newArrayList;
5 import static com.google.common.collect.Maps.newHashMap;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.function.BiConsumer;
13
14 import net.avcompris.binding.dom.helper.DomBinderUtils;
15
16 public class Workspace {
17
18 public static void scanForPoms(final File workspaceDir, final BiConsumer<File, Pom> consumer) throws IOException {
19
20 checkNotNull(workspaceDir, "workspaceDir");
21 checkNotNull(consumer, "consumer");
22
23 new Workspace(consumer).scan(workspaceDir);
24 }
25
26 public static List<Pom> getAllPoms(final File workspaceDir) throws IOException {
27
28 checkNotNull(workspaceDir, "workspaceDir");
29
30 final List<Pom> allPoms = newArrayList();
31
32 new Workspace((dir, pom) -> {
33
34 allPoms.add(pom);
35
36 }).scan(workspaceDir);
37
38 return allPoms;
39 }
40
41 private void scanPom(final File dir, final Pom pom) throws IOException {
42
43 final String dirName = dir.getName();
44
45 if (allPomsByDirNames.containsKey(dirName)) {
46
47
48
49 System.err.println("WARNING *** Directory name was encountered twice: " + dirName);
50
51 return;
52 }
53
54 System.out.println("Adding: " + dirName + "...");
55
56 allPomsByDirNames.put(dirName, pom);
57
58 consumer.accept(dir, pom);
59
60 for (final String module : pom.getModules()) {
61
62 final File moduleDir = new File(dir, module);
63
64 if (!moduleDir.isDirectory()) {
65
66 continue;
67
68
69 }
70
71 final File modulePomFile = new File(moduleDir, "pom.xml");
72
73 if (!modulePomFile.isFile()) {
74
75 throw new FileNotFoundException(modulePomFile.getCanonicalPath());
76 }
77
78 final Pom modulePom = DomBinderUtils.xmlContentToJava(modulePomFile, Pom.class);
79
80 scanPom(moduleDir, modulePom);
81 }
82 }
83
84 private final Map<String, Pom> allPomsByDirNames = newHashMap();
85
86 private final BiConsumer<File, Pom> consumer;
87
88 private Workspace(final BiConsumer<File, Pom> consumer) {
89
90 this.consumer = checkNotNull(consumer, "consumer");
91 }
92
93 private void scan(final File workspaceDir) throws IOException {
94
95 for (final File dir : workspaceDir.listFiles(file -> file.isDirectory())) {
96
97 final File pomFile = new File(dir, "pom.xml");
98
99 if (!pomFile.isFile()) {
100
101 continue;
102 }
103
104 final Pom pom = DomBinderUtils.xmlContentToJava(pomFile, Pom.class);
105
106 scanPom(dir, pom);
107 }
108 }
109 }