1 import java.io.File;
2 import java.util.ArrayList;
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.stream.Collectors;
7 
8 import kotlin.Triple;
9 
10 /**
11  * Class to create a map of available dependencies in repo
12  */
13 public class RepoDependencyMapper {
14 
15   private final Map<String, Triple<String, String, String>> mVersionMap = new HashMap<>();
16 
mapPath(String path)17   public RepoDependencyMapper mapPath(String path) {
18     return mapPath(path, "");
19   }
20 
21   /**
22    * Parses the provided path for a possible m2repository
23    */
mapPath(String path, String prefix)24   public RepoDependencyMapper mapPath(String path, String prefix) {
25     File repoPath = new File(path);
26     for (File child : repoPath.listFiles()) {
27       checkEndPoint(child, new ArrayList<>(), prefix);
28     }
29     return this;
30   }
31 
getMap()32   public Map<String, Triple<String, String, String>> getMap() {
33     return mVersionMap;
34   }
35 
checkEndPoint(File current, List<File> parents, String prefix)36   private void checkEndPoint(File current, List<File> parents, String prefix) {
37     if (!current.isDirectory()) {
38       return;
39     }
40 
41     parents.add(current);
42     for (File child : current.listFiles()) {
43       checkEndPoint(child, parents, prefix);
44     }
45     parents.remove(current);
46 
47     // Check if this is the end point.
48     int parentsCount = parents.size();
49     if (parentsCount > 0) {
50       String versionName = current.getName();
51       String moduleName = parents.get(parentsCount - 1).getName();
52       if (new File(current, moduleName + "-" + versionName + ".pom").exists()) {
53         String groupName = prefix + parents.subList(0, parentsCount - 1)
54             .stream().map(File::getName).collect(Collectors.joining("."));
55 
56         String moduleOverride = null;
57         for (String suffix : PLATFORM_TYPE_SUFFIX) {
58           if (moduleName.endsWith(suffix)) {
59             moduleOverride = moduleName;
60             moduleName = moduleName.substring(0, moduleName.length() - suffix.length());
61             break;
62           }
63         }
64 
65         System.out.println(groupName + ":" + moduleName + " -> " + versionName);
66         mVersionMap.put(groupName + ":" + moduleName,
67             new Triple<>(groupName, moduleOverride, versionName));
68       }
69     }
70   }
71 
72   private static final String[] PLATFORM_TYPE_SUFFIX = new String[]{"-jvm", "-android"};
73 }
74