From 512b24776ad00a8128245436e1cd5e2a647a5748 Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Thu, 6 Jun 2024 22:01:35 +0800 Subject: [PATCH] feat: fetch module path --- .../activities/ProjectStartupActivity.kt | 27 +++++++++---------- ...onfigService.kt => ProjectStoreService.kt} | 12 ++++----- .../toolWindow/PatchesToolWindowFactory.kt | 4 +-- 3 files changed, 20 insertions(+), 23 deletions(-) rename src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/{ProjectConfigService.kt => ProjectStoreService.kt} (81%) diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt index f2a7a90..f471a45 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt @@ -1,7 +1,7 @@ package cn.xor7.xiaohei.leavesknife.activities import cn.xor7.xiaohei.leavesknife.services.LEAVESKNIFE_CONFIG_FILE -import cn.xor7.xiaohei.leavesknife.services.leavesknifeConfigService +import cn.xor7.xiaohei.leavesknife.services.leavesknifeStoreService import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.startup.ProjectActivity @@ -11,27 +11,24 @@ import java.io.File import java.nio.file.Files import java.nio.file.Paths + class ProjectStartupActivity : ProjectActivity { override suspend fun execute(project: Project) { - project.guessProjectDir()?.let { - if (Files.exists( - Paths.get( - it.path, - LEAVESKNIFE_CONFIG_FILE - ) - ) - ) { - // TODO 检查配置文件合法性 - return@let + val store = project.leavesknifeStoreService + project.guessProjectDir()?.let { projectDir -> + if (Files.exists(Paths.get(projectDir.path, LEAVESKNIFE_CONFIG_FILE))) { + // TODO 检查配置文件合法性 合法则设置 enablePlugin 为 true 否则设置 needConfigure 为 true + store.enablePlugin = true } GradleConnector.newConnector() - .forProjectDirectory(File(it.path)) + .forProjectDirectory(File(projectDir.path)) .connect().use { connection -> val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java) - if (ideaProject.modules.any { it.name == "paper-api-generator" }) { - project.leavesknifeConfigService.needConfigure = true - return@let + store.modulePaths = ideaProject.modules.associateTo(mutableMapOf()) { + it.name to it.contentRoots.first().rootDirectory.absolutePath } + if (!store.modulePaths.containsKey("paper-api-generator") && !store.enablePlugin) return@let + store.enablePlugin = true } } } diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt similarity index 81% rename from src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt rename to src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt index c13f02e..152be6b 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt @@ -14,7 +14,7 @@ import com.intellij.openapi.wm.ToolWindowManager const val LEAVESKNIFE_CONFIG_FILE = "leavesknife.properties" @Service(Service.Level.PROJECT) -class ProjectConfigService(private val project: Project) { +class ProjectStoreService(private val project: Project) { var enablePlugin = false set(value) { field = value @@ -27,6 +27,7 @@ class ProjectConfigService(private val project: Project) { } var needConfigure = false set(value) { + if (value) enablePlugin = false if (field == value) return field = value if (value) { @@ -37,9 +38,7 @@ class ProjectConfigService(private val project: Project) { CommonBundle.message("notification.configure.title"), NotificationType.INFORMATION ) - .addAction(object : NotificationAction( - CommonBundle.message("notification.configure.action") - ) { + .addAction(object : NotificationAction(CommonBundle.message("notification.configure.action")) { override fun actionPerformed(e: AnActionEvent, notification: Notification) { // TODO 打开配置窗口 notification.hideBalloon() @@ -48,7 +47,8 @@ class ProjectConfigService(private val project: Project) { .notify(project) } } + var modulePaths: MutableMap = mutableMapOf() } -val Project.leavesknifeConfigService: ProjectConfigService - get() = this.getService(ProjectConfigService::class.java) \ No newline at end of file +val Project.leavesknifeStoreService: ProjectStoreService + get() = this.getService(ProjectStoreService::class.java) \ No newline at end of file diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt index 7a1788b..0d6c176 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt @@ -1,6 +1,6 @@ package cn.xor7.xiaohei.leavesknife.toolWindow -import cn.xor7.xiaohei.leavesknife.services.ProjectConfigService +import cn.xor7.xiaohei.leavesknife.services.ProjectStoreService import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.openapi.wm.ToolWindow @@ -24,7 +24,7 @@ class PatchesToolWindowFactory : ToolWindowFactory { override fun shouldBeAvailable(project: Project) = false class ServerPatchesToolWindow(toolWindow: ToolWindow) { - private val service = toolWindow.project.service() + private val service = toolWindow.project.service() fun getContent() = JBPanel>().apply {