Wizard: add better exception message when template not found

This commit is contained in:
Ilya Kirillov
2020-03-26 01:48:45 +03:00
parent 9fc1220b99
commit d58d3d06f5
2 changed files with 13 additions and 9 deletions
@@ -8,12 +8,10 @@ package org.jetbrains.kotlin.tools.projectWizard.wizard.service
import com.intellij.ide.fileTemplates.FileTemplateUtil
import org.jetbrains.kotlin.tools.projectWizard.core.service.TemplateEngineService
import org.jetbrains.kotlin.tools.projectWizard.templates.FileTemplateDescriptor
import org.jetbrains.kotlin.tools.projectWizard.templates.Template
class IdeaVelocityEngineTemplateService : TemplateEngineService, IdeaWizardService {
class IdeaVelocityEngineTemplateService : TemplateEngineService(), IdeaWizardService {
override fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String {
val templatePath = template.templateId
val templateText = Template::class.java.getResource(templatePath).readText()
val templateText = getTemplateText(template)
return FileTemplateUtil.mergeTemplate(data, templateText, false)
}
}
@@ -18,8 +18,15 @@ import org.jetbrains.kotlin.tools.projectWizard.templates.FileTemplateDescriptor
import org.jetbrains.kotlin.tools.projectWizard.templates.Template
import java.io.StringWriter
interface TemplateEngineService : WizardService {
fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String
abstract class TemplateEngineService : WizardService {
abstract fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String
protected fun getTemplateText(template: FileTemplateDescriptor) = try {
val templateId = template.templateId.replace('\\', '/')
Template::class.java.getResource(templateId).readText()
} catch (e: Throwable) {
throw RuntimeException("Can not get template ${template.templateId}", e)
}
fun Writer.writeTemplate(template: FileTemplate): TaskResult<Unit> {
val formatter = service<FileFormattingService>()
@@ -31,10 +38,9 @@ interface TemplateEngineService : WizardService {
}
class VelocityTemplateEngineServiceImpl : TemplateEngineService, IdeaIndependentWizardService {
class VelocityTemplateEngineServiceImpl : TemplateEngineService(), IdeaIndependentWizardService {
override fun renderTemplate(template: FileTemplateDescriptor, data: Map<String, Any?>): String {
val templatePath = template.templateId
val templateText = Template::class.java.getResource(templatePath).readText()
val templateText = getTemplateText(template)
val context = VelocityContext().apply {
data.forEach { (key, value) -> put(key, value) }
}