Skip to content

Commit

Permalink
[MPP] SourceSetMetadataStorageForIde: Remove faulty 'cleanupStaleEntr…
Browse files Browse the repository at this point in the history
…ies'

^KT-52955 Verification Pending

This method over-aggressively removed libraries in the situation of
isolated KGP ClassLoaders or when a project enabled Gradles configuration
caching. This faulty cleanup is removed in anticipation
of a re-structured SourceSetMetadataStorageForIde in the near future.
  • Loading branch information
sellmair authored and Space committed Jun 27, 2022
1 parent a449dda commit 5c34d5b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 67 deletions.
Expand Up @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.registerDefaultVariantFactori
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupFragmentsMetadataForKpmModules
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupKpmModulesPublication
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.copyAttributes
import org.jetbrains.kotlin.gradle.plugin.sources.CleanupStaleSourceSetMetadataEntriesService
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
import org.jetbrains.kotlin.gradle.plugin.sources.SourceSetMetadataStorageForIde
import org.jetbrains.kotlin.gradle.plugin.sources.checkSourceSetVisibilityRequirements
Expand Down Expand Up @@ -126,24 +125,6 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
project.pluginManager.apply(ScriptingGradleSubplugin::class.java)

exportProjectStructureMetadataForOtherBuilds(project)

SingleActionPerBuild.run(project.rootProject, "cleanup-processed-metadata") {
if (isConfigurationCacheAvailable(project.gradle)) {
BuildEventsListenerRegistryHolder.getInstance(project).listenerRegistry.onTaskCompletion(
project.gradle.sharedServices
.registerIfAbsent(
"cleanup-stale-sourceset-metadata",
CleanupStaleSourceSetMetadataEntriesService::class.java
) {
CleanupStaleSourceSetMetadataEntriesService.configure(it, project)
}
)
} else {
project.gradle.buildFinished {
SourceSetMetadataStorageForIde.cleanupStaleEntries(project)
}
}
}
}

private fun exportProjectStructureMetadataForOtherBuilds(
Expand Down
Expand Up @@ -17,31 +17,9 @@ import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import java.io.File

object SourceSetMetadataStorageForIde {
fun cleanupStaleEntries(project: Project) {
val projectStorageDirectories = project.rootProject.allprojects
.associate { projectStorage(it) to it.multiplatformExtensionOrNull?.sourceSets.orEmpty().map { it.name } }
cleanupStaleEntries(getStorageRoot(project), projectStorageDirectories)
}

fun cleanupStaleEntries(projectStorageRoot: File, projectStorageDirectories: Map<File, List<String>>) {
projectStorageRoot.listFiles().orEmpty().filter { it.isDirectory }.forEach { directory ->
// If no project corresponds to the directory, remove the directory
if (directory !in projectStorageDirectories) {
directory.deleteRecursively()
} else {
// Under the project's directory, delete subdirectories that don't correspond to any source set:
val sourceSetNames = projectStorageDirectories.getValue(directory)
directory.listFiles().orEmpty().filter { it.isDirectory }.forEach { subdirectory ->
if (subdirectory.name !in sourceSetNames)
subdirectory.deleteRecursively()
}
}
}
}
private fun getStorageRoot(project: Project): File = project.rootDir.resolve(".gradle/kotlin/sourceSetMetadata")

internal fun getStorageRoot(project: Project): File = project.rootDir.resolve(".gradle/kotlin/sourceSetMetadata")

internal fun projectStorage(project: Project): File {
private fun projectStorage(project: Project): File {
val projectPathSegments = generateSequence(project) { it.parent }.map { it.name }
return getStorageRoot(project).resolve(
// Escape dots in project names to avoid ambiguous paths.
Expand All @@ -54,27 +32,3 @@ object SourceSetMetadataStorageForIde {
internal fun sourceSetStorageWithScope(project: Project, sourceSetName: String, scope: KotlinDependencyScope) =
sourceSetStorage(project, sourceSetName).resolve(scope.scopeName)
}

abstract class CleanupStaleSourceSetMetadataEntriesService : BuildService<CleanupStaleSourceSetMetadataEntriesService.Parameters>, AutoCloseable, OperationCompletionListener {
interface Parameters : BuildServiceParameters {
val projectStorageRoot: Property<File>
val projectStorageDirectories: MapProperty<File, List<String>>
}

override fun onFinish(event: FinishEvent?) {
// noop
}

override fun close() {
SourceSetMetadataStorageForIde.cleanupStaleEntries(parameters.projectStorageRoot.get(), parameters.projectStorageDirectories.get())
}

companion object {
fun configure(spec: BuildServiceSpec<Parameters>, project: Project) {
spec.parameters.projectStorageRoot.set(SourceSetMetadataStorageForIde.getStorageRoot(project))
spec.parameters.projectStorageDirectories.set(project.rootProject.allprojects.associate {
SourceSetMetadataStorageForIde.projectStorage(it) to it.multiplatformExtensionOrNull?.sourceSets.orEmpty().map { it.name }
})
}
}
}

0 comments on commit 5c34d5b

Please sign in to comment.