Skip to content

Commit

Permalink
Fix id collision by adding DirectoryPath to modules Identity (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
fscarponi authored Feb 27, 2024
1 parent b31beeb commit 111db62
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.jetbrains.packagesearch.plugin.core.data

import com.jetbrains.packagesearch.plugin.core.utils.DirectoryPath
import java.nio.file.Path
import kotlinx.serialization.Serializable
import org.jetbrains.packagesearch.api.v3.ApiRepository
Expand Down Expand Up @@ -42,5 +43,9 @@ sealed interface PackageSearchModule : IconProvider, PackageSearchModuleEditor {
}

@Serializable
data class Identity(val group: String, val path: String)
}
data class Identity(
val group: String,
val path: String,
val projectDir: DirectoryPath,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jetbrains.packagesearch.plugin.core.utils

import java.nio.file.Path
import kotlin.io.path.isDirectory
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

fun Path.toDirectory() = DirectoryPath(this)

@Serializable(with = DirectoryPath.Companion::class)
class DirectoryPath(path: Path) : Path by path {
init {
require(path.isDirectory()) { "Path $path is not a directory" }
}

companion object : KSerializer<DirectoryPath> {
override val descriptor: SerialDescriptor
get() = NioPathSerializer.descriptor

override fun deserialize(decoder: Decoder): DirectoryPath =
NioPathSerializer.deserialize(decoder).toDirectory()

override fun serialize(encoder: Encoder, value: DirectoryPath) {
NioPathSerializer.serialize(encoder, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ object NioPathSerializer : KSerializer<Path> {
String.serializer().serialize(encoder, value.absolutePathString())
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.module.Module
import com.jetbrains.packagesearch.plugin.core.PackageSearch
import com.jetbrains.packagesearch.plugin.core.data.PackageSearchModule
import com.jetbrains.packagesearch.plugin.core.extensions.PackageSearchModuleBuilderContext
import com.jetbrains.packagesearch.plugin.core.utils.toDirectory
import com.jetbrains.packagesearch.plugin.gradle.utils.getDeclaredDependencies
import com.jetbrains.packagesearch.plugin.gradle.utils.getDeclaredKnownRepositories
import kotlinx.coroutines.flow.FlowCollector
Expand Down Expand Up @@ -37,7 +38,8 @@ class GradleModuleProvider : AbstractGradleModuleProvider() {
name = model.projectName,
identity = PackageSearchModule.Identity(
group = "gradle",
path = model.projectIdentityPath
path = model.projectIdentityPath,
projectDir = model.projectDir.toDirectory(),
),
buildFilePath = model.buildFilePath,
declaredKnownRepositories = module.getDeclaredKnownRepositories(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.jetbrains.packagesearch.plugin.core.data.IconProvider.Icons
import com.jetbrains.packagesearch.plugin.core.data.PackageSearchModule
import com.jetbrains.packagesearch.plugin.core.extensions.PackageSearchModuleBuilderContext
import com.jetbrains.packagesearch.plugin.core.utils.icon
import com.jetbrains.packagesearch.plugin.core.utils.toDirectory
import com.jetbrains.packagesearch.plugin.gradle.utils.getDeclaredDependencies
import com.jetbrains.packagesearch.plugin.gradle.utils.toGradleDependencyModel
import kotlinx.coroutines.async
Expand Down Expand Up @@ -48,7 +49,8 @@ class KotlinMultiplatformModuleProvider : AbstractGradleModuleProvider() {
name = model.projectName,
identity = PackageSearchModule.Identity(
group = "gradle",
path = model.projectIdentityPath
path = model.projectIdentityPath,
projectDir = model.projectDir.toDirectory(),
),
buildFilePath = model.buildFilePath,
declaredKnownRepositories = knownRepositories - DependencyModifierService
Expand Down Expand Up @@ -137,11 +139,12 @@ class KotlinMultiplatformModuleProvider : AbstractGradleModuleProvider() {
gradlePackages {
kotlinMultiplatform {
compilationTargets.forEach { compilationTarget ->
when {
when {
compilationTarget is Js -> when (compilationTarget.compiler) {
Js.Compiler.IR -> jsIr()
Js.Compiler.LEGACY -> jsLegacy()
}

compilationTarget is Native -> native(compilationTarget.target)
compilationTarget == MppCompilationInfoModel.Wasm -> wasm()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import com.jetbrains.packagesearch.plugin.core.utils.flow
import com.jetbrains.packagesearch.plugin.core.utils.icon
import com.jetbrains.packagesearch.plugin.core.utils.mapUnit
import com.jetbrains.packagesearch.plugin.core.utils.registryFlow
import com.jetbrains.packagesearch.plugin.core.utils.toDirectory
import com.jetbrains.packagesearch.plugin.core.utils.watchExternalFileChanges
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.Path
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filter
Expand Down Expand Up @@ -104,13 +106,15 @@ suspend fun Module.toPackageSearch(
mavenProject: MavenProject,
): PackageSearchMavenModule {
val declaredDependencies = getDeclaredDependencies()
val pomPath = Path(mavenProject.file.path)
return PackageSearchMavenModule(
name = mavenProject.name ?: name,
identity = PackageSearchModule.Identity(
group = "maven",
path = buildMavenParentHierarchy(mavenProject.file.asRegularFile())
path = buildMavenParentHierarchy(mavenProject.file.asRegularFile()),
projectDir = pomPath.parent.toDirectory(),
),
buildFilePath = Paths.get(mavenProject.file.path),
buildFilePath = pomPath,
declaredKnownRepositories = getDeclaredKnownRepositories(),
declaredDependencies = declaredDependencies,
availableScopes = commonScopes.plus(declaredDependencies.mapNotNull { it.declaredScope }).distinct(),
Expand Down Expand Up @@ -159,7 +163,7 @@ suspend fun Module.getDeclaredDependencies(): List<PackageSearchDeclaredBaseMave
.mapNotNull { (packageId, declaredDependency) ->
PackageSearchDeclaredBaseMavenPackage(
id = packageId,
declaredVersion = declaredDependency.version?.let { NormalizedVersion.fromStringOrNull(it ) },
declaredVersion = declaredDependency.version?.let { NormalizedVersion.fromStringOrNull(it) },
remoteInfo = remoteInfo[packageId]?.asMavenApiPackage(),
groupId = declaredDependency.groupId,
artifactId = declaredDependency.artifactId,
Expand Down

0 comments on commit 111db62

Please sign in to comment.