[Gradle] Fix code review NITs

* rename parentVisibleSourceSetsProvider -> visibleSourceSetsFromParentsProvider
* use flatMap.toSet() instead flatMapTo(mutableSetOf())
* Use `extraProperties` instead of extensions
* KotlinMultiplatformExtension.metadat() instead of finding it by type
* rename PlatformCompilationData.sourceSets to allSourceSets for clarity
* Use FileCollection instead of Provider<Set<File>>
* Use System.lineSeparator() instead of `\n` for better platform support
* Report meaningful error when `participatingSourceSets` is accessed
  in execution state when Task state is loaded from Configuration Cache
* Mark newly added API as internal since there is no use of them in
  user's build scripts.

^KT-49933
This commit is contained in:
Anton Lakotka
2023-01-17 09:19:57 +01:00
committed by Space Team
parent 608760e32b
commit 5094ac945c
7 changed files with 50 additions and 56 deletions
@@ -103,7 +103,7 @@ private val ComponentIdentifier.uniqueKey get(): ComponentIdentifierKey =
internal class GranularMetadataTransformation(
val params: Params,
parentVisibleSourceSetsProvider: () -> Iterable<Map<ComponentIdentifierKey, Set<String>>>
visibleSourceSetsFromParentsProvider: () -> Iterable<Map<ComponentIdentifierKey, Set<String>>>
) {
class Params(
val sourceSetName: String,
@@ -130,8 +130,8 @@ internal class GranularMetadataTransformation(
val moduleId: Provider<ModuleDependencyIdentifier>
)
private val parentVisibleSourceSets: Map<ComponentIdentifierKey, Set<String>> by lazy {
parentVisibleSourceSetsProvider().reduceOrNull { acc, map -> acc mergeWith map }.orEmpty()
private val visibleSourceSetsFromParents: Map<ComponentIdentifierKey, Set<String>> by lazy {
visibleSourceSetsFromParentsProvider().reduceOrNull { acc, map -> acc mergeWith map }.orEmpty()
}
val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() }
@@ -140,7 +140,7 @@ internal class GranularMetadataTransformation(
metadataDependencyResolutions
.filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>()
.groupBy { it.dependency.id.uniqueKey }
.mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMapTo(mutableSetOf()) { it.allVisibleSourceSetNames } }
.mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMap { it.allVisibleSourceSetNames }.toSet() }
}
private fun doTransform(): Iterable<MetadataDependencyResolution> {
@@ -169,7 +169,7 @@ internal class GranularMetadataTransformation(
val dependencyResult = processDependency(
resolvedDependency,
parentVisibleSourceSets[componentId.uniqueKey].orEmpty()
visibleSourceSetsFromParents[componentId.uniqueKey].orEmpty()
)
result.add(dependencyResult)
@@ -41,7 +41,7 @@ open class MetadataDependencyTransformationTask
private val transformationParameters = GranularMetadataTransformation.Params(project, kotlinSourceSet)
@get:OutputDirectory
val outputsDir: File get() = projectLayout.kotlinTransformedMetadataLibraryDirectoryForBuild(transformationParameters.sourceSetName)
internal val outputsDir: File get() = projectLayout.kotlinTransformedMetadataLibraryDirectoryForBuild(transformationParameters.sourceSetName)
@Suppress("unused") // Gradle input
@get:InputFiles
@@ -50,14 +50,18 @@ open class MetadataDependencyTransformationTask
@get:NormalizeLineEndings
internal val configurationToResolve: FileCollection = kotlinSourceSet.internal.resolvableMetadataConfiguration
@delegate:Transient // Only needed for configuring task inputs
private val participatingSourceSets: Set<KotlinSourceSet> by lazy {
@Transient // Only needed for configuring task inputs
private val participatingSourceSetsLazy: Lazy<Set<KotlinSourceSet>>? = lazy {
kotlinSourceSet.internal.withDependsOnClosure.toMutableSet().apply {
if (any { it.name == KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME })
add(project.kotlinExtension.sourceSets.getByName(KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME))
}
}
private val participatingSourceSets: Set<KotlinSourceSet> get() = participatingSourceSetsLazy?.value
?: error("`participatingSourceSets` is null. " +
"Probably it is accessed it during Task Execution with state loaded from Configuration Cache")
@Suppress("unused") // Gradle input
@get:Input
internal val inputSourceSetsAndCompilations: Map<String, Iterable<String>> by lazy {
@@ -76,30 +80,26 @@ open class MetadataDependencyTransformationTask
}
@get:OutputFile
val transformedLibrariesFileIndex: RegularFileProperty = objectFactory
internal val transformedLibrariesFileIndex: RegularFileProperty = objectFactory
.fileProperty()
.apply { set(outputsDir.resolve("${kotlinSourceSet.name}.transformedLibraries")) }
@get:OutputFile
val visibleSourceSetsFile: RegularFileProperty = objectFactory
internal val visibleSourceSetsFile: RegularFileProperty = objectFactory
.fileProperty()
.apply { set(outputsDir.resolve("${kotlinSourceSet.name}.visibleSourceSets")) }
@get:InputFiles
val parentVisibleSourceSetFiles: ConfigurableFileCollection = objectFactory
.fileCollection()
.from(
{
val parentSourceSets: List<Provider<File>> = dependsOnClosureWithInterCompilationDependencies(kotlinSourceSet).mapNotNull {
project
.tasks
.locateTask<MetadataDependencyTransformationTask>(KotlinMetadataTargetConfigurator.transformGranularMetadataTaskName(it.name))
?.flatMap { it.visibleSourceSetsFile.map { it.asFile } }
}
val parentVisibleSourceSetFiles: ConfigurableFileCollection = project.filesProvider {
val parentSourceSets: List<Provider<File>> = dependsOnClosureWithInterCompilationDependencies(kotlinSourceSet).mapNotNull {
project
.tasks
.locateTask<MetadataDependencyTransformationTask>(KotlinMetadataTargetConfigurator.transformGranularMetadataTaskName(it.name))
?.flatMap { it.visibleSourceSetsFile.map { it.asFile } }
}
parentSourceSets
}
)
parentSourceSets
}
//endregion Task Configuration State & Inputs
@@ -107,7 +107,7 @@ open class MetadataDependencyTransformationTask
fun transformMetadata() {
val transformation = GranularMetadataTransformation(
params = transformationParameters,
parentVisibleSourceSetsProvider = { parentVisibleSourceSetFiles.map(::readVisibleSourceSetsFile) }
visibleSourceSetsFromParentsProvider = { parentVisibleSourceSetFiles.map(::readVisibleSourceSetsFile) }
)
if (outputsDir.isDirectory) {
@@ -155,7 +155,7 @@ open class MetadataDependencyTransformationTask
files
}
val content = fileList.joinToString("\n")
val content = fileList.joinToString(System.lineSeparator())
transformedLibrariesFileIndex.get().asFile.writeText(content)
}
@@ -175,7 +175,9 @@ open class MetadataDependencyTransformationTask
}
@get:Internal // Warning! transformedLibraries is available only after Task Execution
val transformedLibraries: Provider<List<File>> get() = transformedLibrariesFileIndex.map { regularFile ->
regularFile.asFile.readLines().map { File(it) }
val transformedLibraries: FileCollection = project.filesProvider {
transformedLibrariesFileIndex.map { regularFile ->
regularFile.asFile.readLines().map { File(it) }
}
}
}
@@ -13,12 +13,12 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.addExtension
import org.jetbrains.kotlin.gradle.plugin.findExtension
import org.jetbrains.kotlin.gradle.plugin.extraProperties
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.toSingleKpmModuleIdentifier
import org.jetbrains.kotlin.gradle.utils.getOrPut
import org.jetbrains.kotlin.project.model.KpmModuleIdentifier
class MppDependencyProjectStructureMetadataExtractorFactory(
internal class MppDependencyProjectStructureMetadataExtractorFactory(
private val projectStructureMetadataByProjectPath: Map<String, Provider<KotlinProjectStructureMetadata?>>
) {
fun create(
@@ -41,19 +41,14 @@ class MppDependencyProjectStructureMetadataExtractorFactory(
companion object {
private val extensionName = MppDependencyProjectStructureMetadataExtractorFactory::class.java.simpleName
fun getOrCreate(project: Project): MppDependencyProjectStructureMetadataExtractorFactory {
val existing = project.findExtension<MppDependencyProjectStructureMetadataExtractorFactory>(extensionName)
if (existing != null) return existing
val projectStructureMetadataByProjectPath = collectProjectStructureMetadataFromAllProjects(project)
val newFactory = MppDependencyProjectStructureMetadataExtractorFactory(projectStructureMetadataByProjectPath)
project.addExtension(extensionName, newFactory)
return newFactory
}
fun getOrCreate(project: Project): MppDependencyProjectStructureMetadataExtractorFactory =
project.extraProperties.getOrPut(extensionName) {
val projectStructureMetadataByProjectPath = collectProjectStructureMetadataFromAllProjects(project)
MppDependencyProjectStructureMetadataExtractorFactory(projectStructureMetadataByProjectPath)
}
/**
* Collect Kotlin Project StructureMetadata only for TCS model.
* TODO: Add support for KPM and auxiliary modules
* Collect Kotlin Project StructureMetadata.
*/
private fun collectProjectStructureMetadataFromAllProjects(project: Project): Map<String, Provider<KotlinProjectStructureMetadata?>> {
return project.rootProject.allprojects.associateBy { it.path }.mapValues { (_, subProject) ->
@@ -106,7 +106,7 @@ internal fun Project.collectSourceSetMetadataOutputs(): Map<SourceSetName, Sourc
}
private fun KotlinMultiplatformExtension.sourceSetsMetadataOutputs(): Map<KotlinSourceSet, FileCollection> {
val commonTarget = targets.withType<KotlinMetadataTarget>().singleOrNull() ?: return emptyMap()
val commonTarget = metadata()
val compilations = commonTarget.compilations
@@ -39,7 +39,7 @@ private fun Project.collectAllPlatformCompilationData(): List<SourceSetVisibilit
}
private fun KotlinCompilation<*>.toPlatformCompilationData() = SourceSetVisibilityProvider.PlatformCompilationData(
sourceSets = allKotlinSourceSets.map { it.name }.toSet(),
allSourceSets = allKotlinSourceSets.map { it.name }.toSet(),
resolvedDependenciesConfiguration = LazyResolvedConfiguration(project.configurations.getByName(compileDependencyConfigurationName)),
hostSpecificMetadataConfiguration = project
.configurations
@@ -56,7 +56,7 @@ internal class SourceSetVisibilityProvider(
)
class PlatformCompilationData(
val sourceSets: Set<KotlinSourceSetName>,
val allSourceSets: Set<KotlinSourceSetName>,
val resolvedDependenciesConfiguration: LazyResolvedConfiguration,
val hostSpecificMetadataConfiguration: LazyResolvedConfiguration?
)
@@ -86,7 +86,7 @@ internal class SourceSetVisibilityProvider(
val visiblePlatformVariantNames: Set<String?> =
platformCompilations
.filter { visibleFromSourceSet in it.sourceSets }
.filter { visibleFromSourceSet in it.allSourceSets }
.mapTo(mutableSetOf()) { resolvedConfiguration ->
val resolvedVariant = resolvedConfiguration
.resolvedDependenciesConfiguration
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.statistics.metrics.BooleanMetrics
import java.util.concurrent.Callable
internal const val COMMON_MAIN_ELEMENTS_CONFIGURATION_NAME = "commonMainMetadataElements"
internal const val ALL_COMPILE_METADATA_CONFIGURATION_NAME = "allSourceSetsCompileDependenciesMetadata"
@@ -360,7 +359,7 @@ class KotlinMetadataTargetConfigurator :
compilation.compileDependencyFiles += project.filesProvider { artifacts.filterNot { it.isMpp }.map { it.file } }
// Transformed Multiplatform Libraries based on source set visibility
compilation.compileDependencyFiles += project.files(transformationTask.flatMap { it.transformedLibraries })
compilation.compileDependencyFiles += project.files(transformationTask.map { it.transformedLibraries })
if (compilation is KotlinSharedNativeCompilation && sourceSet is DefaultKotlinSourceSet) {
compilation.compileDependencyFiles += project.createCInteropMetadataDependencyClasspath(sourceSet)
@@ -388,7 +387,7 @@ class KotlinMetadataTargetConfigurator :
) {
val granularMetadataTransformation = GranularMetadataTransformation(
params = GranularMetadataTransformation.Params(project, sourceSet),
parentVisibleSourceSetsProvider = {
visibleSourceSetsFromParentsProvider = {
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId }
}
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.tasks.dependsOn
import org.jetbrains.kotlin.gradle.tasks.locateOrRegisterTask
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.library.KLIB_FILE_EXTENSION
import org.jetbrains.kotlin.project.model.KpmModuleIdentifier
@@ -199,8 +200,10 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
.apply { set(outputDirectory.resolve("${sourceSet.name}.transformedCinteropLibraries")) }
@get:Internal
val outputLibraryFiles: Provider<Set<File>> get() = outputLibrariesFileIndex.map { file ->
TransformedCinteropLibrariesFile(file.asFile).read()
val outputLibraryFiles: FileCollection = project.filesProvider {
outputLibrariesFileIndex.map { file ->
TransformedCinteropLibrariesFile(file.asFile).read()
}
}
@TaskAction
@@ -215,11 +218,6 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
TransformedCinteropLibrariesFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries)
}
private fun writeTransformedLibraries(files: Set<File>) {
val content = files.joinToString("\n")
outputLibrariesFileIndex.get().asFile.writeText(content)
}
private fun materializeMetadata(
chooseVisibleSourceSets: ChooseVisibleSourceSets
): Unit = when (chooseVisibleSourceSets.metadataProvider) {
@@ -250,7 +248,7 @@ private class TransformedCinteropLibrariesFile(
fun read(): Set<File> = indexFile.readLines().mapTo(mutableSetOf()) { File(it) }
fun write(files: Iterable<File>) {
val content = files.joinToString("\n")
val content = files.joinToString(System.lineSeparator())
indexFile.writeText(content)
}
}