Skip to content

Commit

Permalink
Change how creator finalizers are run
Browse files Browse the repository at this point in the history
Should help with some weird deadlocks in future IDE versions
  • Loading branch information
RedNesto committed Jul 14, 2024
1 parent 246c1d5 commit 11fceec
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main/kotlin/creator/custom/CustomPlatformStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.intellij.ide.wizard.AbstractNewProjectWizardStep
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
Expand Down Expand Up @@ -502,22 +503,26 @@ class CustomPlatformStep(
}
}

application.executeOnPooledThread {
application.invokeLater({
application.runWriteAction {
LocalFileSystem.getInstance().refresh(false)
// Apparently a module root is required for the reformat to work
setupTempRootModule(project, projectPath)
}
val finalizeAction = {
WriteAction.runAndWait<Throwable> {
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) {
Expand Down
60 changes: 60 additions & 0 deletions src/main/kotlin/creator/custom/TemplateService.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<String, suspend () -> 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)
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
<!---->

<moduleBuilder id="CUSTOM_MINECRAFT_MODULE" builderClass="com.demonwav.mcdev.creator.custom.CustomMinecraftModuleBuilder"/>
<postStartupActivity implementation="com.demonwav.mcdev.creator.custom.TemplateProjectFinalizerActivity"/>
<moduleBuilder id="MINECRAFT_MODULE" builderClass="com.demonwav.mcdev.creator.MinecraftModuleBuilder"/>
<facetType implementation="com.demonwav.mcdev.facet.MinecraftFacetType" />
<postStartupActivity implementation="com.demonwav.mcdev.facet.MinecraftFacetDetector"/>
Expand Down

0 comments on commit 11fceec

Please sign in to comment.