diff --git a/src/main/kotlin/no/item/xp/plugin/GenerateCodeTask.kt b/src/main/kotlin/no/item/xp/plugin/GenerateCodeTask.kt index 8672a57..3a673ae 100644 --- a/src/main/kotlin/no/item/xp/plugin/GenerateCodeTask.kt +++ b/src/main/kotlin/no/item/xp/plugin/GenerateCodeTask.kt @@ -7,6 +7,7 @@ import no.item.xp.plugin.extensions.getFormNode import no.item.xp.plugin.models.ObjectTypeModel import no.item.xp.plugin.parser.parseObjectTypeModel import no.item.xp.plugin.parser.resolveMixinGraph +import no.item.xp.plugin.parser.toMacroModel import no.item.xp.plugin.renderers.renderGlobalComponentMap import no.item.xp.plugin.renderers.renderGlobalContentTypeMap import no.item.xp.plugin.renderers.renderGlobalXDataMap @@ -138,7 +139,8 @@ open class GenerateCodeTask val indexFilePath = Paths.get(targetFilePath.parent.toString(), "index.d.ts") val targetFile = File(indexFilePath.toUri()) - val fileContent = renderTypeModelAsTypeScript(right, resolveMixinsImportPath(targetFile, rootOutputDir)) + val model = if (IS_MACRO.matches(fileInJar.entry.name)) toMacroModel(right) else right + val fileContent = renderTypeModelAsTypeScript(model, resolveMixinsImportPath(targetFile, rootOutputDir)) writeTargetFile(targetFile, fileContent, prependText, singleQuote) logger.lifecycle("Updated file: ${Path.of(targetFile.toURI()).toUri()}") diff --git a/src/main/kotlin/no/item/xp/plugin/GenerateTypeScriptWorkAction.kt b/src/main/kotlin/no/item/xp/plugin/GenerateTypeScriptWorkAction.kt index e1666ca..5e4e5aa 100644 --- a/src/main/kotlin/no/item/xp/plugin/GenerateTypeScriptWorkAction.kt +++ b/src/main/kotlin/no/item/xp/plugin/GenerateTypeScriptWorkAction.kt @@ -5,9 +5,12 @@ import arrow.core.right import no.item.xp.plugin.extensions.getFormNode import no.item.xp.plugin.models.ObjectTypeModel import no.item.xp.plugin.parser.parseObjectTypeModel +import no.item.xp.plugin.parser.toMacroModel import no.item.xp.plugin.renderers.renderSiteConfig import no.item.xp.plugin.renderers.ts.renderTypeModelAsTypeScript +import no.item.xp.plugin.util.IS_MACRO import no.item.xp.plugin.util.concatFileName +import no.item.xp.plugin.util.normalizeFilePath import no.item.xp.plugin.util.parseXml import no.item.xp.plugin.util.simpleFilePath import no.item.xp.plugin.util.writeTargetFile @@ -60,10 +63,13 @@ abstract class GenerateTypeScriptWorkAction : WorkAction }, { val fileContent = - if (file.absolutePath.endsWith(concatFileName("resources", "site", "site.xml"))) { - renderSiteConfig(it, mixinsImportPath) - } else { - renderTypeModelAsTypeScript(it, mixinsImportPath) + when { + file.absolutePath.endsWith(concatFileName("resources", "site", "site.xml")) -> + renderSiteConfig(it, mixinsImportPath) + IS_MACRO.matches(normalizeFilePath(file)) -> + renderTypeModelAsTypeScript(toMacroModel(it), mixinsImportPath) + else -> + renderTypeModelAsTypeScript(it, mixinsImportPath) } writeTargetFile(targetFile, fileContent, parameters.getPrependText().get(), parameters.getSingleQuote().get()) diff --git a/src/main/kotlin/no/item/xp/plugin/parser/ParseMacro.kt b/src/main/kotlin/no/item/xp/plugin/parser/ParseMacro.kt new file mode 100644 index 0000000..9146c22 --- /dev/null +++ b/src/main/kotlin/no/item/xp/plugin/parser/ParseMacro.kt @@ -0,0 +1,26 @@ +package no.item.xp.plugin.parser + +import no.item.xp.plugin.models.BooleanField +import no.item.xp.plugin.models.NumberField +import no.item.xp.plugin.models.NumberFieldWithValidation +import no.item.xp.plugin.models.ObjectTypeModel +import no.item.xp.plugin.models.StringField +import no.item.xp.plugin.models.UnknownField + +/** + * The params of a macro are passed in from the HtmlArea, so they are always strings + */ +fun toMacroModel(model: ObjectTypeModel): ObjectTypeModel = + model.copy( + fields = + model.fields.map { field -> + when (field) { + is NumberField, + is NumberFieldWithValidation, + is BooleanField, + is UnknownField, + -> StringField(field) + else -> field + } + }, + ) diff --git a/src/main/kotlin/no/item/xp/plugin/util/TargetFile.kt b/src/main/kotlin/no/item/xp/plugin/util/TargetFile.kt index 5995e45..4f21a06 100644 --- a/src/main/kotlin/no/item/xp/plugin/util/TargetFile.kt +++ b/src/main/kotlin/no/item/xp/plugin/util/TargetFile.kt @@ -7,6 +7,7 @@ val IS_XDATA = "^.*site/x-data.*\$".toRegex(RegexOption.IGNORE_CASE) val IS_MIXIN = "^.*site/mixins.*\$".toRegex(RegexOption.IGNORE_CASE) val IS_PART = "^.*site/parts.*\$".toRegex(RegexOption.IGNORE_CASE) val IS_PAGE = "^.*site/pages.*\$".toRegex(RegexOption.IGNORE_CASE) +val IS_MACRO = "^.*site/macros.*\$".toRegex(RegexOption.IGNORE_CASE) fun writeTargetFile( targetFile: File, diff --git a/src/test/kotlin/no/item/xp/plugin/parser/ToMacroModelTest.kt b/src/test/kotlin/no/item/xp/plugin/parser/ToMacroModelTest.kt new file mode 100644 index 0000000..7fa2fe6 --- /dev/null +++ b/src/test/kotlin/no/item/xp/plugin/parser/ToMacroModelTest.kt @@ -0,0 +1,49 @@ +package no.item.xp.plugin.parser + +import no.item.xp.plugin.models.BooleanField +import no.item.xp.plugin.models.NumberField +import no.item.xp.plugin.models.NumberFieldWithValidation +import no.item.xp.plugin.models.ObjectTypeModel +import no.item.xp.plugin.models.StringField +import no.item.xp.plugin.models.StringFieldWithValidation +import no.item.xp.plugin.models.UnionOfStringLiteralField +import no.item.xp.plugin.models.UnknownField +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class ToMacroModelTest { + @Test + fun `convert all fields to strings`() { + val result = + toMacroModel( + ObjectTypeModel( + "youtube", + listOf( + NumberField("width", "Width", true, false), + NumberFieldWithValidation("height", "Height", false, false, 1, 1000), + BooleanField("autoplay", "Autoplay", false, false), + UnknownField("other", "Other", true, true), + StringField("url", "Url", false, false), + StringFieldWithValidation("start", "Start", true, false, "^\\d+$", null), + UnionOfStringLiteralField("size", "Size", true, false, listOf("small", "large")), + ), + ), + ) + + assertEquals( + ObjectTypeModel( + "youtube", + listOf( + StringField("width", "Width", true, false), + StringField("height", "Height", false, false), + StringField("autoplay", "Autoplay", false, false), + StringField("other", "Other", true, true), + StringField("url", "Url", false, false), + StringFieldWithValidation("start", "Start", true, false, "^\\d+$", null), + UnionOfStringLiteralField("size", "Size", true, false, listOf("small", "large")), + ), + ), + result, + ) + } +}