User: joe Date: 22 Sep 23 16:40 Revision: 164ef43c4aae946547f8ec53d325b8485a6c8f2a Summary: Add ProjectReimporter TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=8738&personal=false Index: src/main/kotlin/facet/MinecraftFacetConfiguration.kt =================================================================== --- src/main/kotlin/facet/MinecraftFacetConfiguration.kt (revision e3ecfb7c02a91253fed8e1baeb2ee50ad62dc935) +++ src/main/kotlin/facet/MinecraftFacetConfiguration.kt (revision 164ef43c4aae946547f8ec53d325b8485a6c8f2a) @@ -31,7 +31,9 @@ class MinecraftFacetConfiguration : FacetConfiguration, PersistentStateComponent { var facet: MinecraftFacet? = null - private var state = MinecraftFacetConfigurationData() + private var state = MinecraftFacetConfigurationData( + projectReimportVersion = ProjectReimporter.CURRENT_REIMPORT_VERSION + ) override fun createEditorTabs(editorContext: FacetEditorContext?, validatorsManager: FacetValidatorsManager?) = arrayOf(MinecraftFacetEditorTabV2(this)) @@ -50,4 +52,6 @@ var autoDetectTypes: MutableSet = mutableSetOf(), @Tag("forgePatcher") var forgePatcher: Boolean = false, + @Tag("projectReimportVersion") + var projectReimportVersion: Int = 0, ) Index: src/main/kotlin/facet/MinecraftFacetDetector.kt =================================================================== --- src/main/kotlin/facet/MinecraftFacetDetector.kt (revision e3ecfb7c02a91253fed8e1baeb2ee50ad62dc935) +++ src/main/kotlin/facet/MinecraftFacetDetector.kt (revision 164ef43c4aae946547f8ec53d325b8485a6c8f2a) @@ -70,6 +70,9 @@ fun doCheck(project: Project) { val moduleManager = ModuleManager.getInstance(project) + + var needsReimport = false + for (module in moduleManager.modules) { val facetManager = FacetManager.getInstance(module) val minecraftFacet = facetManager.getFacetByType(MinecraftFacet.ID) @@ -78,10 +81,17 @@ checkNoFacet(module) } else { checkExistingFacet(module, minecraftFacet) + if (ProjectReimporter.needsReimport(minecraftFacet)) { + needsReimport = true - } - } - } + } + } + } + if (needsReimport) { + ProjectReimporter.reimport(project) + } + } + private fun checkNoFacet(module: Module) { val platforms = autoDetectTypes(module).ifEmpty { return } Index: src/main/kotlin/facet/ProjectReimporter.kt =================================================================== --- src/main/kotlin/facet/ProjectReimporter.kt (revision 164ef43c4aae946547f8ec53d325b8485a6c8f2a) +++ src/main/kotlin/facet/ProjectReimporter.kt (revision 164ef43c4aae946547f8ec53d325b8485a6c8f2a) @@ -0,0 +1,63 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2023 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.facet + +import com.intellij.openapi.actionSystem.ActionManager +import com.intellij.openapi.actionSystem.ActionPlaces +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.module.ModuleManager +import com.intellij.openapi.project.Project + +/** + * Forces a project reimport on existing projects if a new MinecraftDev version requires it to add extra setup to the + * project. + */ +object ProjectReimporter { + private val log = logger() + + const val CURRENT_REIMPORT_VERSION = 0 + + fun needsReimport(facet: MinecraftFacet) = + facet.configuration.state.projectReimportVersion < CURRENT_REIMPORT_VERSION + + fun reimport(project: Project) { + for (module in ModuleManager.getInstance(project).modules) { + val minecraftFacet = MinecraftFacet.getInstance(module) ?: continue + minecraftFacet.configuration.state.projectReimportVersion = CURRENT_REIMPORT_VERSION + } + + val refreshAllProjectsAction = ActionManager.getInstance().getAction("ExternalSystem.RefreshAllProjects") + if (refreshAllProjectsAction == null) { + log.error("Could not find refresh all projects action") + return + } + val callback = ActionManager.getInstance().tryToExecute( + refreshAllProjectsAction, + null, + null, + ActionPlaces.UNKNOWN, + true + ) + callback.doWhenRejected { error -> + log.error("Rejected refresh all projects: $error") + } + } +}