name.remal.gradle_plugins.toolkit.build_logic.check-source-dirs.gradle Maven / Gradle / Ivy
import static java.util.stream.Collectors.toSet
import java.util.stream.Stream
allprojects {
pluginManager.withPlugin('java') {
TaskProvider checkSourceDirs = tasks.register('checkSourceDirs') { Task task ->
task.group = 'verification'
task.doLast {
Set allSourceDirs = project.sourceSets.stream()
.flatMap { SourceSet sourceSet -> sourceSet.allSource.srcDirs.stream() }
.map { File file -> file.canonicalFile }
.flatMap { File file -> Stream.of(file, file.parentFile) }
.filter { it != null }
.collect(toSet())
List invalidSourceDirs = []
Closure> getSubDirs = { File file ->
return file.listFiles()?.collect()?.findAll { it.directory } ?: []
}
File baseSourceDir = project.file('src')
List sourceSetsBaseDirs = getSubDirs(baseSourceDir)
for (File sourceSetsBaseDir : sourceSetsBaseDirs) {
sourceSetsBaseDir = sourceSetsBaseDir.canonicalFile
if (!allSourceDirs.contains(sourceSetsBaseDir)) {
invalidSourceDirs.add(sourceSetsBaseDir)
break
}
List sourceDirs = getSubDirs(sourceSetsBaseDir)
for (File sourceDir : sourceDirs) {
sourceDir = sourceDir.canonicalFile
if (!allSourceDirs.contains(sourceDir)) {
invalidSourceDirs.add(sourceDir)
}
}
}
if (!invalidSourceDirs.isEmpty()) {
throw new GradleException("There are source directories that are not a part of any source sets:\n ${invalidSourceDirs.join('\n ')}")
}
}
}
tasks.named('check').configure { dependsOn(checkSourceDirs) }
}
}