Wizard: do not require to default settings to be present in yaml

This commit is contained in:
Ilya Kirillov
2020-03-10 15:23:23 +03:00
parent a16cb7b74d
commit f1741a4d1b
5 changed files with 45 additions and 11 deletions
@@ -191,6 +191,8 @@ class Context private constructor(
val <V : Any, T : SettingType<V>> SettingReference<V, T>.setting: Setting<V, T>
get() = with(this) { getSetting() }
inline operator fun <T> invoke(reader: ReadingContext.() -> T): T = reader()
}
open inner class WritingContext : ReadingContext() {
@@ -114,3 +114,4 @@ inline val <V : Any, reified T : SettingType<V>> PluginSettingPropertyReference<
get() = PluginSettingReference(this, T::class)
typealias PluginSettingPropertyReference<V, T> = KProperty1<out Plugin, PluginSetting<V, T>>
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2020 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.tools.projectWizard.core.entity.settings
import org.jetbrains.kotlin.tools.projectWizard.core.*
fun ParsingContext.parseSettingsMap(
path: String,
values: Map<String, Any?>,
settingReferences: List<Pair<SettingReference<*, *>, Setting<*, *>>>
): TaskResult<List<Pair<SettingReference<*, *>, Any>>> = settingReferences.mapComputeM { (settingReference, setting) ->
val settingValue = values[setting.path]
when {
settingValue != null -> {
setting.type.parse(this, settingValue, setting.path).map { listOf(settingReference to it) }
}
setting.isRequired -> {
fail(ParseError("No value was found for a key `$path.${setting.path}`"))
}
else -> success(emptyList())
}
}.sequence().map { it.flatten() }
@@ -261,11 +261,14 @@ interface ModuleConfigurator : DisplayableSettingItem, EntitiesOwnerDescriptor {
} or mapParser { map, path ->
val (id) = map.parseValue<String>(path, "name")
val (configurator) = BY_ID[id].toResult { ConfiguratorNotFoundError(id) }
val (settingsWithValues) = configurator.settings.mapComputeM { setting ->
val (settingValue) = map[setting.path].toResult { ParseError("No value was found for a key `$path.${setting.path}`") }
val reference = withSettingsOf(moduleIdentificator, configurator) { setting.reference }
setting.type.parse(this, settingValue, setting.path).map { reference to it }
}.sequence()
val (settingsWithValues) = parseSettingsMap(
path,
map,
configurator.settings.map { setting ->
val reference = withSettingsOf(moduleIdentificator, configurator) { setting.reference }
reference to setting
}
)
updateState { it.withSettings(settingsWithValues) }
configurator
}
@@ -233,14 +233,17 @@ abstract class Template : SettingsOwner, EntitiesOwnerDescriptor {
enumSettingImpl(title, neededAtPhase, init) as ReadOnlyProperty<Any, TemplateSetting<E, DropDownSettingType<E>>>
companion object {
fun parser(sourcesetIdentificator: Identificator): Parser<Template> = mapParser { map, path ->
fun parser(templateId: Identificator): Parser<Template> = mapParser { map, path ->
val (id) = map.parseValue<String>(path, "id")
val (template) = state.idToTemplate[id].toResult { TemplateNotFoundError(id) }
val (settingsWithValues) = template.settings.mapComputeM { setting ->
val (settingValue) = map[setting.path].toResult { ParseError("No value was found for a key `$path.${setting.path}`") }
val reference = withSettingsOf(sourcesetIdentificator, template) { setting.reference }
setting.type.parse(this, settingValue, setting.path).map { reference to it }
}.sequence()
val (settingsWithValues) = parseSettingsMap(
path,
map,
template.settings.map { setting ->
val reference = withSettingsOf(templateId, template) { setting.reference }
reference to setting
}
)
updateState { it.withSettings(settingsWithValues) }
template
} or valueParserM { value, path ->