Wizard: force service to always return non-null value

This commit is contained in:
Ilya Kirillov
2020-02-20 18:45:20 +03:00
parent 05022109fc
commit 1c49b230f4
10 changed files with 17 additions and 15 deletions
@@ -12,10 +12,11 @@ open class ValuesReadingContext(
private val servicesManager: ServicesManager, private val servicesManager: ServicesManager,
val isUnitTestMode: Boolean val isUnitTestMode: Boolean
) { ) {
inline fun <reified S : WizardService> service(noinline filter: (S) -> Boolean = { true }) = serviceByClass(S::class, filter) inline fun <reified S : WizardService> service(noinline filter: (S) -> Boolean = { true }): S =
serviceByClass(S::class, filter)
fun <S : WizardService> serviceByClass(klass: KClass<S>, filter: (S) -> Boolean = { true }) = fun <S : WizardService> serviceByClass(klass: KClass<S>, filter: (S) -> Boolean = { true }): S =
servicesManager.serviceByClass(klass, filter) servicesManager.serviceByClass(klass, filter) ?: error("Service ${klass.simpleName} was not found")
inline val <reified T : Any> PropertyReference<T>.propertyValue: T inline val <reified T : Any> PropertyReference<T>.propertyValue: T
get() = context.propertyContext[this] as T get() = context.propertyContext[this] as T
@@ -44,7 +45,7 @@ open class ValuesReadingContext(
fun <V : Any> Setting<V, SettingType<V>>.getSavedValueForSetting(): V? { fun <V : Any> Setting<V, SettingType<V>>.getSavedValueForSetting(): V? {
if (!isSavable || this !is PluginSetting<*, *>) return null if (!isSavable || this !is PluginSetting<*, *>) return null
val serializer = type.serializer.safeAs<SerializerImpl<V>>() ?: return null val serializer = type.serializer.safeAs<SerializerImpl<V>>() ?: return null
val savedValue = service<SettingSavingWizardService>()!!.getSettingValue(path) ?: return null val savedValue = service<SettingSavingWizardService>().getSettingValue(path) ?: return null
return serializer.fromString(savedValue) return serializer.fromString(savedValue)
} }
@@ -13,7 +13,7 @@ interface BuildSystemAvailabilityWizardService : WizardService {
} }
fun ValuesReadingContext.isBuildSystemAvailable(buildSystemType: BuildSystemType) = fun ValuesReadingContext.isBuildSystemAvailable(buildSystemType: BuildSystemType) =
service<BuildSystemAvailabilityWizardService>()!!.isAvailable(buildSystemType) service<BuildSystemAvailabilityWizardService>().isAvailable(buildSystemType)
class BuildSystemAvailabilityWizardServiceImpl : BuildSystemAvailabilityWizardService, IdeaIndependentWizardService { class BuildSystemAvailabilityWizardServiceImpl : BuildSystemAvailabilityWizardService, IdeaIndependentWizardService {
override fun isAvailable(buildSystemType: BuildSystemType) = true override fun isAvailable(buildSystemType: BuildSystemType) = true
@@ -19,6 +19,7 @@ class AndroidPlugin(context: Context) : Plugin(context) {
"Android SDK Path", "Android SDK Path",
neededAtPhase = GenerationPhase.PROJECT_GENERATION neededAtPhase = GenerationPhase.PROJECT_GENERATION
) { ) {
isSavable = true
shouldExists() shouldExists()
} }
@@ -20,7 +20,7 @@ class RunConfigurationsPlugin(context: Context) : Plugin(context) {
runBefore(BuildSystemPlugin::importProject) runBefore(BuildSystemPlugin::importProject)
withAction { withAction {
service<RunConfigurationsService>()!!.apply { service<RunConfigurationsService>().apply {
addRunConfigurations(RunConfigurationsPlugin::configurations.propertyValue) addRunConfigurations(RunConfigurationsPlugin::configurations.propertyValue)
} }
UNIT_SUCCESS UNIT_SUCCESS
@@ -32,7 +32,7 @@ class StructurePlugin(context: Context) : Plugin(context) {
val createProjectDir by pipelineTask(GenerationPhase.PROJECT_GENERATION) { val createProjectDir by pipelineTask(GenerationPhase.PROJECT_GENERATION) {
withAction { withAction {
service<FileSystemWizardService>()!!.createDirectory(StructurePlugin::projectPath.reference.settingValue) service<FileSystemWizardService>().createDirectory(StructurePlugin::projectPath.reference.settingValue)
} }
} }
} }
@@ -26,7 +26,7 @@ abstract class BuildSystemPlugin(context: Context) : Plugin(context) {
val type by enumSetting<BuildSystemType>("Build System", GenerationPhase.FIRST_STEP) { val type by enumSetting<BuildSystemType>("Build System", GenerationPhase.FIRST_STEP) {
isSavable = true isSavable = true
filter = { _, type -> filter = { _, type ->
val service = service<BuildSystemAvailabilityWizardService>()!! val service = service<BuildSystemAvailabilityWizardService>()
service.isAvailable(type) service.isAvailable(type)
} }
@@ -71,7 +71,7 @@ abstract class BuildSystemPlugin(context: Context) : Plugin(context) {
val createModules by pipelineTask(GenerationPhase.PROJECT_GENERATION) { val createModules by pipelineTask(GenerationPhase.PROJECT_GENERATION) {
runAfter(StructurePlugin::createProjectDir) runAfter(StructurePlugin::createProjectDir)
withAction { withAction {
val fileSystem = service<FileSystemWizardService>()!! val fileSystem = service<FileSystemWizardService>()
val data = BuildSystemPlugin::buildSystemData.propertyValue.first { it.type == buildSystemType } val data = BuildSystemPlugin::buildSystemData.propertyValue.first { it.type == buildSystemType }
val buildFileData = data.buildFileData ?: return@withAction UNIT_SUCCESS val buildFileData = data.buildFileData ?: return@withAction UNIT_SUCCESS
BuildSystemPlugin::buildFiles.propertyValue.mapSequenceIgnore { buildFile -> BuildSystemPlugin::buildFiles.propertyValue.mapSequenceIgnore { buildFile ->
@@ -130,7 +130,7 @@ abstract class GradlePlugin(context: Context) : BuildSystemPlugin(context) {
repositories repositories
) )
val buildFileText = createBuildFile().printBuildFile { settingsGradleIR.render(this) } val buildFileText = createBuildFile().printBuildFile { settingsGradleIR.render(this) }
service<FileSystemWizardService>()!!.createFile( service<FileSystemWizardService>().createFile(
projectPath / buildFileName, projectPath / buildFileName,
buildFileText buildFileText
) )
@@ -26,7 +26,7 @@ class KotlinPlugin(context: Context) : Plugin(context) {
title = "Downloading list of Kotlin versions" title = "Downloading list of Kotlin versions"
withAction { withAction {
val version = service<KotlinVersionProviderService>()!!.getKotlinVersion() val version = service<KotlinVersionProviderService>().getKotlinVersion()
KotlinPlugin::version.update { version.asSuccess() } KotlinPlugin::version.update { version.asSuccess() }
} }
} }
@@ -84,7 +84,7 @@ class KotlinPlugin(context: Context) : Plugin(context) {
val createSourcesetDirectories by pipelineTask(GenerationPhase.PROJECT_GENERATION) { val createSourcesetDirectories by pipelineTask(GenerationPhase.PROJECT_GENERATION) {
runAfter(KotlinPlugin::createModules) runAfter(KotlinPlugin::createModules)
withAction { withAction {
fun Path.createKotlinAndResourceDirectories() = with(service<FileSystemWizardService>()!!) { fun Path.createKotlinAndResourceDirectories() = with(service<FileSystemWizardService>()) {
createDirectory(this@createKotlinAndResourceDirectories / Defaults.KOTLIN_DIR) andThen createDirectory(this@createKotlinAndResourceDirectories / Defaults.KOTLIN_DIR) andThen
createDirectory(this@createKotlinAndResourceDirectories / Defaults.RESOURCES_DIR) createDirectory(this@createKotlinAndResourceDirectories / Defaults.RESOURCES_DIR)
} }
@@ -17,11 +17,11 @@ interface TemplateEngine {
fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String
fun TaskRunningContext.writeTemplate(template: FileTemplate): TaskResult<Unit> { fun TaskRunningContext.writeTemplate(template: FileTemplate): TaskResult<Unit> {
val formatter = service<FileFormattingService>()!! val formatter = service<FileFormattingService>()
val text = renderTemplate(template.descriptor, template.data).let { text -> val text = renderTemplate(template.descriptor, template.data).let { text ->
formatter.formatFile(text, template.descriptor.relativePath.fileName.toString()) formatter.formatFile(text, template.descriptor.relativePath.fileName.toString())
} }
return service<FileSystemWizardService>()!!.createFile(template.rootPath / template.descriptor.relativePath, text) return service<FileSystemWizardService>().createFile(template.rootPath / template.descriptor.relativePath, text)
} }
} }
@@ -34,7 +34,7 @@ abstract class Wizard(createPlugins: PluginsCreator, val servicesManager: Servic
if (setting.neededAtPhase !in phases) continue if (setting.neededAtPhase !in phases) continue
if (!setting.isSavable) continue if (!setting.isSavable) continue
val serializer = setting.type.serializer as? SerializerImpl<Any> ?: continue val serializer = setting.type.serializer as? SerializerImpl<Any> ?: continue
service<SettingSavingWizardService>()!!.saveSettingValue( service<SettingSavingWizardService>().saveSettingValue(
setting.path, setting.path,
serializer.toString(setting.reference.settingValue) serializer.toString(setting.reference.settingValue)
) )