diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Context.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Context.kt index 2955ae214d7..55bbc38625d 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Context.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Context.kt @@ -109,7 +109,7 @@ class Context private constructor( fun serviceByClass(klass: KClass, filter: (S) -> Boolean = { true }): S = servicesManager.serviceByClass(klass, filter) ?: error("Service ${klass.simpleName} was not found") - val Property.propertyValue: T + val PluginProperty.propertyValue: T get() = propertyContext[this] ?: error("No value is present for property `$this`") val > SettingReference.settingValue: V @@ -169,18 +169,18 @@ class Context private constructor( return action(this@Writer, value) } - fun Property.update( + fun PluginProperty.update( updater: suspend ComputeContext<*>.(T) -> TaskResult ): TaskResult = compute { val (newValue) = updater(propertyValue) propertyContext[this@update] = newValue } - fun Property>.addValues( + fun PluginProperty>.addValues( vararg values: T ): TaskResult = update { oldValues -> success(oldValues + values) } - fun Property>.addValues( + fun PluginProperty>.addValues( values: List ): TaskResult = update { oldValues -> success(oldValues + values) } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Plugin.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Plugin.kt index 193e8537d9d..671b1eeb07a 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Plugin.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Plugin.kt @@ -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 } -abstract class PluginSettingsOwner : SettingsOwner { +abstract class PluginSettingsOwner : SettingsOwner, PropertiesOwner { abstract val pluginPath: String + // properties + override fun propertyDelegate( + create: (path: String) -> PropertyBuilder + ): ReadOnlyProperty> = + cached { name -> PluginProperty(create(withPluginPath(name)).build()) } + + @Suppress("UNCHECKED_CAST") + override fun property( + defaultValue: T, + init: PropertyBuilder.() -> Unit, + ): ReadOnlyProperty> = + super.property(defaultValue, init) as ReadOnlyProperty> + + override fun listProperty( + vararg defaultValues: T, + init: PropertyBuilder>.() -> Unit, + ): ReadOnlyProperty>> = + property(defaultValues.toList(), init) + + // pippeline tasks + fun pipelineTask( phase: GenerationPhase, init: PipelineTask.Builder.() -> Unit ): ReadOnlyProperty = cached { name -> PipelineTask.Builder(withPluginPath(name), phase).apply(init).build() } + // task1 + fun task1( init: Task1.Builder.() -> Unit ): ReadOnlyProperty> = cached { name -> Task1.Builder(withPluginPath(name)).apply(init).build() } - fun property( - defaultValue: T, - init: Property.Builder.() -> Unit = {} - ): ReadOnlyProperty> = - cached { name -> Property.Builder(withPluginPath(name), defaultValue).apply(init).build() } - - fun listProperty(vararg defaultValues: T, init: Property.Builder>.() -> Unit = {}) = - property(defaultValues.toList(), init) - - private fun withPluginPath(name: String): String = "$pluginPath.$name" + // settings override fun > settingDelegate( create: (path: String) -> SettingBuilder @@ -151,5 +166,9 @@ abstract class PluginSettingsOwner : SettingsOwner { crossinline init: DropDownSettingType.Builder.() -> Unit = {} ): ReadOnlyProperty>> where E : Enum, E : DisplayableSettingItem = enumSettingImpl(title, neededAtPhase, init) as ReadOnlyProperty>> + + // utils + + private fun withPluginPath(name: String): String = "$pluginPath.$name" } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Property.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Property.kt index ef01a0764d8..e9445d6b70c 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Property.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Property.kt @@ -2,14 +2,24 @@ package org.jetbrains.kotlin.tools.projectWizard.core.entity class PropertyContext : ValuedEntityContext>() -data class Property( - override val path: String, +interface Property : Entity { val defaultValue: T -) : EntityWithValue() { - class Builder( - private val name: String, - private val defaultValue: T - ) { - fun build(): Property = Property(name, defaultValue) - } } + +data class InternalProperty( + override val path: String, + override val defaultValue: T, +) : Property, EntityWithValue() + +sealed class PropertyImpl : Property + +class PluginProperty( + internal: Property +) : PropertyImpl(), Property by internal + +class PropertyBuilder( + private val name: String, + private val defaultValue: T +) { + fun build(): Property = InternalProperty(name, defaultValue) +} \ No newline at end of file diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/ValuedEntityContext.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/ValuedEntityContext.kt index f1776af2bf4..e27801a1ecb 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/ValuedEntityContext.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/ValuedEntityContext.kt @@ -1,6 +1,6 @@ package org.jetbrains.kotlin.tools.projectWizard.core.entity -abstract class ValuedEntityContext> { +abstract class ValuedEntityContext { private val values = mutableMapOf() @Suppress("UNCHECKED_CAST")