Commit ea54253f authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ simple dependency analyser to aid cleanup, #145

parent 3f43017e
Loading
Loading
Loading
Loading
+70 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.helper;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class DependencyAnalyser {
	static List<String> matchesList = new ArrayList<String>();

	public static void main(String[] args) throws Exception {
		PathMatcher matcher = FileSystems.getDefault().getPathMatcher("regex:.*META-INF/MANIFEST\\.MF");
        
        try (Stream<Path> files = Files.walk(Paths.get("../"))) {
            // Filter files that match the pattern
            List<Path> manifests = files.filter(matcher::matches)
            		.sorted()
            		.collect(Collectors.toList());
            for (Path m : manifests) {
//            	System.out.println(m);
            	getDependencies(m.toFile());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
		
	}
	public static void getDependencies(File file) throws Exception{
		Manifest manifest = new Manifest(new FileInputStream(file));
		Attributes attr = manifest.getMainAttributes();
		for (Entry<Object, Object> e:attr.entrySet()) {
//			System.out.println(e.getKey() + " : "+e.getValue());
		}
		String id = (String) attr.getValue("Bundle-SymbolicName");
		String bundles = (String) attr.getValue("Require-Bundle");
		if (bundles != null) {
			bundles = bundles.replaceAll(";bundle-version=(\".+?\")", "")
					.replaceAll(";resolution:=optional", "")
					.replaceAll(";visibility:=reexport", "")
					.replaceAll("-", ".")
					;
			String[] bundleList = bundles.split(",");
			System.out.println(""+id.replaceAll(";.+", ""));
			for (String b : bundleList) {
				if (!b.startsWith("org.etsi.mts")) {
//					System.out.println(id.replaceAll(";.+", "")+"-->"+b);
					System.out.println("  "+b);
				}
			}
		}
	}
}