|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2023 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.creator.step |
| 22 | + |
| 23 | +import com.demonwav.mcdev.creator.storeToData |
| 24 | +import com.intellij.ide.wizard.AbstractNewProjectWizardStep |
| 25 | +import com.intellij.ide.wizard.NewProjectWizardBaseData |
| 26 | +import com.intellij.ide.wizard.NewProjectWizardStep |
| 27 | +import com.intellij.openapi.project.Project |
| 28 | +import com.intellij.openapi.ui.validation.AFTER_GRAPH_PROPAGATION |
| 29 | +import com.intellij.openapi.ui.validation.CHECK_NON_EMPTY |
| 30 | +import com.intellij.openapi.ui.validation.and |
| 31 | +import com.intellij.openapi.ui.validation.validationErrorIf |
| 32 | +import com.intellij.openapi.util.Key |
| 33 | +import com.intellij.ui.dsl.builder.COLUMNS_MEDIUM |
| 34 | +import com.intellij.ui.dsl.builder.Panel |
| 35 | +import com.intellij.ui.dsl.builder.bindText |
| 36 | +import com.intellij.ui.dsl.builder.columns |
| 37 | +import com.intellij.ui.dsl.builder.textValidation |
| 38 | + |
| 39 | +private val validModIdRegex = "[a-z][a-z0-9-_]{1,63}".toRegex() |
| 40 | +private val invalidModIdRegex = "[^a-z0-9-_]+".toRegex() |
| 41 | + |
| 42 | +private val validForgeModIdRegex = "[a-z][a-z0-9_]{1,63}".toRegex() |
| 43 | +private val invalidForgeModIdRegex = "[^a-z0-9_]+".toRegex() |
| 44 | + |
| 45 | +abstract class AbstractModIdStep( |
| 46 | + parent: NewProjectWizardStep, |
| 47 | + private val validRegex: Regex = validModIdRegex, |
| 48 | + private val invalidRegex: Regex = invalidModIdRegex |
| 49 | +) : AbstractNewProjectWizardStep(parent) { |
| 50 | + private val baseData = data.getUserData(NewProjectWizardBaseData.KEY) |
| 51 | + ?: throw IllegalStateException("Mod id step created without base step") |
| 52 | + val idProperty = propertyGraph.lazyProperty(::suggestId) |
| 53 | + var id by idProperty |
| 54 | + |
| 55 | + private val idValidation = validationErrorIf<String>("Id must match $validRegex") { !it.matches(validRegex) } |
| 56 | + |
| 57 | + init { |
| 58 | + idProperty.dependsOn(baseData.nameProperty, ::suggestId) |
| 59 | + storeToData() |
| 60 | + } |
| 61 | + |
| 62 | + fun suggestId(): String { |
| 63 | + val sanitized = baseData.name.lowercase().replace(invalidRegex, "_") |
| 64 | + if (sanitized.length > 64) { |
| 65 | + return sanitized.substring(0, 64) |
| 66 | + } |
| 67 | + return sanitized |
| 68 | + } |
| 69 | + |
| 70 | + abstract val label: String |
| 71 | + |
| 72 | + override fun setupUI(builder: Panel) { |
| 73 | + with(builder) { |
| 74 | + row(label) { |
| 75 | + textField() |
| 76 | + .bindText(idProperty) |
| 77 | + .columns(COLUMNS_MEDIUM) |
| 78 | + .validationRequestor(AFTER_GRAPH_PROPAGATION(propertyGraph)) |
| 79 | + .textValidation(CHECK_NON_EMPTY and idValidation) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override fun setupProject(project: Project) { |
| 85 | + data.putUserData(KEY, id) |
| 86 | + } |
| 87 | + |
| 88 | + companion object { |
| 89 | + val KEY = Key.create<String>("${AbstractModIdStep::class.java.name}.id") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class ModIdStep(parent: NewProjectWizardStep) : AbstractModIdStep(parent) { |
| 94 | + override val label = "Mod Id:" |
| 95 | +} |
| 96 | + |
| 97 | +class ForgeStyleModIdStep(parent: NewProjectWizardStep) : |
| 98 | + AbstractModIdStep(parent, validForgeModIdRegex, invalidForgeModIdRegex) { |
| 99 | + override val label = "Mod Id:" |
| 100 | +} |
| 101 | + |
| 102 | +class PluginIdStep(parent: NewProjectWizardStep) : AbstractModIdStep(parent) { |
| 103 | + override val label = "Plugin Id:" |
| 104 | +} |
0 commit comments