Skip to content

Commit

Permalink
implement shared properties mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Morfly committed Oct 29, 2023
1 parent 8321111 commit 251ce10
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 5 deletions.
12 changes: 11 additions & 1 deletion airin-core/src/jvmMain/kotlin/io/morfly/airin/Component.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package io.morfly.airin

abstract class Component<P : PackageDescriptor> : HasId, PropertiesHolder {
import io.morfly.airin.label.MavenCoordinates

abstract class Component<P : PackageDescriptor> : HasId, PropertiesHolder, SharedPropertiesHolder {

final override val properties = mutableMapOf<String, Any?>()

final override lateinit var sharedProperties: MutableMap<String, Any?>
internal set

override val sharedPropertiesAvailable
get() = ::sharedProperties.isInitialized

val allMavenArtifacts: MutableSet<MavenCoordinates> by sharedProperty(mutableSetOf())

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Component<*>) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class DependencyOverrideContext {
}

sealed interface DependencyOverride {

data class Override(val label: Label, val configuration: String? = null) : DependencyOverride

data object Ignore : DependencyOverride
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class MigrationProcessor<P : PackageDescriptor>(
}
traverse(packageDescriptor)

// Processing packages following the order in which components were registered.
// Processing packages, following the order in which components were registered.
val sharedProperties = mutableMapOf<String, Any?>()
for ((id, component) in components) {
component.sharedProperties = sharedProperties

for (pkg in packages[id].orEmpty()) {
val result = component.invoke(pkg)
writeGeneratedFiles(pkg.dirPath, result.starlarkFiles)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.morfly.airin

import io.morfly.airin.label.MavenCoordinates
import io.morfly.pendant.starlark.lang.append

abstract class PackageComponent<P : PackageDescriptor> : Component<P>(), PropertiesHolder,
Expand All @@ -20,10 +21,14 @@ abstract class PackageComponent<P : PackageDescriptor> : Component<P>(), Propert
val features = subcomponents.values.asSequence()
.filterIsInstance<FeatureComponent<P>>()
.filter { it.id in packageDescriptor.featureComponentIds }
.onEach { it.sharedProperties = sharedProperties }
.map { it.invoke(packageDescriptor) }
.toList()

packageDescriptor.applyDependencies(packageDescriptor.transformDependencies(features))
allMavenArtifacts += packageDescriptor.dependencies.values.flatten()
.filterIsInstance<MavenCoordinates>()

context.onInvoke(packageDescriptor)

for (file in context.starlarkFiles.values.flatten()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fun PackageDescriptor.transformDependencies(features: List<FeatureContext>): Map

val processedDependencies = mutableMapOf<ConfigurationName, MutableSet<Label>>()

// Process dependency overrides
for (feature in features) {
for ((configuration, labels) in this.originalDependencies) {
for (dependency in labels) {
Expand All @@ -54,6 +55,7 @@ fun PackageDescriptor.transformDependencies(features: List<FeatureContext>): Map
}
}

// Process configuration overrides for the remaining dependencies
for (feature in features) {
for ((configuration, labels) in this.originalDependencies) {
for (dependency in labels) {
Expand All @@ -62,8 +64,7 @@ fun PackageDescriptor.transformDependencies(features: List<FeatureContext>): Map
val configOverride = feature.configurationOverrides[configuration]
val config = configOverride?.configuration
?: continue
val dep = dependency.asComparable()
transformedDependencies.getOrPut(config, ::mutableSetOf) += dep
transformedDependencies.getOrPut(config, ::mutableSetOf) += dependency
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.morfly.airin

interface SharedPropertiesHolder {

val sharedProperties: MutableMap<String, Any?>

val sharedPropertiesAvailable: Boolean
}

//@Suppress("UNCHECKED_CAST")
//val MutableMap<String, Any?>.allMavenArtifacts: MutableSet<MavenCoordinates>
// get() {
// val artifacts = getOrPut("allMavenArtifacts") { mutableSetOf<MavenCoordinates>() }
// return artifacts as MutableSet<MavenCoordinates>
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.morfly.airin

import java.io.Serializable
import kotlin.reflect.KProperty

class SharedPropertyDelegate<V : Any>(val defaultValue: V) : Serializable {

operator fun provideDelegate(
holder: SharedPropertiesHolder, property: KProperty<*>
): SharedPropertyDelegate<V> = this

operator fun getValue(holder: SharedPropertiesHolder, property: KProperty<*>): V {
validate(holder, property.name)
@Suppress("UNCHECKED_CAST")
return holder.sharedProperties.getOrPut(property.name) { defaultValue } as V
}

operator fun setValue(holder: SharedPropertiesHolder, property: KProperty<*>, value: V) {
validate(holder, property.name)
holder.sharedProperties[property.name] = value
}

private fun validate(holder: SharedPropertiesHolder, property: String) {
require(holder.sharedPropertiesAvailable) {
"Shared property $property is not available during build configuration phase!"
}
}
}

fun <V : Any> SharedPropertiesHolder.sharedProperty(default: V): SharedPropertyDelegate<V> {
return SharedPropertyDelegate(default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fun FunctionCallContext.applyDependenciesFrom(packageDescriptor: GradleProject)
for ((configuration, dependencies) in packageDescriptor.dependencies) {
configuration `=` dependencies.mapNotNull {
when (it) {
is MavenCoordinates -> artifact(it.copy(version = null).toString())
is MavenCoordinates -> artifact(it.asComparable().toString())
else -> it.asBazelLabel()?.toString()
}
}
Expand Down

0 comments on commit 251ce10

Please sign in to comment.