Skip to content

Commit

Permalink
Refactor Kotlin Multiplatform Plugin (#64)
Browse files Browse the repository at this point in the history
Refactored the Kotlin Multiplatform Plugin and added plugin parameters in a generated static class. Changes include modifications and relocations of several utilities methods related to KMP attributes and conditions. Environment variables have been introduced to control runtime behavior.
  • Loading branch information
lamba92 authored Feb 12, 2024
1 parent 5a9173f commit 6997fb3
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 57 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ jobs:
MAVEN_SPACE_USERNAME: ${{ secrets.MAVEN_SPACE_USERNAME }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
KMP: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build Plugin" type="GradleRunConfiguration" factoryName="Gradle">
<configuration default="false" name="[KMP] Package Search Plugin" type="GradleRunConfiguration" factoryName="Gradle">
<log_file alias="idea.log" path="$PROJECT_DIR$/plugin/build/idea-sandbox/system/log/idea.log"/>
<ExternalSystemSettings>
<option name="env">
<map>
<entry key="KMP" value="true"/>
</map>
</option>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
Expand All @@ -10,7 +16,7 @@
</option>
<option name="taskNames">
<list>
<option value=":plugin:buildShadowPlugin" />
<option value=":plugin:runIde"/>
</list>
</option>
<option name="vmOptions" />
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ packages from the following package managers by default:

It also supports Kotlin Multiplatform projects for both for Gradle and Amper.

![Package Search](https://plugins.jetbrains.com/files/12507/screenshot_2db7914e-4a6a-45a1-aa34-ed00b150cf62)
![Package Search](https://plugins.jetbrains.com/files/12507/screenshot_26124d52-4baf-4e5c-bff3-1ecb81efd83c)
![Package Search](https://plugins.jetbrains.com/files/12507/screenshot_2cd70867-8304-496a-a023-a052b01e24f6)
![Package Search](https://plugins.jetbrains.com/files/12507/screenshot_2314f197-0e0a-4b45-bdbe-23d2bf745df2)

# Installation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import javax.inject.Inject
import kotlin.math.max
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.the

open class GeneratePackageSearchObject @Inject constructor(objects: ObjectFactory) : DefaultTask() {

Expand All @@ -22,11 +22,21 @@ open class GeneratePackageSearchObject @Inject constructor(objects: ObjectFactor

@get:Input
val pluginVersion = objects.property<String>()
.convention(project.version.toString())
.convention(project.provider {
val runNumber = System.getenv("RUN_NUMBER")?.toInt() ?: 0
val runAttempt = System.getenv("RUN_ATTEMPT")?.toInt() ?: 0
val snapshotMinorVersion = max(0, runNumber + runAttempt - 1)
val versionString = project.version.toString()
versionString.replace("-SNAPSHOT", ".$snapshotMinorVersion")
})

@get:Input
val deleteCachesOnStartup = objects.property<Boolean>()
.convention(System.getenv("CI") != "true")
.convention(project.provider { System.getenv("CI") != "true" })

@get:Input
val KMPEnabled = objects.property<Boolean>()
.convention(project.provider { System.getenv("KMP") == "true" })

@get:Input
val packageName = objects.property<String>()
Expand Down Expand Up @@ -71,6 +81,15 @@ open class GeneratePackageSearchObject @Inject constructor(objects: ObjectFactor
)
.build()
)
.addProperty(
PropertySpec.builder("isKMPEnabled", Boolean::class)
.getter(
FunSpec.getterBuilder()
.addStatement("return ${KMPEnabled.get()}")
.build()
)
.build()
)
.build()
)
.build()
Expand Down
7 changes: 1 addition & 6 deletions plugin/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,14 @@ kotlin.sourceSets.main {

val pkgsPluginId: String by project

val runNumber = System.getenv("RUN_NUMBER")?.toInt() ?: 0
val runAttempt = System.getenv("RUN_ATTEMPT")?.toInt() ?: 0
val snapshotMinorVersion = max(0, runNumber + runAttempt - 1)
val versionString = project.version.toString()

tasks {
withType<Test> {
environment("DB_PATH", layout.buildDirectory.file("tests/cache.db").get().asFile.absolutePath)
}
val generatePluginDataSources by registering(GeneratePackageSearchObject::class) {
group = "generate"
pluginId = pkgsPluginId
outputDir = generatedDir
pluginVersion = versionString.replace("-SNAPSHOT", ".$snapshotMinorVersion")
packageName = "com.jetbrains.packagesearch.plugin.core"
}
sourcesJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jetbrains.packagesearch.plugin.core.utils

import com.jetbrains.packagesearch.plugin.core.data.PackageSearchModuleVariant


data class KmpAttributesGroups(
val displayName: String,
val aggregationKeyword: String = displayName,
)

val KMP_ATTRIBUTES_GROUPS = listOf(
KmpAttributesGroups("iOS"),
KmpAttributesGroups("macOS"),
KmpAttributesGroups("tvOS"),
KmpAttributesGroups("watchOS"),
KmpAttributesGroups("JS"),
KmpAttributesGroups("JVM"),
KmpAttributesGroups("Linux"),
KmpAttributesGroups("Android"),
KmpAttributesGroups("WASM"),
KmpAttributesGroups("Windows", "mingw"),
)

fun Set<String>.parseAttributesFromRawStrings() = buildList {
var queue = this@parseAttributesFromRawStrings.toList()
for (attributeTitle in KMP_ATTRIBUTES_GROUPS) {
val (targets, rest) = queue
.partition { it.contains(attributeTitle.aggregationKeyword, true) }

if (targets.isEmpty()) continue
this.add(
PackageSearchModuleVariant.Attribute.NestedAttribute(
attributeTitle.displayName,
targets.map { PackageSearchModuleVariant.Attribute.StringAttribute(it) })
)
queue = rest
}
addAll(queue.map { PackageSearchModuleVariant.Attribute.StringAttribute(it) })

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.jetbrains.packagesearch.plugin.gradle

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.gradle.utils.getDeclaredDependencies
Expand All @@ -20,7 +21,7 @@ class GradleModuleProvider : AbstractGradleModuleProvider() {
module: Module,
model: PackageSearchGradleModel,
) {
if (!model.isKotlinMultiplatformApplied) {
if (!PackageSearch.isKMPEnabled || !model.isKotlinMultiplatformApplied) {
val availableKnownRepositories =
model.repositories.toSet().let { availableGradleRepositories ->
knownRepositories.filterValues {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.packageSearch.mppDependencyUpdater.resolved.MppCompilationIn
import com.jetbrains.packagesearch.plugin.core.data.EditModuleContext
import com.jetbrains.packagesearch.plugin.core.data.PackageSearchDeclaredPackage
import com.jetbrains.packagesearch.plugin.core.data.PackageSearchModuleVariant
import com.jetbrains.packagesearch.plugin.core.utils.parseAttributesFromRawStrings
import kotlin.contracts.contract

fun Set<MppCompilationInfoModel.Compilation>.buildAttributes(): List<PackageSearchModuleVariant.Attribute> {
Expand Down Expand Up @@ -37,25 +38,6 @@ fun List<PackageSearchModuleVariant.Attribute>.mergeAttributes() =
.toSet()
.parseAttributesFromRawStrings()

fun Set<String>.parseAttributesFromRawStrings() = buildList {
var queue = this@parseAttributesFromRawStrings.toList()
for (attributeTitle in KMP_ATTRIBUTES_GROUPS) {
val (targets, rest) = queue
.partition { it.contains(attributeTitle.aggregationKeyword, true) }

if (targets.isEmpty()) continue
this.add(
PackageSearchModuleVariant.Attribute.NestedAttribute(
attributeTitle.displayName,
targets.map { PackageSearchModuleVariant.Attribute.StringAttribute(it) })
)
queue = rest
}
addAll(queue.map { PackageSearchModuleVariant.Attribute.StringAttribute(it) })

}


operator fun Set<String>.contains(attribute: PackageSearchModuleVariant.Attribute.NestedAttribute): Boolean =
attribute.flatten().all { it in this }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.packageSearch.mppDependencyUpdater.resolved.MppCompilationIn
import com.intellij.packageSearch.mppDependencyUpdater.resolved.MppCompilationInfoModel.Jvm
import com.intellij.packageSearch.mppDependencyUpdater.resolved.MppCompilationInfoModel.Native
import com.intellij.packageSearch.mppDependencyUpdater.resolved.MppCompilationInfoProvider
import com.jetbrains.packagesearch.plugin.core.PackageSearch
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
Expand All @@ -34,7 +35,7 @@ class KotlinMultiplatformModuleProvider : AbstractGradleModuleProvider() {
module: Module,
model: PackageSearchGradleModel,
) {
if (model.isKotlinMultiplatformApplied && !model.isAmperApplied)
if (PackageSearch.isKMPEnabled && model.isKotlinMultiplatformApplied && !model.isAmperApplied)
MppCompilationInfoProvider.sourceSetsMap(project, model.projectDir)
.collect { compilationModel ->
val variants = module.getKMPVariants(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import org.jetbrains.packagesearch.api.v3.ApiMavenPackage
import org.jetbrains.packagesearch.api.v3.ApiPackage
import org.jetbrains.packagesearch.api.v3.ApiRepository
import org.jetbrains.packagesearch.packageversionutils.normalization.NormalizedVersion
Expand Down Expand Up @@ -98,11 +99,12 @@ suspend fun Module.getDeclaredDependencies(): List<PackageSearchGradleDeclaredPa
val remoteInfo = getPackageInfoByIdHashes(distinctIds.map { ApiPackage.hashPackageId(it) }.toSet())

return declaredDependencies
.map { declaredDependency ->
.mapNotNull { declaredDependency ->
PackageSearchGradleDeclaredPackage(
id = declaredDependency.packageId,
declaredVersion = declaredDependency.version?.let { NormalizedVersion.fromStringOrNull(it) },
remoteInfo = remoteInfo[declaredDependency.packageId]?.asMavenApiPackage(),
remoteInfo = remoteInfo[declaredDependency.packageId] as? ApiMavenPackage
?: return@mapNotNull null,
icon = remoteInfo[declaredDependency.packageId]?.icon
?: IconProvider.Icons.MAVEN,
module = declaredDependency.groupId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.jetbrains.packagesearch.plugin.core.data.PackageSearchDeclaredPackage
import com.jetbrains.packagesearch.plugin.core.data.listKMPAttributesNames
import com.jetbrains.packagesearch.plugin.core.extensions.PackageSearchKnownRepositoriesContext
import com.jetbrains.packagesearch.plugin.core.utils.icon
import com.jetbrains.packagesearch.plugin.gradle.parseAttributesFromRawStrings
import com.jetbrains.packagesearch.plugin.core.utils.parseAttributesFromRawStrings
import com.jetbrains.packagesearch.plugin.ui.model.getLatestVersion
import org.jetbrains.packagesearch.api.v3.ApiMavenPackage
import org.jetbrains.packagesearch.api.v3.ApiPackage
Expand Down

0 comments on commit 6997fb3

Please sign in to comment.