generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbe74b0
commit 4287bf7
Showing
4 changed files
with
103 additions
and
70 deletions.
There are no files selected for viewing
66 changes: 50 additions & 16 deletions
66
src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,68 @@ | ||
package cn.xor7.xiaohei.leavesknife.activities | ||
|
||
import cn.xor7.xiaohei.leavesknife.services.PatchType | ||
import cn.xor7.xiaohei.leavesknife.services.PatchesInfo | ||
import cn.xor7.xiaohei.leavesknife.services.leavesknifeStoreService | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.project.guessProjectDir | ||
import com.intellij.openapi.startup.ProjectActivity | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.gradle.tooling.GradleConnector | ||
import org.gradle.tooling.model.idea.IdeaProject | ||
import java.io.File | ||
import java.nio.file.Files | ||
import kotlin.io.path.inputStream | ||
|
||
|
||
class ProjectStartupActivity : ProjectActivity { | ||
override suspend fun execute(project: Project) { | ||
val store = project.leavesknifeStoreService | ||
project.guessProjectDir()?.let { projectDir -> | ||
if (Files.exists(store.configPath)) { | ||
// TODO 检查配置文件合法性 合法则设置 enablePlugin 为 true | ||
override suspend fun execute(project: Project) = with(project.leavesknifeStoreService) { | ||
if (Files.exists(configPath)) { | ||
configPath.inputStream().use { | ||
properties.load(it) | ||
try { | ||
patchesInfo[PatchType.SERVER] = PatchesInfo( | ||
properties.getProperty("patches.server.module")!!, | ||
properties.getProperty("patches.server.path")!!, | ||
properties.getProperty("patches.server.base")!! | ||
) | ||
patchesInfo[PatchType.API] = PatchesInfo( | ||
properties.getProperty("patches.api.module")!!, | ||
properties.getProperty("patches.api.path")!!, | ||
properties.getProperty("patches.api.base")!! | ||
) | ||
patchesInfo[PatchType.GENERATED_API] = PatchesInfo( | ||
properties.getProperty("patches.generated-api.module")!!, | ||
properties.getProperty("patches.generated-api.path")!!, | ||
properties.getProperty("patches.generated-api.base")!! | ||
) | ||
enablePlugin = true | ||
println(patchesInfo) | ||
} catch (_: Exception) { | ||
thisLogger().warn("Failed to read plugin config") | ||
} | ||
} | ||
GradleConnector.newConnector() | ||
.forProjectDirectory(File(projectDir.path)) | ||
.connect().use { connection -> | ||
val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java) | ||
store.modulePaths = ideaProject.modules.filter { | ||
it.gradleProject.path != ":" | ||
}.associateTo(mutableMapOf()) { | ||
it.name to it.contentRoots.first().rootDirectory.absolutePath | ||
} | ||
scanModules(project) | ||
if (!modulePaths.containsKey("paper-api-generator") && !enablePlugin) return | ||
if (!enablePlugin) needConfigure = true | ||
} | ||
|
||
private suspend fun scanModules(project: Project) { | ||
withContext(Dispatchers.IO) { | ||
project.guessProjectDir()?.let { projectDir -> | ||
GradleConnector.newConnector() | ||
.forProjectDirectory(File(projectDir.path)) | ||
.connect().use { connection -> | ||
val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java) | ||
project.leavesknifeStoreService.modulePaths = ideaProject.modules.filter { | ||
it.gradleProject.path != ":" | ||
}.associateTo(mutableMapOf()) { | ||
it.name to it.contentRoots.first().rootDirectory.absolutePath | ||
} | ||
} | ||
if (!store.modulePaths.containsKey("paper-api-generator") && !store.enablePlugin) return@let | ||
if (!store.enablePlugin) store.needConfigure = true | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters