Wizard: wrap property into PluginProperty for plugin properties

This commit is contained in:
Ilya Kirillov
2020-07-22 17:01:53 +03:00
committed by Kirill Shmakov
parent 3c3ba361e8
commit 74d6919c7c
4 changed files with 54 additions and 25 deletions
@@ -109,7 +109,7 @@ class Context private constructor(
fun <S : WizardService> serviceByClass(klass: KClass<S>, filter: (S) -> Boolean = { true }): S =
servicesManager.serviceByClass(klass, filter) ?: error("Service ${klass.simpleName} was not found")
val <T : Any> Property<T>.propertyValue: T
val <T : Any> PluginProperty<T>.propertyValue: T
get() = propertyContext[this] ?: error("No value is present for property `$this`")
val <V : Any, T : SettingType<V>> SettingReference<V, T>.settingValue: V
@@ -169,18 +169,18 @@ class Context private constructor(
return action(this@Writer, value)
}
fun <T : Any> Property<T>.update(
fun <T : Any> PluginProperty<T>.update(
updater: suspend ComputeContext<*>.(T) -> TaskResult<T>
): TaskResult<Unit> = compute {
val (newValue) = updater(propertyValue)
propertyContext[this@update] = newValue
}
fun <T : Any> Property<List<T>>.addValues(
fun <T : Any> PluginProperty<List<T>>.addValues(
vararg values: T
): TaskResult<Unit> = update { oldValues -> success(oldValues + values) }
fun <T : Any> Property<List<T>>.addValues(
fun <T : Any> PluginProperty<List<T>>.addValues(
values: List<T>
): TaskResult<Unit> = update { oldValues -> success(oldValues + values) }
@@ -1,5 +1,6 @@
package org.jetbrains.kotlin.tools.projectWizard.core
import org.jetbrains.kotlin.tools.projectWizard.PropertiesOwner
import org.jetbrains.kotlin.tools.projectWizard.SettingsOwner
import org.jetbrains.kotlin.tools.projectWizard.core.entity.*
import org.jetbrains.kotlin.tools.projectWizard.core.entity.settings.*
@@ -28,29 +29,43 @@ abstract class Plugin(override val context: Context) : EntityBase(),
abstract val pipelineTasks: List<PipelineTask>
}
abstract class PluginSettingsOwner : SettingsOwner {
abstract class PluginSettingsOwner : SettingsOwner, PropertiesOwner {
abstract val pluginPath: String
// properties
override fun <T : Any> propertyDelegate(
create: (path: String) -> PropertyBuilder<T>
): ReadOnlyProperty<Any, Property<T>> =
cached { name -> PluginProperty(create(withPluginPath(name)).build()) }
@Suppress("UNCHECKED_CAST")
override fun <T : Any> property(
defaultValue: T,
init: PropertyBuilder<T>.() -> Unit,
): ReadOnlyProperty<Any, PluginProperty<T>> =
super.property(defaultValue, init) as ReadOnlyProperty<Any, PluginProperty<T>>
override fun <T : Any> listProperty(
vararg defaultValues: T,
init: PropertyBuilder<List<T>>.() -> Unit,
): ReadOnlyProperty<Any, PluginProperty<List<T>>> =
property(defaultValues.toList(), init)
// pippeline tasks
fun pipelineTask(
phase: GenerationPhase,
init: PipelineTask.Builder.() -> Unit
): ReadOnlyProperty<Any, PipelineTask> =
cached { name -> PipelineTask.Builder(withPluginPath(name), phase).apply(init).build() }
// task1
fun <A, B : Any> task1(
init: Task1.Builder<A, B>.() -> Unit
): ReadOnlyProperty<Any, Task1<A, B>> = cached { name -> Task1.Builder<A, B>(withPluginPath(name)).apply(init).build() }
fun <T : Any> property(
defaultValue: T,
init: Property.Builder<T>.() -> Unit = {}
): ReadOnlyProperty<Any, Property<T>> =
cached { name -> Property.Builder(withPluginPath(name), defaultValue).apply(init).build() }
fun <T : Any> listProperty(vararg defaultValues: T, init: Property.Builder<List<T>>.() -> Unit = {}) =
property(defaultValues.toList(), init)
private fun withPluginPath(name: String): String = "$pluginPath.$name"
// settings
override fun <V : Any, T : SettingType<V>> settingDelegate(
create: (path: String) -> SettingBuilder<V, T>
@@ -151,5 +166,9 @@ abstract class PluginSettingsOwner : SettingsOwner {
crossinline init: DropDownSettingType.Builder<E>.() -> Unit = {}
): ReadOnlyProperty<Any, PluginSetting<E, DropDownSettingType<E>>> where E : Enum<E>, E : DisplayableSettingItem =
enumSettingImpl(title, neededAtPhase, init) as ReadOnlyProperty<Any, PluginSetting<E, DropDownSettingType<E>>>
// utils
private fun withPluginPath(name: String): String = "$pluginPath.$name"
}
@@ -2,14 +2,24 @@ package org.jetbrains.kotlin.tools.projectWizard.core.entity
class PropertyContext : ValuedEntityContext<Property<Any>>()
data class Property<out T : Any>(
override val path: String,
interface Property<out T : Any> : Entity {
val defaultValue: T
) : EntityWithValue<T>() {
class Builder<T : Any>(
private val name: String,
private val defaultValue: T
) {
fun build(): Property<T> = Property(name, defaultValue)
}
}
data class InternalProperty<out T : Any>(
override val path: String,
override val defaultValue: T,
) : Property<T>, EntityWithValue<T>()
sealed class PropertyImpl<out T : Any> : Property<T>
class PluginProperty<out T : Any>(
internal: Property<T>
) : PropertyImpl<T>(), Property<T> by internal
class PropertyBuilder<T : Any>(
private val name: String,
private val defaultValue: T
) {
fun build(): Property<T> = InternalProperty(name, defaultValue)
}
@@ -1,6 +1,6 @@
package org.jetbrains.kotlin.tools.projectWizard.core.entity
abstract class ValuedEntityContext<E : EntityWithValue<Any>> {
abstract class ValuedEntityContext<E : Entity> {
private val values = mutableMapOf<String, Any>()
@Suppress("UNCHECKED_CAST")