summaryrefslogtreecommitdiffstats
path: root/container-dev-builder/tools/src/main/java/com/yahoo/container/dev/builder/DependencyResolver.java
blob: 7e9d7afaed37eb5d924d299d0a156c0ec4866a92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.dev.builder;

import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
public class DependencyResolver {

    private static final Path DEPENDENCIES = Paths.get("dependencies");

    public static void main(String[] args) throws IOException, XmlPullParserException {
        final Set<String> blacklist = new HashSet<>(Arrays.asList(args).subList(1, args.length));
        final Set<String> dependencies = new TreeSet<>();
        Files.walkFileTree(Paths.get(args[0]), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                if (!attrs.isRegularFile()) {
                    return FileVisitResult.CONTINUE;
                }
                if (!file.getFileName().equals(DEPENDENCIES)) {
                    return FileVisitResult.CONTINUE;
                }
                for (final String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
                    for (final String dependency : line.split(" ")) {
                        if (dependency == null || dependency.isEmpty()) {
                            continue;
                        }
                        final String[] arr = dependency.split(":");
                        if (arr.length != 5 || blacklist.contains(arr[0] + ":" + arr[1])) {
                            continue;
                        }
                        dependencies.add(dependency);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
        for (final String dependency : dependencies) {
            System.out.println(dependency);
        }
    }
}