Skip to content

Commit

Permalink
Share defaultSourceFolder logic between syncKotlinAndAndroidSourceSet…
Browse files Browse the repository at this point in the history
…s.kt and KotlinSourceSetFactory.kt
  • Loading branch information
sellmair authored and Sebastian Sellmair committed Nov 24, 2020
1 parent 7c7eada commit 2286498
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,15 @@ import org.jetbrains.kotlin.statistics.metrics.StringMetrics
import javax.inject.Inject
import kotlin.reflect.KClass

abstract class KotlinBasePluginWrapper(
protected val fileResolver: FileResolver
) : Plugin<Project> {
abstract class KotlinBasePluginWrapper : Plugin<Project> {

private val log = Logging.getLogger(this.javaClass)
val kotlinPluginVersion = loadKotlinVersionFromResource(log)

open val projectExtensionClass: KClass<out KotlinProjectExtension> get() = KotlinProjectExtension::class

internal open fun kotlinSourceSetFactory(project: Project): NamedDomainObjectFactory<KotlinSourceSet> =
DefaultKotlinSourceSetFactory(project, fileResolver)
DefaultKotlinSourceSetFactory(project)

override fun apply(project: Project) {
val listenerRegistryHolder = BuildEventsListenerRegistryHolder.getInstance(project)
Expand Down Expand Up @@ -127,9 +125,8 @@ abstract class KotlinBasePluginWrapper(
}

open class KotlinPluginWrapper @Inject constructor(
fileResolver: FileResolver,
protected val registry: ToolingModelBuilderRegistry
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
KotlinPlugin(kotlinPluginVersion, registry)

Expand All @@ -138,9 +135,8 @@ open class KotlinPluginWrapper @Inject constructor(
}

open class KotlinCommonPluginWrapper @Inject constructor(
fileResolver: FileResolver,
protected val registry: ToolingModelBuilderRegistry
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
KotlinCommonPlugin(kotlinPluginVersion, registry)

Expand All @@ -149,9 +145,8 @@ open class KotlinCommonPluginWrapper @Inject constructor(
}

open class KotlinAndroidPluginWrapper @Inject constructor(
fileResolver: FileResolver,
protected val registry: ToolingModelBuilderRegistry
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
KotlinAndroidPlugin(kotlinPluginVersion, registry)

Expand All @@ -160,9 +155,8 @@ open class KotlinAndroidPluginWrapper @Inject constructor(
}

open class Kotlin2JsPluginWrapper @Inject constructor(
fileResolver: FileResolver,
protected val registry: ToolingModelBuilderRegistry
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
Kotlin2JsPlugin(kotlinPluginVersion, registry)

Expand All @@ -171,8 +165,7 @@ open class Kotlin2JsPluginWrapper @Inject constructor(
}

open class KotlinJsPluginWrapper @Inject constructor(
fileResolver: FileResolver
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
KotlinJsPlugin(kotlinPluginVersion)

Expand Down Expand Up @@ -204,9 +197,8 @@ open class KotlinJsPluginWrapper @Inject constructor(
}

open class KotlinMultiplatformPluginWrapper @Inject constructor(
fileResolver: FileResolver,
private val featurePreviews: FeaturePreviews
) : KotlinBasePluginWrapper(fileResolver) {
) : KotlinBasePluginWrapper() {
override fun getPlugin(project: Project, kotlinGradleBuildServices: KotlinGradleBuildServices): Plugin<Project> =
KotlinMultiplatformPlugin(
kotlinPluginVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.AbstractAndroidProjectHandler.Companion.kotlinSourceSetNameForAndroidSourceSet
import org.jetbrains.kotlin.gradle.plugin.addConvention
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinSourceSetFactory
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinSourceSetFactory.Companion.defaultSourceFolder
import java.io.File

internal fun syncKotlinAndAndroidSourceSets(target: KotlinAndroidTarget) {
Expand Down Expand Up @@ -107,39 +109,36 @@ private fun syncKotlinAndAndroidResources(

androidSourceSet.resources.srcDirs(*kotlinSourceSet.resources.toList().toTypedArray())
if (androidSourceSet.resources.srcDirs.isNotEmpty()) {
androidSourceSet.resources.srcDir(kotlinSourceSet.sourceFolderFor(project, "resources"))
androidSourceSet.resources.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "resources"))
kotlinSourceSet.resources.srcDirs(androidSourceSet.resources.srcDirs)
}

if (androidSourceSet.assets.srcDirs.isNotEmpty()) {
androidSourceSet.assets.srcDir(kotlinSourceSet.sourceFolderFor(project, "assets"))
androidSourceSet.assets.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "assets"))
}

if (androidSourceSet.res.srcDirs.isNotEmpty()) {
androidSourceSet.res.srcDir(kotlinSourceSet.sourceFolderFor(project, "res"))
androidSourceSet.res.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "res"))
}

if (androidSourceSet.aidl.srcDirs.isNotEmpty()) {
androidSourceSet.aidl.srcDir(kotlinSourceSet.sourceFolderFor(project, "aidl"))
androidSourceSet.aidl.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "aidl"))
}

if (androidSourceSet.renderscript.srcDirs.isNotEmpty()) {
androidSourceSet.renderscript.srcDir(kotlinSourceSet.sourceFolderFor(project, "rs"))
androidSourceSet.renderscript.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "rs"))
}

if (androidSourceSet.jni.srcDirs.isNotEmpty()) {
androidSourceSet.jni.srcDir(kotlinSourceSet.sourceFolderFor(project, "jni"))
androidSourceSet.jni.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "jni"))
}

