From 05022109fc2ddcd3a936520f29a1e7e2ac91695b Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Thu, 20 Feb 2020 18:42:44 +0300 Subject: [PATCH] Wizard: save some setting values in UI #KT-35585 fixed --- .../tools/projectWizard/wizard/IdeWizard.kt | 4 ++- .../wizard/NewProjectWizardModuleBuilder.kt | 4 ++- .../service/IdeaSettingSavingWizardService.kt | 25 +++++++++++++ .../wizard/service/IdeaWizardService.kt | 3 +- .../wizard/ui/components/UIComponent.kt | 9 ++--- .../ui/firstStep/FirstWizardStepComponent.kt | 6 ++-- .../modulesEditor/ModulesEditorComponent.kt | 2 +- .../secondStep/modulesEditor/TargetsModel.kt | 7 ++-- .../ui/setting/DefaultSettingComponent.kt | 2 +- .../UIComponentDelegatingSettingComponent.kt | 2 +- .../kotlin/tools/projectWizard/core/Parser.kt | 6 +++- .../core/ValuesReadingContext.kt | 10 ++++++ .../projectWizard/core/entity/Setting.kt | 36 ++++++++++++++++--- .../service/SettingSavingWizardService.kt | 16 +++++++++ .../core/service/WizardService.kt | 3 +- .../projectWizard/plugins/StructurePlugin.kt | 1 + .../plugins/buildSystem/BuildSystemPlugin.kt | 1 + .../settings/buildsystem/Module.kt | 10 +++--- .../tools/projectWizard/templates/Template.kt | 4 +-- .../tools/projectWizard/wizard/Wizard.kt | 14 ++++++++ 20 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaSettingSavingWizardService.kt create mode 100644 libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/SettingSavingWizardService.kt diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/IdeWizard.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/IdeWizard.kt index 39705be1da8..e517dbabf9b 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/IdeWizard.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/IdeWizard.kt @@ -29,7 +29,9 @@ class IdeWizard( private val allSettings = plugins.flatMap { it.declaredSettings } init { - context.settingContext.initPluginSettings(allSettings) + with(valuesReadingContext) { + context.settingContext.apply { initPluginSettings(allSettings) } + } } var projectPath by setting(StructurePlugin::projectPath.reference) diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/NewProjectWizardModuleBuilder.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/NewProjectWizardModuleBuilder.kt index 5c9df0e5e8a..50bb9fccf04 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/NewProjectWizardModuleBuilder.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/NewProjectWizardModuleBuilder.kt @@ -65,6 +65,7 @@ class NewProjectWizardModuleBuilder : EmptyModuleBuilder() { override fun isAvailable(): Boolean = ExperimentalFeatures.NewWizard.isEnabled private var wizardContext: WizardContext? = null + private var pomValuesAreSet: Boolean = false override fun getModuleType(): ModuleType<*> = NewProjectWizardModuleType() override fun getParentGroup(): String = KotlinTemplatesFactory.KOTLIN_PARENT_GROUP_NAME @@ -137,7 +138,7 @@ class NewProjectWizardModuleBuilder : EmptyModuleBuilder() { } private fun updateProjectNameAndPomDate(settingsStep: SettingsStep) { - if (wizard.artifactId != null || wizard.groupId != null) return + if (pomValuesAreSet) return val suggestedProjectName = with(wizard.valuesReadingContext) { ProjectTemplatesPlugin::template.settingValue.suggestedProjectName.decapitalize() } @@ -150,6 +151,7 @@ class NewProjectWizardModuleBuilder : EmptyModuleBuilder() { wizard.artifactId = suggestedProjectName wizard.groupId = suggestGroupId() + pomValuesAreSet = true } private fun suggestGroupId(): String { diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaSettingSavingWizardService.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaSettingSavingWizardService.kt new file mode 100644 index 00000000000..1f8059b1803 --- /dev/null +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaSettingSavingWizardService.kt @@ -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.wizard.service + +import com.intellij.ide.util.PropertiesComponent +import org.jetbrains.kotlin.idea.util.application.runReadAction +import org.jetbrains.kotlin.idea.util.application.runWriteAction +import org.jetbrains.kotlin.tools.projectWizard.core.service.SettingSavingWizardService + +class IdeaSettingSavingWizardService : SettingSavingWizardService, IdeaWizardService { + override fun saveSettingValue(settingPath: String, settingValue: String) = runWriteAction { + PropertiesComponent.getInstance().setValue(PATH_PREFIX + settingPath, settingValue) + } + + override fun getSettingValue(settingPath: String): String? = runReadAction { + PropertiesComponent.getInstance().getValue(PATH_PREFIX + settingPath) + } + + companion object { + private const val PATH_PREFIX = "NewKotlinWizard." + } +} \ No newline at end of file diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaWizardService.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaWizardService.kt index 9bfd45c4e44..0e911ee4bda 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaWizardService.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/service/IdeaWizardService.kt @@ -16,7 +16,8 @@ object IdeaServices { val PROJECT_INDEPENDENT: List = listOf( IdeaFileSystemWizardService(), IdeaBuildSystemAvailabilityWizardService(), - IdeaKotlinVersionProviderService() + IdeaKotlinVersionProviderService(), + IdeaSettingSavingWizardService() ) fun createScopeDependent(project: Project, model: ModifiableModuleModel) = listOf( diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/components/UIComponent.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/components/UIComponent.kt index 6d0ff5b24e6..9aaa0f06797 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/components/UIComponent.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/components/UIComponent.kt @@ -8,14 +8,13 @@ package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components import com.intellij.openapi.Disposable import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext import org.jetbrains.kotlin.tools.projectWizard.core.entity.* +import org.jetbrains.kotlin.tools.projectWizard.core.service.SettingSavingWizardService import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.DynamicComponent import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.FocusableComponent import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.label import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ErrorAwareComponent import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.IdeaBasedComponentValidator -import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.SettingComponent -import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator import java.awt.BorderLayout import javax.swing.JComponent @@ -83,5 +82,7 @@ abstract class UIComponent( } } -fun UIComponent.valueForSetting(setting: Setting>): V? = - setting.defaultValue ?: getUiValue() \ No newline at end of file +fun ValuesReadingContext.valueForSetting( + uiComponent: UIComponent, + setting: Setting> +): V? = setting.savedOrDefaultValue ?: uiComponent.getUiValue() \ No newline at end of file diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/firstStep/FirstWizardStepComponent.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/firstStep/FirstWizardStepComponent.kt index bad830c658c..cad004401cc 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/firstStep/FirstWizardStepComponent.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/firstStep/FirstWizardStepComponent.kt @@ -55,8 +55,10 @@ class TemplatesSubStep(valuesReadingContext: ValuesReadingContext) : // TODO do not use settingContext directly context.settingContext[setting] = value } - allModules().forEach { module -> - module.initDefaultValuesForSettings(valuesReadingContext.context) + read { + allModules().forEach { module -> + module.apply { initDefaultValuesForSettings(valuesReadingContext.context) } + } } } diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/ModulesEditorComponent.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/ModulesEditorComponent.kt index 3ef330837f6..6870b648171 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/ModulesEditorComponent.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/ModulesEditorComponent.kt @@ -41,7 +41,7 @@ class ModulesEditorComponent( } ) - private val model = TargetsModel(tree, ::value, valuesReadingContext.context, uiEditorUsagesStats) + private val model = TargetsModel(tree, ::value, valuesReadingContext, uiEditorUsagesStats) override fun onInit() { super.onInit() diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/TargetsModel.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/TargetsModel.kt index c679675c474..f7c31f1ad44 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/TargetsModel.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/secondStep/modulesEditor/TargetsModel.kt @@ -2,6 +2,7 @@ package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.secondStep.modulesEdi import org.jetbrains.kotlin.idea.projectWizard.UiEditorUsageStats import org.jetbrains.kotlin.tools.projectWizard.core.Context +import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.Module import org.jetbrains.kotlin.tools.projectWizard.settings.buildsystem.ModuleKind import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -12,7 +13,7 @@ import kotlin.reflect.KMutableProperty0 class TargetsModel( private val tree: ModulesEditorTree, private val value: KMutableProperty0?>, - private val context: Context, + private val valuesReadingContext: ValuesReadingContext, private val uiEditorUsagesStats: UiEditorUsageStats ) { private val root get() = tree.model.root as DefaultMutableTreeNode @@ -64,7 +65,9 @@ class TargetsModel( fun add(module: Module) { uiEditorUsagesStats.modulesCreated++ - module.initDefaultValuesForSettings(context) + with(valuesReadingContext) { + module.apply { initDefaultValuesForSettings(context) } + } addToTheTree(module, modifyValue = true, parent = tree.selectedNode ?: root) } diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/DefaultSettingComponent.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/DefaultSettingComponent.kt index de1f295792f..bad7e133e8d 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/DefaultSettingComponent.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/DefaultSettingComponent.kt @@ -65,7 +65,7 @@ class VersionSettingComponent( override fun onInit() { super.onInit() - val values = setting.defaultValue?.let(::listOf).orEmpty() + val values = read { setting.savedOrDefaultValue }?.let(::listOf).orEmpty() comboBox.model = DefaultComboBoxModel(values.toTypedArray()) if (values.isNotEmpty()) { diff --git a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/UIComponentDelegatingSettingComponent.kt b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/UIComponentDelegatingSettingComponent.kt index f9b1926ecf0..197a471d9a1 100644 --- a/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/UIComponentDelegatingSettingComponent.kt +++ b/idea/idea-new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/ui/setting/UIComponentDelegatingSettingComponent.kt @@ -24,7 +24,7 @@ abstract class UIComponentDelegatingSettingComponent override fun onInit() { super.onInit() if (value == null) { - uiComponent.valueForSetting(setting)?.let { value = it } + read { valueForSetting(uiComponent, setting) }?.let { value = it } } value?.let(uiComponent::updateUiValue) } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Parser.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Parser.kt index a1a1cbe8d91..46d4f896bf5 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Parser.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/Parser.kt @@ -10,7 +10,11 @@ import kotlin.reflect.full.isSubclassOf data class ParsingState( val idToTemplate: Map, val settingValues: Map, Any> -) : ComputeContextState +) : ComputeContextState { + companion object { + val EMPTY = ParsingState(emptyMap(), emptyMap()) + } +} fun ParsingState.withSettings(newSettings: List, Any>>) = copy(settingValues = settingValues + newSettings) diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/ValuesReadingContext.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/ValuesReadingContext.kt index aa0feea887b..e3fb2cbd8c2 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/ValuesReadingContext.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/ValuesReadingContext.kt @@ -3,6 +3,7 @@ package org.jetbrains.kotlin.tools.projectWizard.core import org.jetbrains.kotlin.tools.projectWizard.core.entity.* import org.jetbrains.kotlin.tools.projectWizard.core.service.WizardService import org.jetbrains.kotlin.tools.projectWizard.core.service.ServicesManager +import org.jetbrains.kotlin.tools.projectWizard.core.service.SettingSavingWizardService import kotlin.reflect.KClass import kotlin.reflect.KProperty1 @@ -40,5 +41,14 @@ open class ValuesReadingContext( val > PluginSettingReference.pluginSetting: Setting get() = context.settingContext.getPluginSetting(this) + fun Setting>.getSavedValueForSetting(): V? { + if (!isSavable || this !is PluginSetting<*, *>) return null + val serializer = type.serializer.safeAs>() ?: return null + val savedValue = service()!!.getSettingValue(path) ?: return null + return serializer.fromString(savedValue) + } + + val Setting>.savedOrDefaultValue: V? + get() = getSavedValueForSetting() ?: defaultValue } \ No newline at end of file diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Setting.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Setting.kt index 729cb1d2b8b..af4f86dc422 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Setting.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/entity/Setting.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.tools.projectWizard.settings.version.Version import org.jetbrains.kotlin.tools.projectWizard.templates.Template import java.nio.file.Files import java.nio.file.Path +import java.nio.file.Paths import kotlin.reflect.KClass import kotlin.reflect.KProperty1 @@ -130,9 +131,9 @@ class SettingContext(val onUpdated: (SettingReference<*, *>) -> Unit) { onUpdated(reference) } - fun initPluginSettings(settings: List>) { + fun ValuesReadingContext.initPluginSettings(settings: List>) { for (setting in settings) { - setting.defaultValue?.let { values[setting.path] = it } + setting.savedOrDefaultValue?.let { values[setting.path] = it } } } @@ -167,9 +168,9 @@ interface Setting> : Entity, ActivityChecker val title: String val defaultValue: V? val isRequired: Boolean + val isSavable: Boolean var neededAtPhase: GenerationPhase val type: T - } data class InternalSetting>( @@ -178,6 +179,7 @@ data class InternalSetting>( override val defaultValue: V?, override val activityChecker: Checker, override val isRequired: Boolean, + override val isSavable: Boolean, override var neededAtPhase: GenerationPhase, override val validator: SettingValidator<@UnsafeVariance V>, override val type: T @@ -205,6 +207,7 @@ abstract class SettingBuilder>( ) { var checker: Checker = Checker.ALWAYS_AVAILABLE var defaultValue: V? = null + var isSavable: Boolean = false protected var validator = SettingValidator { ValidationResult.OK } @@ -225,6 +228,7 @@ abstract class SettingBuilder>( defaultValue = defaultValue, activityChecker = checker, isRequired = defaultValue == null, + isSavable = isSavable, neededAtPhase = neededAtPhase, validator = validator, type = type @@ -232,14 +236,26 @@ abstract class SettingBuilder>( } +sealed class SettingSerializer() + +object NonSerializable : SettingSerializer() + +data class SerializerImpl( + val fromString: (String) -> V?, + val toString: (V) -> String = Any::toString +) : SettingSerializer() + sealed class SettingType { abstract fun parse(context: ParsingContext, value: Any, name: String): TaskResult + open val serializer: SettingSerializer = NonSerializable } object StringSettingType : SettingType() { override fun parse(context: ParsingContext, value: Any, name: String) = value.parseAs(name) + override val serializer: SettingSerializer = SerializerImpl(fromString = { it }) + class Builder( path: String, private val title: String, @@ -257,6 +273,8 @@ object BooleanSettingType : SettingType() { override fun parse(context: ParsingContext, value: Any, name: String) = value.parseAs(name) + override val serializer: SettingSerializer = SerializerImpl(fromString = { it.toBoolean() }) + class Builder( path: String, title: String, @@ -264,6 +282,7 @@ object BooleanSettingType : SettingType() { ) : SettingBuilder(path, title, neededAtPhase) { override val type = BooleanSettingType } + } class DropDownSettingType( @@ -277,6 +296,12 @@ class DropDownSettingType( } } + override val serializer: SettingSerializer = SerializerImpl(fromString = { value -> + ComputeContext.runInComputeContextWithState(ParsingState.EMPTY) { + parser.parse(this, value, "") + }.asNullable?.first + }) + class Builder( path: String, title: String, @@ -292,7 +317,8 @@ class DropDownSettingType( } } -typealias DropDownSettingTypeFilter = ValuesReadingContext.(SettingReference>, V) -> Boolean +typealias DropDownSettingTypeFilter = + ValuesReadingContext.(SettingReference>, V) -> Boolean class ValueSettingType( @@ -375,6 +401,8 @@ object PathSettingType : SettingType() { } } + override val serializer: SettingSerializer = SerializerImpl(fromString = { Paths.get(it) }) + class Builder( path: String, private val title: String, diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/SettingSavingWizardService.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/SettingSavingWizardService.kt new file mode 100644 index 00000000000..fe4a6e1801e --- /dev/null +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/SettingSavingWizardService.kt @@ -0,0 +1,16 @@ +/* + * 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.service + +interface SettingSavingWizardService : WizardService { + fun saveSettingValue(settingPath: String, settingValue: String) + fun getSettingValue(settingPath: String): String? +} + +class SettingSavingWizardServiceImpl : SettingSavingWizardService, IdeaIndependentWizardService { + override fun saveSettingValue(settingPath: String, settingValue: String) {} + override fun getSettingValue(settingPath: String): String? = null +} \ No newline at end of file diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/WizardService.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/WizardService.kt index db30e9176e1..d6e74228874 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/WizardService.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/core/service/WizardService.kt @@ -11,7 +11,8 @@ object Services { BuildSystemAvailabilityWizardServiceImpl(), DummyFileFormattingService(), KotlinVersionProviderServiceImpl(), - RunConfigurationsServiceImpl() + RunConfigurationsServiceImpl(), + SettingSavingWizardServiceImpl() ) } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/StructurePlugin.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/StructurePlugin.kt index aa310e68414..dc4c8064d91 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/StructurePlugin.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/StructurePlugin.kt @@ -16,6 +16,7 @@ class StructurePlugin(context: Context) : Plugin(context) { val name by stringSetting("Name", GenerationPhase.PROJECT_GENERATION) val groupId by stringSetting("Group ID", GenerationPhase.PROJECT_GENERATION) { + isSavable = true shouldNotBeBlank() validate(StringValidators.shouldBeValidIdentifier("Group ID", setOf('.', '_'))) } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/buildSystem/BuildSystemPlugin.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/buildSystem/BuildSystemPlugin.kt index b652337b077..a9b6cf9a1c9 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/buildSystem/BuildSystemPlugin.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/plugins/buildSystem/BuildSystemPlugin.kt @@ -24,6 +24,7 @@ import java.nio.file.Path abstract class BuildSystemPlugin(context: Context) : Plugin(context) { val type by enumSetting("Build System", GenerationPhase.FIRST_STEP) { + isSavable = true filter = { _, type -> val service = service()!! service.isAvailable(type) diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/settings/buildsystem/Module.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/settings/buildsystem/Module.kt index a098862ac91..bf584fda026 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/settings/buildsystem/Module.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/settings/buildsystem/Module.kt @@ -44,7 +44,7 @@ class Module( is ModuleConfiguratorSetting> -> setting.reference.notRequiredSettingValue else -> null } - ?: setting.defaultValue + ?: setting.savedOrDefaultValue ?: return@map ValidationResult.ValidationError("${setting.title.capitalize()} should not be blank") (setting.validator as SettingValidator).validate(this@settingValidator, value) }.fold() @@ -54,7 +54,7 @@ class Module( org.jetbrains.kotlin.tools.projectWizard.templates.withSettingsOf(module) { template.settings.map { setting -> val value = setting.reference.notRequiredSettingValue - ?: setting.defaultValue + ?: setting.savedOrDefaultValue ?: return@map ValidationResult.ValidationError("${setting.title.capitalize()} should not be blank") (setting.validator as SettingValidator).validate(this@settingValidator, value) }.fold() @@ -86,9 +86,9 @@ class Module( else -> "Module" } - fun initDefaultValuesForSettings(context: Context) { - configurator.safeAs()?.initDefaultValuesFor(this, context) - template?.initDefaultValuesFor(this, context) + fun ValuesReadingContext.initDefaultValuesForSettings(context: Context) { + configurator.safeAs()?.initDefaultValuesFor(this@Module, context) + template?.apply { initDefaultValuesFor(this@Module, context) } } companion object { diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/templates/Template.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/templates/Template.kt index e6b206efa00..dbf52cc2829 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/templates/Template.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/templates/Template.kt @@ -71,10 +71,10 @@ abstract class Template : SettingsOwner { open val settings: List> = emptyList() open val interceptionPoints: List> = emptyList() - fun initDefaultValuesFor(module: Module, context: Context) { + fun ValuesReadingContext.initDefaultValuesFor(module: Module, context: Context) { withSettingsOf(module) { settings.forEach { setting -> - val defaultValue = setting.defaultValue ?: return@forEach + val defaultValue = setting.savedOrDefaultValue ?: return@forEach context.settingContext[setting.reference] = defaultValue } } diff --git a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/Wizard.kt b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/Wizard.kt index a0f4fbcfe47..0c8339e02e4 100644 --- a/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/Wizard.kt +++ b/libraries/tools/new-project-wizard/src/org/jetbrains/kotlin/tools/projectWizard/wizard/Wizard.kt @@ -4,6 +4,7 @@ import org.jetbrains.kotlin.tools.projectWizard.core.* import org.jetbrains.kotlin.tools.projectWizard.core.entity.* import org.jetbrains.kotlin.tools.projectWizard.core.service.WizardService import org.jetbrains.kotlin.tools.projectWizard.core.service.ServicesManager +import org.jetbrains.kotlin.tools.projectWizard.core.service.SettingSavingWizardService import org.jetbrains.kotlin.tools.projectWizard.phases.GenerationPhase abstract class Wizard(createPlugins: PluginsCreator, val servicesManager: ServicesManager, private val isUnitTestMode: Boolean) { @@ -28,6 +29,18 @@ abstract class Wizard(createPlugins: PluginsCreator, val servicesManager: Servic } }.fold(ValidationResult.OK, ValidationResult::and).toResult() + private fun ValuesReadingContext.saveSettingValues(phases: Set) { + for (setting in pluginSettings) { + if (setting.neededAtPhase !in phases) continue + if (!setting.isSavable) continue + val serializer = setting.type.serializer as? SerializerImpl ?: continue + service()!!.saveSettingValue( + setting.path, + serializer.toString(setting.reference.settingValue) + ) + } + } + open fun apply( services: List, phases: Set, @@ -36,6 +49,7 @@ abstract class Wizard(createPlugins: PluginsCreator, val servicesManager: Servic context.checkAllRequiredSettingPresent(phases).ensure() val taskRunningContext = TaskRunningContext(context, servicesManager.withServices(services), isUnitTestMode) taskRunningContext.validate(phases).ensure() + taskRunningContext.saveSettingValues(phases) val (tasksSorted) = context.sortTasks().map { tasks -> tasks.groupBy { it.phase }.toList().sortedBy { it.first }.flatMap { it.second } }