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(".*");
}
}
}
Aggregations