Import package prefix from MPP projects

This commit is contained in:
Andrey Uskov
2019-07-02 22:36:16 +03:00
parent 2b6f566d58
commit 2665fff373
6 changed files with 70 additions and 15 deletions
@@ -369,11 +369,26 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
resolverCtx: ProjectResolverContext
) {
val mppModel = resolverCtx.getMppModel(gradleModule) ?: return
val sourceSetToPackagePrefix = mppModel.targets.flatMap { it.compilations }
.flatMap { compilation ->
compilation.sourceSets.map { sourceSet -> sourceSet.name to compilation.kotlinTaskProperties.packagePrefix }
}
.toMap()
if (resolverCtx.getExtraProject(gradleModule, ExternalProject::class.java) == null) return
processSourceSets(gradleModule, mppModel, ideModule, resolverCtx) { dataNode, sourceSet ->
if (dataNode == null || sourceSet.actualPlatforms.supports(KotlinPlatform.ANDROID)) return@processSourceSets
createContentRootData(sourceSet.sourceDirs, sourceSet.sourceType, dataNode)
createContentRootData(sourceSet.resourceDirs, sourceSet.resourceType, dataNode)
createContentRootData(
sourceSet.sourceDirs,
sourceSet.sourceType,
sourceSetToPackagePrefix[sourceSet.name],
dataNode
)
createContentRootData(
sourceSet.resourceDirs,
sourceSet.resourceType,
null,
dataNode
)
}
for (gradleContentRoot in gradleModule.contentRoots ?: emptySet<IdeaContentRoot?>()) {
@@ -529,10 +544,10 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() {
return ideModule.findChildModuleById(usedModuleId)
}
private fun createContentRootData(sourceDirs: Set<File>, sourceType: ExternalSystemSourceType, parentNode: DataNode<*>) {
private fun createContentRootData(sourceDirs: Set<File>, sourceType: ExternalSystemSourceType, packagePrefix: String?, parentNode: DataNode<*>) {
for (sourceDir in sourceDirs) {
val contentRootData = ContentRootData(GradleConstants.SYSTEM_ID, sourceDir.absolutePath)
contentRootData.storePath(sourceType, sourceDir.absolutePath)
contentRootData.storePath(sourceType, sourceDir.absolutePath, packagePrefix)
parentNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
}
}
@@ -102,6 +102,8 @@ abstract class AbstractKotlinGradleModelBuilder : ModelBuilderService {
val kotlinProjectExtensionClass = "org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension"
val kotlinSourceSetClass = "org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet"
val kotlinPluginWrapper = "org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt"
fun Task.getSourceSetName(): String {
return try {
javaClass.methods.firstOrNull { it.name.startsWith("getSourceSetName") && it.parameterTypes.isEmpty() }?.invoke(this) as? String
@@ -78,6 +78,7 @@ interface KotlinCompilation : KotlinModule {
val dependencyClasspath: List<String>
val disambiguationClassifier: String?
val platform: KotlinPlatform
val kotlinTaskProperties: KotlinTaskProperties
companion object {
@@ -268,7 +268,17 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
val arguments = buildCompilationArguments(compileKotlinTask) ?: return null
val dependencyClasspath = buildDependencyClasspath(compileKotlinTask)
val dependencies = buildCompilationDependencies(gradleCompilation, classifier, sourceSetMap, dependencyResolver, project)
return KotlinCompilationImpl(gradleCompilation.name, kotlinSourceSets, dependencies, output, arguments, dependencyClasspath)
val kotlinTaskProperties = getKotlinTaskProperties(compileKotlinTask)
return KotlinCompilationImpl(
gradleCompilation.name,
kotlinSourceSets,
dependencies,
output,
arguments,
dependencyClasspath,
kotlinTaskProperties
)
}
private fun buildCompilationDependencies(
@@ -85,7 +85,8 @@ data class KotlinCompilationImpl(
override val dependencies: Set<KotlinDependency>,
override val output: KotlinCompilationOutput,
override val arguments: KotlinCompilationArguments,
override val dependencyClasspath: List<String>
override val dependencyClasspath: List<String>,
override val kotlinTaskProperties: KotlinTaskProperties
) : KotlinCompilation {
// create deep copy
@@ -99,7 +100,8 @@ data class KotlinCompilationImpl(
kotlinCompilation.dependencies.map { it.deepCopy(cloningCache) }.toSet(),
KotlinCompilationOutputImpl(kotlinCompilation.output),
KotlinCompilationArgumentsImpl(kotlinCompilation.arguments),
ArrayList(kotlinCompilation.dependencyClasspath)
ArrayList(kotlinCompilation.dependencyClasspath),
KotlinTaskPropertiesImpl(kotlinCompilation.kotlinTaskProperties)
) {
disambiguationClassifier = kotlinCompilation.disambiguationClassifier
platform = kotlinCompilation.platform
@@ -9,6 +9,7 @@ import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.internal.FactoryNamedDomainObjectContainer
import org.gradle.api.plugins.JavaPluginConvention
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder.Companion.getSourceSetName
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder.Companion.kotlinPluginWrapper
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder.Companion.kotlinProjectExtensionClass
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder.Companion.kotlinSourceSetClass
import java.io.File
@@ -19,14 +20,22 @@ interface KotlinTaskProperties : Serializable {
val incremental: Boolean?
val packagePrefix: String?
val pureKotlinSourceFolders: List<File>?
val pluginVersion: String?
}
data class KotlinTaskPropertiesImpl(
override val incremental: Boolean?,
override val packagePrefix: String?,
override val pureKotlinSourceFolders: List<File>?
) : KotlinTaskProperties
override val pureKotlinSourceFolders: List<File>?,
override val pluginVersion: String?
) : KotlinTaskProperties {
constructor(kotlinTaskProperties: KotlinTaskProperties) : this(
kotlinTaskProperties.incremental,
kotlinTaskProperties.packagePrefix,
kotlinTaskProperties.pureKotlinSourceFolders?.map { it }?.toList(),
kotlinTaskProperties.pluginVersion
)
}
typealias KotlinTaskPropertiesBySourceSet = MutableMap<String, KotlinTaskProperties>
@@ -69,11 +78,27 @@ private fun Task.getPureKotlinSourceRoots(sourceSet: String): List<File>? {
return null
}
private fun Task.getKotlinPluginVersion(): String? {
try {
val pluginWrapperClass = javaClass.classLoader.loadClass(kotlinPluginWrapper)
val getVersionMethod =
pluginWrapperClass.getMethod("getKotlinPluginVersion", javaClass.classLoader.loadClass("org.gradle.api.Project"))
return getVersionMethod.invoke(null, this.project) as String
} catch (e: Exception) {
}
return null
}
fun KotlinTaskPropertiesBySourceSet.acknowledgeTask(compileTask: Task) {
this[compileTask.getSourceSetName()] =
KotlinTaskPropertiesImpl(
compileTask.getIsIncremental(),
compileTask.getPackagePrefix(),
compileTask.getPureKotlinSourceRoots(compileTask.getSourceSetName())
)
getKotlinTaskProperties(compileTask)
}
fun getKotlinTaskProperties(compileTask: Task): KotlinTaskPropertiesImpl {
return KotlinTaskPropertiesImpl(
compileTask.getIsIncremental(),
compileTask.getPackagePrefix(),
compileTask.getPureKotlinSourceRoots(compileTask.getSourceSetName()),
compileTask.getKotlinPluginVersion()
)
}