if (androidSourceSet.jniLibs.srcDirs.isNotEmpty()) {
androidSourceSet.jniLibs.srcDir(kotlinSourceSet.sourceFolderFor(project, "jniLibs"))
androidSourceSet.jniLibs.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "jniLibs"))
}

if (androidSourceSet.shaders.srcDirs.isNotEmpty()) {
androidSourceSet.shaders.srcDir(kotlinSourceSet.sourceFolderFor(project, "shaders"))
androidSourceSet.shaders.srcDir(defaultSourceFolder(project, kotlinSourceSet.name, "shaders"))
}
}

private fun KotlinSourceSet.sourceFolderFor(project: Project, type: String): File {
return project.file("src/${this.name}/$type")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.gradle.targets.metadata.isKotlinGranularMetadataEnab
import java.io.File

internal abstract class KotlinSourceSetFactory<T : KotlinSourceSet> internal constructor(
protected val fileResolver: FileResolver,
protected val project: Project
) : NamedDomainObjectFactory<KotlinSourceSet> {

Expand All @@ -29,11 +28,8 @@ internal abstract class KotlinSourceSetFactory<T : KotlinSourceSet> internal con
return result
}

protected open fun defaultSourceLocation(sourceSetName: String): File =
project.file("src/$sourceSetName")

protected open fun setUpSourceSetDefaults(sourceSet: T) {
sourceSet.kotlin.srcDir(File(defaultSourceLocation(sourceSet.name), "kotlin"))
sourceSet.kotlin.srcDir(defaultSourceFolder(project, sourceSet.name, "kotlin"))
defineSourceSetConfigurations(project, sourceSet)
}

Expand All @@ -49,19 +45,29 @@ internal abstract class KotlinSourceSetFactory<T : KotlinSourceSet> internal con
}

protected abstract fun doCreateSourceSet(name: String): T

companion object {
/**
* @return default location of source folders for a kotlin source set
* e.g. src/jvmMain/kotlin (sourceSetName="jvmMain", type="kotlin")
*/
fun defaultSourceFolder(project: Project, sourceSetName: String, type: String): File {
return project.file("src/$sourceSetName/$type")
}
}
}


internal class DefaultKotlinSourceSetFactory(
project: Project,
fileResolver: FileResolver
) : KotlinSourceSetFactory<DefaultKotlinSourceSet>(fileResolver, project) {
project: Project
) : KotlinSourceSetFactory<DefaultKotlinSourceSet>(project) {

override val itemClass: Class<DefaultKotlinSourceSet>
get() = DefaultKotlinSourceSet::class.java

override fun setUpSourceSetDefaults(sourceSet: DefaultKotlinSourceSet) {
super.setUpSourceSetDefaults(sourceSet)
sourceSet.resources.srcDir(File(defaultSourceLocation(sourceSet.name), "resources"))
sourceSet.resources.srcDir(defaultSourceFolder(project, sourceSet.name, "resources"))

val dependencyConfigurationWithMetadata = with(sourceSet) {
listOf(
Expand Down Expand Up @@ -90,4 +96,4 @@ internal class DefaultKotlinSourceSetFactory(
override fun doCreateSourceSet(name: String): DefaultKotlinSourceSet {
return DefaultKotlinSourceSet(project, name)
}
}
}

0 comments on commit 2286498

Please sign in to comment.