Search in sources :

Example 1 with SimpleClassMetaDataRepository

use of org.gradle.build.docs.model.SimpleClassMetaDataRepository in project gradle by gradle.

the class GenerateDefaultImportsTask method generate.

@TaskAction
public void generate() throws IOException {
    SimpleClassMetaDataRepository<ClassMetaData> repository = new SimpleClassMetaDataRepository<>();
    repository.load(getMetaDataFile().getAsFile().get());
    final Set<String> excludedPrefixes = new HashSet<>();
    final Set<String> excludedPackages = new HashSet<>();
    for (String excludePattern : excludePatterns) {
        if (excludePattern.endsWith(".**")) {
            String baseName = excludePattern.substring(0, excludePattern.length() - 3);
            excludedPrefixes.add(baseName + '.');
            excludedPackages.add(baseName);
        } else {
            excludedPackages.add(excludePattern);
        }
    }
    final Set<String> packages = new LinkedHashSet<>();
    final Multimap<String, String> simpleNames = LinkedHashMultimap.create();
    repository.each(new Action<ClassMetaData>() {

        public void execute(ClassMetaData classMetaData) {
            if (classMetaData.getOuterClassName() != null) {
                // Ignore inner classes
                return;
            }
            String packageName = classMetaData.getPackageName();
            if (excludedPackages.contains(packageName)) {
                return;
            }
            for (String excludedPrefix : excludedPrefixes) {
                if (packageName.startsWith(excludedPrefix)) {
                    return;
                }
            }
            simpleNames.put(classMetaData.getSimpleName(), classMetaData.getClassName());
            packages.add(packageName);
        }
    });
    try (PrintWriter mappingFileWriter = new PrintWriter(new FileWriter(getMappingDestFile().getAsFile().get()))) {
        for (Map.Entry<String, Collection<String>> entry : simpleNames.asMap().entrySet()) {
            if (entry.getValue().size() > 1) {
                System.out.println(String.format("Multiple DSL types have short name '%s'", entry.getKey()));
                for (String className : entry.getValue()) {
                    System.out.println("    * " + className);
                }
            }
            mappingFileWriter.print(entry.getKey());
            mappingFileWriter.print(":");
            for (String className : entry.getValue()) {
                mappingFileWriter.print(className);
                mappingFileWriter.print(";");
            }
            mappingFileWriter.println();
        }
    }
    try (PrintWriter writer = new PrintWriter(new FileWriter(getImportsDestFile().getAsFile().get()))) {
        for (String packageName : packages) {
            writer.print("import ");
            writer.print(packageName);
            writer.println(".*");
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileWriter(java.io.FileWriter) Collection(java.util.Collection) Map(java.util.Map) ClassMetaData(org.gradle.build.docs.dsl.source.model.ClassMetaData) SimpleClassMetaDataRepository(org.gradle.build.docs.model.SimpleClassMetaDataRepository) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) PrintWriter(java.io.PrintWriter) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 TaskAction (org.gradle.api.tasks.TaskAction)1 ClassMetaData (org.gradle.build.docs.dsl.source.model.ClassMetaData)1 SimpleClassMetaDataRepository (org.gradle.build.docs.model.SimpleClassMetaDataRepository)1