Refactoring: simplify source roots search
This commit is contained in:
+28
-36
@@ -141,8 +141,7 @@ open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments>() {
|
|||||||
override val compiler = K2JVMCompiler()
|
override val compiler = K2JVMCompiler()
|
||||||
override fun createBlankArgs(): K2JVMCompilerArguments = K2JVMCompilerArguments()
|
override fun createBlankArgs(): K2JVMCompilerArguments = K2JVMCompilerArguments()
|
||||||
|
|
||||||
// Should be SourceDirectorySet or File
|
private val sourceRoots = HashSet<File>()
|
||||||
val srcDirsSources = HashSet<Any>()
|
|
||||||
|
|
||||||
// lazy because name is probably not available when constructor is called
|
// lazy because name is probably not available when constructor is called
|
||||||
private val taskBuildDirectory: File by lazy { File(File(project.buildDir, KOTLIN_BUILD_DIR_NAME), name) }
|
private val taskBuildDirectory: File by lazy { File(File(project.buildDir, KOTLIN_BUILD_DIR_NAME), name) }
|
||||||
@@ -493,55 +492,48 @@ open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getJavaSourceRoots(): Set<File> =
|
private fun getJavaSourceRoots(): Set<File> =
|
||||||
getSource()
|
findRootsForSources(getSource().filter { it.isJavaFile() })
|
||||||
.filter { it.isJavaFile() }
|
|
||||||
.map { findSrcDirRoot(it) }
|
|
||||||
.filterNotNull()
|
|
||||||
.toSet()
|
|
||||||
|
|
||||||
private fun File.isJavaFile() = extension.equals(JavaFileType.INSTANCE.defaultExtension, ignoreCase = true)
|
private fun File.isJavaFile() =
|
||||||
|
extension.equals(JavaFileType.INSTANCE.defaultExtension, ignoreCase = true)
|
||||||
|
|
||||||
// override setSource to track source directory sets and files (for generated android folders)
|
// override setSource to track source directory sets and files (for generated android folders)
|
||||||
override fun setSource(source: Any?) {
|
override fun setSource(source: Any?) {
|
||||||
srcDirsSources.clear()
|
sourceRoots.clear()
|
||||||
if (source is SourceDirectorySet) {
|
addSourceRoot(source)
|
||||||
srcDirsSources.add(source)
|
|
||||||
}
|
|
||||||
else if (source is File) {
|
|
||||||
srcDirsSources.add(source)
|
|
||||||
}
|
|
||||||
super.setSource(source)
|
super.setSource(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// override source to track source directory sets and files (for generated android folders)
|
// override source to track source directory sets and files (for generated android folders)
|
||||||
override fun source(vararg sources: Any?): SourceTask? {
|
override fun source(vararg sources: Any?): SourceTask? {
|
||||||
for (source in sources) {
|
sources.forEach { addSourceRoot(it) }
|
||||||
if (source is SourceDirectorySet) {
|
|
||||||
srcDirsSources.add(source)
|
|
||||||
}
|
|
||||||
else if (source is File) {
|
|
||||||
srcDirsSources.add(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.source(sources)
|
return super.source(sources)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun findSrcDirRoot(file: File): File? {
|
fun findRootsForSources(sources: Iterable<File>): Set<File> {
|
||||||
for (source in srcDirsSources) {
|
val resultRoots = HashSet<File>()
|
||||||
if (source is SourceDirectorySet) {
|
val sourceDirs = sources.mapTo(HashSet()) { it.parentFile }
|
||||||
for (root in source.srcDirs) {
|
|
||||||
if (FileUtil.isAncestor(root, file, false)) {
|
for (sourceDir in sourceDirs) {
|
||||||
return root
|
for (sourceRoot in sourceRoots) {
|
||||||
}
|
if (FileUtil.isAncestor(sourceRoot, sourceDir, /* strict = */false)) {
|
||||||
}
|
resultRoots.add(sourceRoot)
|
||||||
}
|
|
||||||
else if (source is File) {
|
|
||||||
if (FileUtil.isAncestor(source, file, false)) {
|
|
||||||
return source
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
|
return resultRoots
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addSourceRoot(source: Any?) {
|
||||||
|
when (source) {
|
||||||
|
is SourceDirectorySet -> {
|
||||||
|
sourceRoots.addAll(source.srcDirs)
|
||||||
|
}
|
||||||
|
is File -> {
|
||||||
|
sourceRoots.add(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -103,8 +103,15 @@ class AndroidGradleWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static def List<File> getJavaSources(BaseVariantData variantData) {
|
static def List<File> getJavaSources(BaseVariantData variantData) {
|
||||||
def result = new ArrayList<File>()
|
def result = new LinkedHashSet<File>()
|
||||||
|
|
||||||
|
// user sources
|
||||||
|
List<SourceProvider> providers = variantData.variantConfiguration.getSortedSourceProviders();
|
||||||
|
for (SourceProvider provider : providers) {
|
||||||
|
result.addAll((provider as AndroidSourceSet).getJava().getSrcDirs());
|
||||||
|
}
|
||||||
|
|
||||||
|
// generated sources
|
||||||
def getJavaSourcesMethod = variantData.getMetaClass().getMetaMethod("getJavaSources")
|
def getJavaSourcesMethod = variantData.getMetaClass().getMetaMethod("getJavaSources")
|
||||||
if (getJavaSourcesMethod.returnType.metaClass == Object[].metaClass) {
|
if (getJavaSourcesMethod.returnType.metaClass == Object[].metaClass) {
|
||||||
result.addAll(variantData.getJavaSources().findAll { it instanceof File })
|
result.addAll(variantData.getJavaSources().findAll { it instanceof File })
|
||||||
@@ -114,12 +121,6 @@ class AndroidGradleWrapper {
|
|||||||
result.addAll(fileTrees.collect { it.getDir() })
|
result.addAll(fileTrees.collect { it.getDir() })
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Old impl copied from android tools source. Delete?
|
|
||||||
List<SourceProvider> providers = variantData.variantConfiguration.getSortedSourceProviders();
|
|
||||||
for (SourceProvider provider : providers) {
|
|
||||||
result.addAll((provider as AndroidSourceSet).getJava().getSourceDirectoryTrees());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (variantData.scope.getGenerateRClassTask() != null) {
|
if (variantData.scope.getGenerateRClassTask() != null) {
|
||||||
result.add(variantData.scope.getRClassSourceOutputDir());
|
result.add(variantData.scope.getRClassSourceOutputDir());
|
||||||
}
|
}
|
||||||
@@ -150,6 +151,6 @@ class AndroidGradleWrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result.toList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user