diff --git a/src/main/kotlin/creator/custom/types/BuildSystemCoordinatesCreatorProperty.kt b/src/main/kotlin/creator/custom/types/BuildSystemCoordinatesCreatorProperty.kt index 6c458f25f..5bd459467 100644 --- a/src/main/kotlin/creator/custom/types/BuildSystemCoordinatesCreatorProperty.kt +++ b/src/main/kotlin/creator/custom/types/BuildSystemCoordinatesCreatorProperty.kt @@ -3,7 +3,7 @@ * * https://mcdev.io/ * - * Copyright (C) 2025 minecraft-dev + * Copyright (C) 2026 minecraft-dev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -83,16 +83,13 @@ class BuildSystemCoordinatesCreatorProperty( if (projectNameProperty != null) { val projectName = projectNameProperty.get() if (projectName is String) { - coords = coords.copy(artifactId = projectName) + coords = coords.copy(artifactId = sanitizeArtifactId(projectName)) } graphProperty.dependsOn(projectNameProperty, false) { val newProjectName = projectNameProperty.get() if (newProjectName is String) { - val sanitizedArtifactId = newProjectName.lowercase() - .replace(Regex("\\W+"), "-") - .removeSuffix("-") - coords.copy(artifactId = sanitizedArtifactId) + coords.copy(artifactId = sanitizeArtifactId(newProjectName)) } else { coords } @@ -100,6 +97,11 @@ class BuildSystemCoordinatesCreatorProperty( } } + private fun sanitizeArtifactId(projectName: String): String = + projectName.lowercase() + .replace(Regex("\\W+"), "-") + .removeSuffix("-") + override fun buildUi(panel: Panel) { panel.collapsibleGroup(MCDevBundle("creator.ui.group.title")) { this.row(MCDevBundle("creator.ui.group.group_id")) { diff --git a/src/test/kotlin/creator/BuildSystemCoordinatesCreatorPropertyTest.kt b/src/test/kotlin/creator/BuildSystemCoordinatesCreatorPropertyTest.kt new file mode 100644 index 000000000..4874cc3ad --- /dev/null +++ b/src/test/kotlin/creator/BuildSystemCoordinatesCreatorPropertyTest.kt @@ -0,0 +1,68 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.creator + +import com.demonwav.mcdev.creator.custom.TemplateDescriptor +import com.demonwav.mcdev.creator.custom.model.BuildSystemCoordinates +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test + +@DisplayName("Build System Coordinates Creator Property Tests") +class BuildSystemCoordinatesCreatorPropertyTest : CreatorTemplateProcessorTestBase() { + + private val template = + """ + { + "version": ${TemplateDescriptor.LATEST_FORMAT_VERSION}, + "properties": [ + { + "name": "BUILD_COORDS", + "type": "build_system_coordinates" + } + ] + } + """.trimIndent() + + @Test + @DisplayName("Artifact ID Derived From Project Name") + fun artifactIdDerivedFromProjectName() { + makeTemplate(template) + + val projectNameProperty = processor.context.property("PROJECT_NAME") + val buildCoordsProperty = processor.context.property("BUILD_COORDS") + + projectNameProperty.graphProperty.set("My Cool Project") + assertEquals("my-cool-project", buildCoordsProperty.get()?.artifactId) + } + + @Test + @DisplayName("Initial Artifact ID Is Sanitized") + fun initialArtifactIdIsSanitized() { + processor.context.property("PROJECT_NAME").graphProperty.set("My Cool Project") + + // Setting up a template again, e.g. after switching platforms, must not reset the artifact ID to the raw name + makeTemplate(template) + + val buildCoordsProperty = processor.context.property("BUILD_COORDS") + assertEquals("my-cool-project", buildCoordsProperty.get()?.artifactId) + } +}