User: rednesto Date: 14 Jul 24 09:54 Revision: 11fceec01f5bdf96cb5570eb55433f4f9c063b98 Summary: Change how creator finalizers are run Should help with some weird deadlocks in future IDE versions TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=9431&personal=false Index: src/main/kotlin/creator/custom/CustomPlatformStep.kt =================================================================== --- src/main/kotlin/creator/custom/CustomPlatformStep.kt (revision 246c1d5f65e7042b1d42397c8a25d41b9bddf87f) +++ src/main/kotlin/creator/custom/CustomPlatformStep.kt (revision 11fceec01f5bdf96cb5570eb55433f4f9c063b98) @@ -38,6 +38,7 @@ import com.intellij.ide.wizard.GitNewProjectWizardData import com.intellij.ide.wizard.NewProjectWizardBaseData import com.intellij.ide.wizard.NewProjectWizardStep +import com.intellij.openapi.application.WriteAction import com.intellij.openapi.diagnostic.Attachment import com.intellij.openapi.diagnostic.ControlFlowException import com.intellij.openapi.diagnostic.getOrLogException @@ -502,23 +503,27 @@ } } - application.executeOnPooledThread { - application.invokeLater({ - application.runWriteAction { + val finalizeAction = { + WriteAction.runAndWait { - LocalFileSystem.getInstance().refresh(false) - // Apparently a module root is required for the reformat to work - setupTempRootModule(project, projectPath) + LocalFileSystem.getInstance().refresh(false) + // Apparently a module root is required for the reformat to work + setupTempRootModule(project, projectPath) - } + reformatFiles(project, generatedFiles) openFilesInEditor(project, generatedFiles) - }, project.disposed) + } val finalizers = selectedTemplate.descriptor.finalizers if (!finalizers.isNullOrEmpty()) { CreatorFinalizer.executeAll(context, project, finalizers, templateProperties) } } + if (context.isCreatingNewProject) { + TemplateService.instance.registerFinalizerAction(project, finalizeAction) + } else { + application.executeOnPooledThread { finalizeAction() } - } + } + } private fun setupTempRootModule(project: Project, projectPath: Path) { val modifiableModel = ModuleManager.getInstance(project).getModifiableModel() Index: src/main/kotlin/creator/custom/TemplateService.kt =================================================================== --- src/main/kotlin/creator/custom/TemplateService.kt (revision 11fceec01f5bdf96cb5570eb55433f4f9c063b98) +++ src/main/kotlin/creator/custom/TemplateService.kt (revision 11fceec01f5bdf96cb5570eb55433f4f9c063b98) @@ -0,0 +1,60 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2024 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.custom + +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.thisLogger +import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.ProjectActivity +import com.intellij.util.application + +@Service +class TemplateService { + + private val pendingActions: MutableMap Unit> = mutableMapOf() + + fun registerFinalizerAction(project: Project, action: suspend () -> Unit) { + if (pendingActions.containsKey(project.name)) { + thisLogger().error("More than one finalizer action registered for project $project") + return + } + + pendingActions[project.locationHash] = action + } + + suspend fun executeFinalizer(project: Project) { + pendingActions.remove(project.locationHash)?.invoke() + } + + companion object { + + val instance: TemplateService + get() = application.service() + } +} + +class TemplateProjectFinalizerActivity : ProjectActivity { + + override suspend fun execute(project: Project) { + TemplateService.instance.executeFinalizer(project) + } +} Index: src/main/resources/META-INF/plugin.xml =================================================================== --- src/main/resources/META-INF/plugin.xml (revision 246c1d5f65e7042b1d42397c8a25d41b9bddf87f) +++ src/main/resources/META-INF/plugin.xml (revision 11fceec01f5bdf96cb5570eb55433f4f9c063b98) @@ -220,6 +220,7 @@ +