[kpm] kgp-idea-proto: Add conversion from IdeaKpm.* to IdeaKpm.*Proto

^KT-52568 In Progress
This commit is contained in:
sebastian.sellmair
2022-06-13 14:13:40 +02:00
committed by Space
parent 2ecfd76bbb
commit d78fb1d4d2
18 changed files with 838 additions and 0 deletions
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
object IdeaKpmProtoSchema {
const val versionMajor = 1
const val versionMinor = 0
const val versionPatch = 0
internal val infos = listOf(
ideaKpmSchemaInfoProto {
sinceSchemaVersionMajor = 1
sinceSchemaVersionMinor = 0
sinceSchemaVersionPatch = 0
message = "Initial version of IdeaKpmProto*"
}
)
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmBinaryCoordinates
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmBinaryCoordinatesImpl
internal fun IdeaKpmBinaryCoordinatesProto(coordinates: IdeaKpmBinaryCoordinates): IdeaKpmBinaryCoordinatesProto {
return ideaKpmBinaryCoordinatesProto {
group = coordinates.group
module = coordinates.module
version = coordinates.version
coordinates.kotlinModuleName?.let { kotlinModuleName = it }
coordinates.kotlinFragmentName?.let { kotlinFragmentName = it }
}
}
internal fun IdeaKpmBinaryCoordinates(proto: IdeaKpmBinaryCoordinatesProto): IdeaKpmBinaryCoordinates {
return IdeaKpmBinaryCoordinatesImpl(
group = proto.group,
module = proto.module,
version = proto.version,
kotlinModuleName = if (proto.hasKotlinModuleName()) proto.kotlinModuleName else null,
kotlinFragmentName = if (proto.hasKotlinFragmentName()) proto.kotlinFragmentName else null
)
}
internal fun IdeaKpmBinaryCoordinates(data: ByteArray): IdeaKpmBinaryCoordinates {
return IdeaKpmBinaryCoordinates(IdeaKpmBinaryCoordinatesProto.parseFrom(data))
}
internal fun IdeaKpmBinaryCoordinates.toByteArray(): ByteArray {
return IdeaKpmBinaryCoordinatesProto(this).toByteArray()
}
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmCompilationOutput
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmCompilationOutputImpl
import java.io.File
internal fun IdeaKpmCompilationOutputProto(output: IdeaKpmCompilationOutput): IdeaKpmCompilationOutputProto {
return ideaKpmCompilationOutputProto {
classesDirs.addAll(output.classesDirs.map { it.absolutePath })
output.resourcesDir?.absolutePath?.let { this.resourcesDir = it }
}
}
internal fun IdeaKpmCompilationOutput(proto: IdeaKpmCompilationOutputProto): IdeaKpmCompilationOutput {
return IdeaKpmCompilationOutputImpl(
classesDirs = proto.classesDirsList.map { File(it) }.toSet(),
resourcesDir = if (proto.hasResourcesDir()) File(proto.resourcesDir) else null
)
}
internal fun IdeaKpmCompilationOutput(data: ByteArray): IdeaKpmCompilationOutput {
return IdeaKpmCompilationOutput(IdeaKpmCompilationOutputProto.parseFrom(data))
}
internal fun IdeaKpmCompilationOutput.toByteArray(): ByteArray {
return IdeaKpmCompilationOutputProto(this).toByteArray()
}
@@ -0,0 +1,100 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("FunctionName")
package org.jetbrains.kotlin.kpm.idea.proto
import com.google.protobuf.ByteString
import com.google.protobuf.InvalidProtocolBufferException
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProject
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import java.io.InputStream
import java.io.OutputStream
import java.nio.ByteBuffer
fun IdeaKpmSerializationContext.IdeaKpmProject(data: ByteArray): IdeaKpmProject? {
return IdeaKpmProject(data) { IdeaKpmContainerProto.parseFrom(data) }
}
fun IdeaKpmSerializationContext.IdeaKpmProject(data: ByteBuffer): IdeaKpmProject? {
return IdeaKpmProject(data) { IdeaKpmContainerProto.parseFrom(data) }
}
fun IdeaKpmSerializationContext.IdeaKpmProject(data: ByteString): IdeaKpmProject? {
return IdeaKpmProject(data) { IdeaKpmContainerProto.parseFrom(data) }
}
fun IdeaKpmSerializationContext.IdeaKpmProject(stream: InputStream): IdeaKpmProject? {
return IdeaKpmProject(stream) { IdeaKpmContainerProto.parseFrom(stream) }
}
internal fun <T> IdeaKpmSerializationContext.IdeaKpmProject(data: T, proto: (T) -> IdeaKpmContainerProto): IdeaKpmProject? {
val container = try {
proto(data)
} catch (e: InvalidProtocolBufferException) {
logger.error("Failed to deserialize IdeaKpmProject", e)
return null
}
return IdeaKpmProject(container)
}
fun IdeaKpmProject.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmContainerProto(this).toByteArray()
}
fun IdeaKpmProject.toByteString(context: IdeaKpmSerializationContext): ByteString {
return context.IdeaKpmContainerProto(this).toByteString()
}
fun IdeaKpmProject.writeTo(context: IdeaKpmSerializationContext, output: OutputStream) {
context.IdeaKpmContainerProto(this).writeDelimitedTo(output)
}
internal fun IdeaKpmSerializationContext.IdeaKpmContainerProto(project: IdeaKpmProject): IdeaKpmContainerProto {
return ideaKpmContainerProto {
schemaVersionMajor = IdeaKpmProtoSchema.versionMajor
schemaVersionMinor = IdeaKpmProtoSchema.versionMinor
schemaVersionPatch = IdeaKpmProtoSchema.versionPatch
schemaInfos.addAll(IdeaKpmProtoSchema.infos)
this.project = IdeaKpmProjectProto(project)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmProject(proto: IdeaKpmContainerProto): IdeaKpmProject? {
if (!proto.hasSchemaVersionMajor()) {
logger.error("Missing 'schema_version_major'", Throwable())
return null
}
if (!proto.hasSchemaVersionMinor()) {
logger.error("Missing 'schema_version_minor'", Throwable())
return null
}
if (proto.schemaVersionMajor > IdeaKpmProtoSchema.versionMajor) {
logger.error(
"Incompatible IdeaKpmProto* version. Received major version ${proto.schemaVersionMajor}. " +
"Supported version ${IdeaKpmProtoSchema.versionMajor}", Throwable()
)
val relevantInfos = proto.schemaInfosList.filter { info ->
info.sinceSchemaVersionMajor > IdeaKpmProtoSchema.versionMajor
}
relevantInfos.forEach { info ->
logger.error(
"Since: ${info.sinceSchemaVersionMajor}.${info.sinceSchemaVersionMinor}.${info.sinceSchemaVersionPatch}: ${info.message}"
)
}
return null
}
return IdeaKpmProject(proto.project)
}
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("FunctionName")
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.*
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import org.jetbrains.kotlin.kpm.idea.proto.IdeaKpmDependencyProto.DependencyCase
internal fun IdeaKpmSerializationContext.IdeaKpmDependencyProto(dependency: IdeaKpmDependency): IdeaKpmDependencyProto {
return ideaKpmDependencyProto {
when (dependency) {
is IdeaKpmResolvedBinaryDependency -> resolvedBinaryDependency = IdeaKpmResolvedBinaryDependencyProto(dependency)
is IdeaKpmUnresolvedBinaryDependency -> unresolvedBinaryDependency = IdeaKpmUnresolvedBinaryDependencyProto(dependency)
is IdeaKpmFragmentDependency -> fragmentDependency = IdeaKpmFragmentDependencyProto(dependency)
}
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmDependency(proto: IdeaKpmDependencyProto): IdeaKpmDependency? {
return when (proto.dependencyCase) {
DependencyCase.UNRESOLVED_BINARY_DEPENDENCY -> IdeaKpmUnresolvedBinaryDependency(proto.unresolvedBinaryDependency)
DependencyCase.RESOLVED_BINARY_DEPENDENCY -> IdeaKpmResolvedBinaryDependency(proto.resolvedBinaryDependency)
DependencyCase.FRAGMENT_DEPENDENCY -> IdeaKpmFragmentDependency(proto.fragmentDependency)
DependencyCase.DEPENDENCY_NOT_SET, null -> null
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import com.google.protobuf.ByteString
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmExtrasSerializer
import org.jetbrains.kotlin.tooling.core.Extras
import org.jetbrains.kotlin.tooling.core.toExtras
import org.jetbrains.kotlin.tooling.core.withValue
@Suppress("unchecked_cast")
internal fun IdeaKpmSerializationContext.IdeaKpmExtrasProto(extras: Extras): IdeaKpmExtrasProto {
val context = this
return ideaKpmExtrasProto {
extras.entries.forEach { (key, value) ->
val serializer = context.extrasSerializationExtension.serializer(key) ?: return@forEach
serializer as IdeaKpmExtrasSerializer<Any>
val serialized = runCatching { serializer.serialize(context, value) ?: return@forEach }.getOrElse { exception ->
logger.error("Failed to serialize $key, using ${serializer.javaClass.simpleName}", exception)
return@forEach
}
values.put(key.stableString, ByteString.copyFrom(serialized))
}
}
}
@Suppress("unchecked_cast")
internal fun IdeaKpmSerializationContext.Extras(proto: IdeaKpmExtrasProto): Extras {
return proto.valuesMap.entries.mapNotNull { (keyString, value) ->
val key = Extras.Key.fromString(keyString) as Extras.Key<Any>
val serializer = extrasSerializationExtension.serializer(key) ?: return@mapNotNull null
val deserialized = runCatching {
serializer.deserialize(this, value.toByteArray()) ?: return@mapNotNull null
}.getOrElse { exception ->
logger.error("Failed to deserialize $keyString, using ${serializer.javaClass.simpleName}", exception)
return@mapNotNull null
}
key withValue deserialized
}.toExtras()
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragment
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragmentImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmFragmentProto(fragment: IdeaKpmFragment): IdeaKpmFragmentProto {
return ideaKpmFragmentProto {
coordinates = IdeaKpmFragmentCoordinatesProto(fragment.coordinates)
platforms.addAll(fragment.platforms.map { IdeaKpmPlatformProto(it) })
languageSettings = IdeaKpmLanguageSettingsProto(fragment.languageSettings)
dependencies.addAll(fragment.dependencies.map { IdeaKpmDependencyProto(it) })
sourceDirectories.addAll(fragment.sourceDirectories.map { IdeaKpmSourceDirectoryProto(it) })
extras = IdeaKpmExtrasProto(fragment.extras)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmFragment(proto: IdeaKpmFragmentProto): IdeaKpmFragment {
return IdeaKpmFragmentImpl(
coordinates = IdeaKpmFragmentCoordinates(proto.coordinates),
platforms = proto.platformsList.map { IdeaKpmPlatform(it) }.toSet(),
languageSettings = IdeaKpmLanguageSettings(proto.languageSettings),
dependencies = proto.dependenciesList.mapNotNull { IdeaKpmDependency(it) },
sourceDirectories = proto.sourceDirectoriesList.mapNotNull { IdeaKpmSourceDirectory(it) },
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmFragment(data: ByteArray): IdeaKpmFragment {
return IdeaKpmFragment(IdeaKpmFragmentProto.parseFrom(data))
}
internal fun IdeaKpmFragment.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmFragmentProto(this).toByteArray()
}
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragmentCoordinates
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragmentCoordinatesImpl
internal fun IdeaKpmFragmentCoordinatesProto(coordinates: IdeaKpmFragmentCoordinates): IdeaKpmFragmentCoordinatesProto {
return ideaKpmFragmentCoordinatesProto {
module = IdeaKpmModuleCoordinatesProto(coordinates.module)
fragmentName = coordinates.fragmentName
}
}
internal fun IdeaKpmFragmentCoordinates(proto: IdeaKpmFragmentCoordinatesProto): IdeaKpmFragmentCoordinates {
return IdeaKpmFragmentCoordinatesImpl(
module = IdeaKpmModuleCoordinates(proto.module),
fragmentName = proto.fragmentName
)
}
internal fun IdeaKpmFragmentCoordinates(data: ByteArray): IdeaKpmFragmentCoordinates {
return IdeaKpmFragmentCoordinates(IdeaKpmFragmentCoordinatesProto.parseFrom(data))
}
internal fun IdeaKpmFragmentCoordinates.toByteArray(): ByteArray {
return IdeaKpmFragmentCoordinatesProto(this).toByteArray()
}
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragmentDependency
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmFragmentDependencyImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmFragmentDependencyProto(dependency: IdeaKpmFragmentDependency): IdeaKpmFragmentDependencyProto {
return ideaKpmFragmentDependencyProto {
type = when (dependency.type) {
IdeaKpmFragmentDependency.Type.Regular -> IdeaKpmFragmentDependencyProto.Type.REGULAR
IdeaKpmFragmentDependency.Type.Friend -> IdeaKpmFragmentDependencyProto.Type.FRIEND
IdeaKpmFragmentDependency.Type.Refines -> IdeaKpmFragmentDependencyProto.Type.REFINES
}
coordinates = IdeaKpmFragmentCoordinatesProto(dependency.coordinates)
extras = IdeaKpmExtrasProto(dependency.extras)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmFragmentDependency(proto: IdeaKpmFragmentDependencyProto): IdeaKpmFragmentDependency {
return IdeaKpmFragmentDependencyImpl(
type = when (proto.type) {
IdeaKpmFragmentDependencyProto.Type.REGULAR -> IdeaKpmFragmentDependency.Type.Regular
IdeaKpmFragmentDependencyProto.Type.FRIEND -> IdeaKpmFragmentDependency.Type.Friend
IdeaKpmFragmentDependencyProto.Type.REFINES -> IdeaKpmFragmentDependency.Type.Refines
else -> IdeaKpmFragmentDependency.Type.Regular
},
coordinates = IdeaKpmFragmentCoordinates(proto.coordinates),
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmFragmentDependency(data: ByteArray): IdeaKpmFragmentDependency {
return IdeaKpmFragmentDependency(IdeaKpmFragmentDependencyProto.parseFrom(data))
}
internal fun IdeaKpmFragmentDependency.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmFragmentDependencyProto(this).toByteArray()
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(InternalKotlinGradlePluginApi::class)
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmLanguageSettings
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmLanguageSettingsImpl
import org.jetbrains.kotlin.gradle.kpm.idea.InternalKotlinGradlePluginApi
import java.io.File
internal fun IdeaKpmLanguageSettingsProto(languageSettings: IdeaKpmLanguageSettings): IdeaKpmLanguageSettingsProto {
return ideaKpmLanguageSettingsProto {
languageSettings.languageVersion?.let { languageVersion = it }
languageSettings.apiVersion?.let { apiVersion = it }
isProgressiveMode = languageSettings.isProgressiveMode
enabledLanguageFeatures.addAll(languageSettings.enabledLanguageFeatures)
optInAnnotationsInUse.addAll(languageSettings.optInAnnotationsInUse)
compilerPluginArguments.addAll(languageSettings.compilerPluginArguments)
compilerPluginClasspath.addAll(languageSettings.compilerPluginClasspath.map { it.absolutePath })
freeCompilerArgs.addAll(languageSettings.freeCompilerArgs)
}
}
internal fun IdeaKpmLanguageSettings(proto: IdeaKpmLanguageSettingsProto): IdeaKpmLanguageSettings {
return IdeaKpmLanguageSettingsImpl(
languageVersion = if (proto.hasLanguageVersion()) proto.languageVersion else null,
apiVersion = if (proto.hasApiVersion()) proto.apiVersion else null,
isProgressiveMode = proto.isProgressiveMode,
enabledLanguageFeatures = proto.enabledLanguageFeaturesList.toSet(),
optInAnnotationsInUse = proto.optInAnnotationsInUseList.toSet(),
compilerPluginArguments = proto.compilerPluginArgumentsList.toList(),
compilerPluginClasspath = proto.compilerPluginClasspathList.map { File(it) },
freeCompilerArgs = proto.freeCompilerArgsList.toList()
)
}
internal fun IdeaKpmLanguageSettings(data: ByteArray): IdeaKpmLanguageSettings {
return IdeaKpmLanguageSettings(IdeaKpmLanguageSettingsProto.parseFrom(data))
}
internal fun IdeaKpmLanguageSettings.toByteArray(): ByteArray {
return IdeaKpmLanguageSettingsProto(this).toByteArray()
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmModule
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmModuleImpl
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmVariant
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmModuleProto(module: IdeaKpmModule): IdeaKpmModuleProto {
return ideaKpmModuleProto {
coordinates = IdeaKpmModuleCoordinatesProto(module.coordinates)
fragments.addAll(module.fragments.filter { it !is IdeaKpmVariant }.map { IdeaKpmFragmentProto(it) })
variants.addAll(module.fragments.filterIsInstance<IdeaKpmVariant>().map { IdeaKpmVariantProto(it) })
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmModule(proto: IdeaKpmModuleProto): IdeaKpmModule {
return IdeaKpmModuleImpl(
coordinates = IdeaKpmModuleCoordinates(proto.coordinates),
fragments = proto.fragmentsList.map { IdeaKpmFragment(it) } + proto.variantsList.map { IdeaKpmVariant(it) }
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmModule(data: ByteArray): IdeaKpmModule {
return IdeaKpmModule(IdeaKpmModuleProto.parseFrom(data))
}
internal fun IdeaKpmModule.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmModuleProto(this).toByteArray()
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmModuleCoordinates
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmModuleCoordinatesImpl
internal fun IdeaKpmModuleCoordinatesProto(coordinates: IdeaKpmModuleCoordinates): IdeaKpmModuleCoordinatesProto {
return ideaKpmModuleCoordinatesProto {
buildId = coordinates.buildId
projectPath = coordinates.projectPath
projectName = coordinates.projectName
moduleName = coordinates.moduleName
coordinates.moduleClassifier?.let { moduleClassifier = it }
}
}
internal fun IdeaKpmModuleCoordinates(proto: IdeaKpmModuleCoordinatesProto): IdeaKpmModuleCoordinates {
return IdeaKpmModuleCoordinatesImpl(
buildId = proto.buildId,
projectPath = proto.projectPath,
projectName = proto.projectName,
moduleName = proto.moduleName,
moduleClassifier = if (proto.hasModuleClassifier()) proto.moduleClassifier else null
)
}
internal fun IdeaKpmModuleCoordinates(data: ByteArray): IdeaKpmModuleCoordinates {
return IdeaKpmModuleCoordinates(IdeaKpmModuleCoordinatesProto.parseFrom(data))
}
internal fun IdeaKpmModuleCoordinates.toByteArray(): ByteArray {
return IdeaKpmModuleCoordinatesProto(this).toByteArray()
}
@@ -0,0 +1,148 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.*
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmPlatformProto(platform: IdeaKpmPlatform): IdeaKpmPlatformProto {
return ideaKpmPlatformProto {
when (platform) {
is IdeaKpmJsPlatformImpl -> js = IdeaKpmJsPlatformProto(platform)
is IdeaKpmJvmPlatformImpl -> jvm = IdeaKpmJvmPlatformProto(platform)
is IdeaKpmNativePlatformImpl -> native = IdeaKpmNativePlatformProto(platform)
is IdeaKpmUnknownPlatformImpl -> unknown = IdeaKpmUnknownPlatformProto(platform)
is IdeaKpmWasmPlatformImpl -> wasm = IdeaKpmWasmPlatformProto(platform)
}
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmPlatform(proto: IdeaKpmPlatformProto): IdeaKpmPlatform {
return when (proto.platformCase) {
IdeaKpmPlatformProto.PlatformCase.JVM -> IdeaKpmJvmPlatform(proto.jvm)
IdeaKpmPlatformProto.PlatformCase.NATIVE -> IdeaKpmNativePlatform(proto.native)
IdeaKpmPlatformProto.PlatformCase.JS -> IdeaKpmJsPlatform(proto.js)
IdeaKpmPlatformProto.PlatformCase.WASM -> IdeaKpmWasmPlatform(proto.wasm)
IdeaKpmPlatformProto.PlatformCase.UNKNOWN -> IdeaKpmUnknownPlatform(proto.unknown)
IdeaKpmPlatformProto.PlatformCase.PLATFORM_NOT_SET, null -> IdeaKpmUnknownPlatformImpl()
}
}
/* Jvm */
internal fun IdeaKpmSerializationContext.IdeaKpmJvmPlatformProto(platform: IdeaKpmJvmPlatform): IdeaKpmJvmPlatformProto {
return ideaKpmJvmPlatformProto {
if (platform.extras.isNotEmpty()) extras = IdeaKpmExtrasProto(platform.extras)
jvmTarget = platform.jvmTarget
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmJvmPlatform(proto: IdeaKpmJvmPlatformProto): IdeaKpmJvmPlatform {
return IdeaKpmJvmPlatformImpl(
jvmTarget = proto.jvmTarget,
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmJvmPlatform(data: ByteArray): IdeaKpmJvmPlatform {
return IdeaKpmJvmPlatform(IdeaKpmJvmPlatformProto.parseFrom(data))
}
internal fun IdeaKpmJvmPlatform.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmJvmPlatformProto(this).toByteArray()
}
/* Native */
internal fun IdeaKpmSerializationContext.IdeaKpmNativePlatformProto(platform: IdeaKpmNativePlatform): IdeaKpmNativePlatformProto {
return ideaKpmNativePlatformProto {
if (platform.extras.isNotEmpty()) extras = IdeaKpmExtrasProto(platform.extras)
konanTarget = platform.konanTarget
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmNativePlatform(proto: IdeaKpmNativePlatformProto): IdeaKpmNativePlatform {
return IdeaKpmNativePlatformImpl(
konanTarget = proto.konanTarget,
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmNativePlatform(data: ByteArray): IdeaKpmNativePlatform {
return IdeaKpmNativePlatform(IdeaKpmNativePlatformProto.parseFrom(data))
}
internal fun IdeaKpmNativePlatform.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmNativePlatformProto(this).toByteArray()
}
/* Js */
internal fun IdeaKpmSerializationContext.IdeaKpmJsPlatformProto(platform: IdeaKpmJsPlatform): IdeaKpmJsPlatformProto {
return ideaKpmJsPlatformProto {
if (platform.extras.isNotEmpty()) extras = IdeaKpmExtrasProto(platform.extras)
isIr = platform.isIr
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmJsPlatform(proto: IdeaKpmJsPlatformProto): IdeaKpmJsPlatform {
return IdeaKpmJsPlatformImpl(
isIr = proto.isIr,
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmJsPlatform(data: ByteArray): IdeaKpmJsPlatform {
return IdeaKpmJsPlatform(IdeaKpmJsPlatformProto.parseFrom(data))
}
internal fun IdeaKpmJsPlatform.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmJsPlatformProto(this).toByteArray()
}
/* Wasm */
internal fun IdeaKpmSerializationContext.IdeaKpmWasmPlatformProto(platform: IdeaKpmWasmPlatform): IdeaKpmWasmPlatformProto {
return ideaKpmWasmPlatformProto {
if (platform.extras.isNotEmpty()) extras = IdeaKpmExtrasProto(platform.extras)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmWasmPlatform(proto: IdeaKpmWasmPlatformProto): IdeaKpmWasmPlatform {
return IdeaKpmWasmPlatformImpl(
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmWasmPlatform(data: ByteArray): IdeaKpmWasmPlatform {
return IdeaKpmWasmPlatform(IdeaKpmWasmPlatformProto.parseFrom(data))
}
internal fun IdeaKpmWasmPlatform.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmWasmPlatformProto(this).toByteArray()
}
/* Unknown */
internal fun IdeaKpmSerializationContext.IdeaKpmUnknownPlatformProto(platform: IdeaKpmUnknownPlatform): IdeaKpmUnknownPlatformProto {
return ideaKpmUnknownPlatformProto {
if (platform.extras.isNotEmpty()) extras = IdeaKpmExtrasProto(platform.extras)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmUnknownPlatform(proto: IdeaKpmUnknownPlatformProto): IdeaKpmUnknownPlatform {
return IdeaKpmUnknownPlatformImpl(
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmUnknownPlatform(data: ByteArray): IdeaKpmUnknownPlatform {
return IdeaKpmUnknownPlatform(IdeaKpmUnknownPlatformProto.parseFrom(data))
}
internal fun IdeaKpmUnknownPlatform.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmUnknownPlatformProto(this).toByteArray()
}
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProject
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProjectImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import java.io.File
internal fun IdeaKpmSerializationContext.IdeaKpmProjectProto(project: IdeaKpmProject): IdeaKpmProjectProto {
return ideaKpmProjectProto {
gradlePluginVersion = project.gradlePluginVersion
coreLibrariesVersion = project.coreLibrariesVersion
project.explicitApiModeCliOption?.let { explicitApiModeCliOption = it }
kotlinNativeHome = project.kotlinNativeHome.absolutePath
modules.addAll(project.modules.map { IdeaKpmModuleProto(it) })
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmProject(proto: IdeaKpmProjectProto): IdeaKpmProject {
return IdeaKpmProjectImpl(
gradlePluginVersion = proto.gradlePluginVersion,
coreLibrariesVersion = proto.coreLibrariesVersion,
explicitApiModeCliOption = if (proto.hasExplicitApiModeCliOption()) proto.explicitApiModeCliOption else null,
kotlinNativeHome = File(proto.kotlinNativeHome),
modules = proto.modulesList.map { IdeaKpmModule(it) }
)
}
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmResolvedBinaryDependency
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmResolvedBinaryDependencyImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import java.io.File
internal fun IdeaKpmSerializationContext.IdeaKpmResolvedBinaryDependencyProto(
dependency: IdeaKpmResolvedBinaryDependency
): IdeaKpmResolvedBinaryDependencyProto {
return ideaKpmResolvedBinaryDependencyProto {
extras = IdeaKpmExtrasProto(dependency.extras)
dependency.coordinates?.let { coordinates = IdeaKpmBinaryCoordinatesProto(it) }
binaryType = dependency.binaryType
binaryFileAbsolutePath = dependency.binaryFile.absolutePath
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmResolvedBinaryDependency(
proto: IdeaKpmResolvedBinaryDependencyProto
): IdeaKpmResolvedBinaryDependency {
return IdeaKpmResolvedBinaryDependencyImpl(
coordinates = if (proto.hasCoordinates()) IdeaKpmBinaryCoordinates(proto.coordinates) else null,
binaryType = proto.binaryType,
binaryFile = File(proto.binaryFileAbsolutePath),
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmResolvedBinaryDependency(data: ByteArray): IdeaKpmResolvedBinaryDependency {
return IdeaKpmResolvedBinaryDependency(IdeaKpmResolvedBinaryDependencyProto.parseFrom(data))
}
internal fun IdeaKpmResolvedBinaryDependency.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmResolvedBinaryDependencyProto(this).toByteArray()
}
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmSourceDirectory
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmSourceDirectoryImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
import java.io.File
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
internal fun IdeaKpmSerializationContext.IdeaKpmSourceDirectoryProto(sourceDirectory: IdeaKpmSourceDirectory): IdeaKpmSourceDirectoryProto {
return ideaKpmSourceDirectoryProto {
absolutePath = sourceDirectory.file.absolutePath
type = sourceDirectory.type
extras = IdeaKpmExtrasProto(sourceDirectory.extras)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmSourceDirectory(proto: IdeaKpmSourceDirectoryProto): IdeaKpmSourceDirectory {
return IdeaKpmSourceDirectoryImpl(
file = File(proto.absolutePath),
type = proto.type,
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmSourceDirectory(data: ByteArray): IdeaKpmSourceDirectory {
return IdeaKpmSourceDirectory(IdeaKpmSourceDirectoryProto.parseFrom(data))
}
internal fun IdeaKpmSourceDirectory.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmSourceDirectoryProto(this).toByteArray()
}
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmUnresolvedBinaryDependency
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmUnresolvedBinaryDependencyImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmUnresolvedBinaryDependencyProto(
dependency: IdeaKpmUnresolvedBinaryDependency
): IdeaKpmUnresolvedBinaryDependencyProto {
return ideaKpmUnresolvedBinaryDependencyProto {
extras = IdeaKpmExtrasProto(dependency.extras)
dependency.cause?.let { cause = it }
dependency.coordinates?.let { coordinates = IdeaKpmBinaryCoordinatesProto(it) }
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmUnresolvedBinaryDependency(proto: IdeaKpmUnresolvedBinaryDependencyProto): IdeaKpmUnresolvedBinaryDependency {
return IdeaKpmUnresolvedBinaryDependencyImpl(
cause = if (proto.hasCause()) proto.cause else null,
coordinates = if (proto.hasCoordinates()) IdeaKpmBinaryCoordinates(proto.coordinates) else null,
extras = Extras(proto.extras)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmUnresolvedBinaryDependency(data: ByteArray): IdeaKpmUnresolvedBinaryDependency {
return IdeaKpmUnresolvedBinaryDependency(IdeaKpmUnresolvedBinaryDependencyProto.parseFrom(data))
}
internal fun IdeaKpmUnresolvedBinaryDependency.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmUnresolvedBinaryDependencyProto(this).toByteArray()
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kpm.idea.proto
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmVariant
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmVariantImpl
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationContext
internal fun IdeaKpmSerializationContext.IdeaKpmVariantProto(variant: IdeaKpmVariant): IdeaKpmVariantProto {
return ideaKpmVariantProto {
fragment = IdeaKpmFragmentProto(variant)
variantAttributes.putAll(variant.variantAttributes)
platform = IdeaKpmPlatformProto(variant.platform)
compilationOutput = IdeaKpmCompilationOutputProto(variant.compilationOutputs)
}
}
internal fun IdeaKpmSerializationContext.IdeaKpmVariant(proto: IdeaKpmVariantProto): IdeaKpmVariant {
return IdeaKpmVariantImpl(
fragment = IdeaKpmFragment(proto.fragment),
platform = IdeaKpmPlatform(proto.platform),
variantAttributes = proto.variantAttributesMap.toMap(),
compilationOutputs = IdeaKpmCompilationOutput(proto.compilationOutput)
)
}
internal fun IdeaKpmSerializationContext.IdeaKpmVariant(data: ByteArray): IdeaKpmVariant {
return IdeaKpmVariant(IdeaKpmVariantProto.parseFrom(data))
}
internal fun IdeaKpmVariant.toByteArray(context: IdeaKpmSerializationContext): ByteArray {
return context.IdeaKpmVariantProto(this).toByteArray()
}