1 import org.gradle.api.Action 2 import org.gradle.api.Project 3 import org.gradle.api.file.FileCollection 4 import org.gradle.api.tasks.Delete 5 import org.gradle.api.tasks.compile.JavaCompile 6 import org.gradle.api.tasks.util.PatternFilterable 7 import org.gradle.configurationcache.extensions.capitalized 8 import org.gradle.kotlin.dsl.maybeCreate 9 import org.gradle.kotlin.dsl.register 10 import org.gradle.kotlin.dsl.withType 11 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 12 import task.SymlinkSourcesTask 13 import java.io.File 14 Projectnull15fun Project.symlinkedSources( 16 dir: String, 17 name: String = File(dir).name, 18 excludeSubdirectories: Boolean = true, 19 test: Boolean = false, 20 filter: Action<PatternFilterable> = Action { }, 21 ): FileCollection { 22 val sourceDir = File(dir) 23 val type = if (test) "test" else "main" 24 val outputDir = rootDir.resolve(".symlinkSrc/${project.name}/${type}/${name}") <lambda>null25 val task = tasks.register<SymlinkSourcesTask>("symlink${type.capitalized()}Sources${name.capitalized()}") { 26 description = "Filter sources from ${sourceDir.absolutePath} and link them to ${outputDir.absolutePath}" 27 this.sourcesRoot.set(dir) 28 this.files { 29 exclude { !it.isDirectory && it.file.extension !in arrayOf("kt", "java") } 30 if (excludeSubdirectories) { 31 exclude { it.isDirectory } 32 } 33 filter.execute(this) 34 } 35 this.destinationDir.set(outputDir) 36 } <lambda>null37 tasks.withType<KotlinCompile> { dependsOn(task) } <lambda>null38 tasks.withType<JavaCompile> { dependsOn(task) } <lambda>null39 tasks.maybeCreate<Delete>("cleanSymlinkedSources").apply { 40 group = "symlink" 41 delete(outputDir) 42 } <lambda>null43 val symlinkSources = tasks.maybeCreate("symlinkSources").apply { 44 group = "symlink" 45 dependsOn(task) 46 } <lambda>null47 rootProject.tasks.named("updateSdkSources") { 48 dependsOn(symlinkSources) 49 } <lambda>null50 return files(task.flatMap(SymlinkSourcesTask::destinationDir)) { 51 builtBy(task) 52 } 53 } 54