Skip to content

Commit

Permalink
Add ProjectReimporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Sep 22, 2023
1 parent e3ecfb7 commit 164ef43
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/kotlin/facet/MinecraftFacetConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import com.intellij.util.xmlb.annotations.XCollection
class MinecraftFacetConfiguration : FacetConfiguration, PersistentStateComponent<MinecraftFacetConfigurationData> {

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))
Expand All @@ -50,4 +52,6 @@ data class MinecraftFacetConfigurationData(
var autoDetectTypes: MutableSet<PlatformType> = mutableSetOf(),
@Tag("forgePatcher")
var forgePatcher: Boolean = false,
@Tag("projectReimportVersion")
var projectReimportVersion: Int = 0,
)
10 changes: 10 additions & 0 deletions src/main/kotlin/facet/MinecraftFacetDetector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class MinecraftFacetDetector : StartupActivity {

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)
Expand All @@ -78,8 +81,15 @@ class MinecraftFacetDetector : StartupActivity {
checkNoFacet(module)
} else {
checkExistingFacet(module, minecraftFacet)
if (ProjectReimporter.needsReimport(minecraftFacet)) {
needsReimport = true
}
}
}

if (needsReimport) {
ProjectReimporter.reimport(project)
}
}

private fun checkNoFacet(module: Module) {
Expand Down
63 changes: 63 additions & 0 deletions src/main/kotlin/facet/ProjectReimporter.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<ProjectReimporter>()

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")
}
}
}

0 comments on commit 164ef43

Please sign in to comment.