From a74cd169db563b51eb379e4d728279c90cae7a19 Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Sat, 1 Jun 2024 21:55:02 +0800 Subject: [PATCH] feat: finish startup check --- .../activities/ProjectStartupActivity.kt | 32 +++++++++++---- .../MyApplicationActivationListener.kt | 12 ------ .../services/GradleProjectResolver.kt | 18 --------- .../leavesknife/services/GradleTaskManager.kt | 4 +- .../services/ProjectConfigService.kt | 26 ++++++------- .../toolWindow/PatchesToolWindowFactory.kt | 5 +-- src/main/resources/META-INF/plugin.xml | 5 +-- .../xor7/xiaohei/leavesknife/MyPluginTest.kt | 39 ------------------- 8 files changed, 41 insertions(+), 100 deletions(-) delete mode 100644 src/main/kotlin/cn/xor7/xiaohei/leavesknife/listeners/MyApplicationActivationListener.kt delete mode 100644 src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleProjectResolver.kt delete mode 100644 src/test/kotlin/cn/xor7/xiaohei/leavesknife/MyPluginTest.kt 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 8e062bd..ef9968a 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/activities/ProjectStartupActivity.kt @@ -1,21 +1,39 @@ package cn.xor7.xiaohei.leavesknife.activities +import cn.xor7.xiaohei.leavesknife.services.LEAVESKNIFE_CONFIG_FILE import cn.xor7.xiaohei.leavesknife.services.leavesknifeConfigService import com.intellij.openapi.project.Project +import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.startup.ProjectActivity import org.gradle.tooling.GradleConnector import org.gradle.tooling.model.idea.IdeaProject import java.io.File +import java.nio.file.Files +import java.nio.file.Paths class ProjectStartupActivity : ProjectActivity { override suspend fun execute(project: Project) { - val projectDir = project.basePath ?: return - GradleConnector.newConnector() - .forProjectDirectory(File(projectDir)) - .connect().use { connection -> - val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java) - project.leavesknifeConfigService.enablePlugin = - ideaProject.modules.any { it.name == "paper-api-generator" } + project.guessProjectDir()?.let { + if (Files.exists( + Paths.get( + it.path, + LEAVESKNIFE_CONFIG_FILE + ) + ) + ) { + project.leavesknifeConfigService.enablePlugin = true + return@let } + GradleConnector.newConnector() + .forProjectDirectory(File(it.path)) + .connect().use { connection -> + val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java) + if (ideaProject.modules.any { it.name == "paper-api-generator" }) { + // project.leavesknifeConfigService.enablePlugin = true + // TODO: 提示用户是否启用插件 + return@let + } + } + } } } \ No newline at end of file diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/listeners/MyApplicationActivationListener.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/listeners/MyApplicationActivationListener.kt deleted file mode 100644 index 4b0bbec..0000000 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/listeners/MyApplicationActivationListener.kt +++ /dev/null @@ -1,12 +0,0 @@ -package cn.xor7.xiaohei.leavesknife.listeners - -import com.intellij.openapi.application.ApplicationActivationListener -import com.intellij.openapi.diagnostic.thisLogger -import com.intellij.openapi.wm.IdeFrame - -internal class MyApplicationActivationListener : ApplicationActivationListener { - - override fun applicationActivated(ideFrame: IdeFrame) { - thisLogger().warn("Don't forget to remove all non-needed sample code files with their corresponding registration entries in `plugin.xml`.") - } -} diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleProjectResolver.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleProjectResolver.kt deleted file mode 100644 index f6ec3c7..0000000 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleProjectResolver.kt +++ /dev/null @@ -1,18 +0,0 @@ -package cn.xor7.xiaohei.leavesknife.services - -import com.intellij.openapi.externalSystem.model.DataNode -import com.intellij.openapi.externalSystem.model.project.ModuleData -import com.intellij.openapi.externalSystem.model.project.ProjectData -import org.gradle.tooling.model.idea.IdeaModule -import org.jetbrains.plugins.gradle.service.project.AbstractProjectResolverExtension - -class GradleProjectResolver : AbstractProjectResolverExtension() { - override fun populateModuleDependencies( - gradleModule: IdeaModule, - ideModule: DataNode, - ideProject: DataNode, - ) { - super.populateModuleDependencies(gradleModule, ideModule, ideProject) - println("populateModuleDependencies") - } -} \ No newline at end of file diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleTaskManager.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleTaskManager.kt index dea2a6e..b8075b6 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleTaskManager.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/GradleTaskManager.kt @@ -18,7 +18,5 @@ class GradleTaskManager : GradleTaskManagerExtension { return super.executeTasks(id, taskNames, projectPath, settings, jvmParametersSetup, listener) } - override fun cancelTask(id: ExternalSystemTaskId, listener: ExternalSystemTaskNotificationListener): Boolean { - return false - } + override fun cancelTask(id: ExternalSystemTaskId, listener: ExternalSystemTaskNotificationListener): Boolean = false } \ No newline at end of file diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt index 7d085f6..83264db 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectConfigService.kt @@ -1,27 +1,25 @@ package cn.xor7.xiaohei.leavesknife.services +import com.intellij.openapi.application.runInEdt import com.intellij.openapi.components.Service import com.intellij.openapi.project.Project -import com.intellij.openapi.project.guessProjectDir -import java.nio.file.Files -import java.nio.file.Paths +import com.intellij.openapi.wm.ToolWindowManager const val LEAVESKNIFE_CONFIG_FILE = "leavesknife.properties" @Service(Service.Level.PROJECT) -class ProjectConfigService(project: Project) { +class ProjectConfigService(private val project: Project) { var enablePlugin = false - - init { - project.guessProjectDir()?.let { - enablePlugin = Files.exists( - Paths.get( - it.path, - LEAVESKNIFE_CONFIG_FILE - ) - ) + set(value) { + field = value + println("enablePlugin: $value") + runInEdt { + ToolWindowManager + .getInstance(project) + .getToolWindow("Patches") + ?.isAvailable = value + } } - } } val Project.leavesknifeConfigService: ProjectConfigService 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 62139a8..7a1788b 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt @@ -1,7 +1,6 @@ package cn.xor7.xiaohei.leavesknife.toolWindow import cn.xor7.xiaohei.leavesknife.services.ProjectConfigService -import cn.xor7.xiaohei.leavesknife.services.leavesknifeConfigService import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.openapi.wm.ToolWindow @@ -20,8 +19,7 @@ class PatchesToolWindowFactory : ToolWindowFactory { toolWindow.contentManager.addContent(content) } - override suspend fun isApplicableAsync(project: Project): Boolean = - project.leavesknifeConfigService.enablePlugin + override suspend fun isApplicableAsync(project: Project): Boolean = true override fun shouldBeAvailable(project: Project) = false @@ -29,6 +27,7 @@ class PatchesToolWindowFactory : ToolWindowFactory { private val service = toolWindow.project.service() fun getContent() = JBPanel>().apply { + } } } \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 67f5acf..11d4963 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,4 +1,3 @@ - cn.xor7.xiaohei.leavesknife LeavesKnife @@ -15,16 +14,14 @@ id="Patches" anchor="right" icon="AllIcons.Vcs.Patch" + canCloseContents="true" /> - - diff --git a/src/test/kotlin/cn/xor7/xiaohei/leavesknife/MyPluginTest.kt b/src/test/kotlin/cn/xor7/xiaohei/leavesknife/MyPluginTest.kt deleted file mode 100644 index 8980072..0000000 --- a/src/test/kotlin/cn/xor7/xiaohei/leavesknife/MyPluginTest.kt +++ /dev/null @@ -1,39 +0,0 @@ -package cn.xor7.xiaohei.leavesknife - -import com.intellij.ide.highlighter.XmlFileType -import com.intellij.openapi.components.service -import com.intellij.psi.xml.XmlFile -import com.intellij.testFramework.TestDataPath -import com.intellij.testFramework.fixtures.BasePlatformTestCase -import com.intellij.util.PsiErrorElementUtil -import cn.xor7.xiaohei.leavesknife.services.ProjectConfigService - -@TestDataPath("\$CONTENT_ROOT/src/test/testData") -class MyPluginTest : BasePlatformTestCase() { - - fun testXMLFile() { - val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "bar") - val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java) - - assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile)) - - assertNotNull(xmlFile.rootTag) - - xmlFile.rootTag?.let { - assertEquals("foo", it.name) - assertEquals("bar", it.value.text) - } - } - - fun testRename() { - myFixture.testRename("foo.xml", "foo_after.xml", "a2") - } - - fun testProjectService() { - val projectService = project.service() - - assertNotSame(projectService.getRandomNumber(), projectService.getRandomNumber()) - } - - override fun getTestDataPath() = "src/test/testData/rename" -}