Skip to content

Commit

Permalink
feat: add project detect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MC-XiaoHei committed Jun 1, 2024
1 parent 2d04e1d commit b6f279a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.xor7.xiaohei.leavesknife.activities

import cn.xor7.xiaohei.leavesknife.services.ProjectConfigService
import cn.xor7.xiaohei.leavesknife.services.leavesknifeConfigService
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import org.gradle.tooling.GradleConnector
Expand All @@ -9,18 +11,14 @@ import java.io.File

class ProjectStartupActivity : ProjectActivity {
override suspend fun execute(project: Project) {
println("Project opened.")
val projectDir = project.basePath ?: return
GradleConnector.newConnector()
.forProjectDirectory(File(projectDir))
.connect().use { connection ->
val ideaProject: IdeaProject = connection.getModel(IdeaProject::class.java)
for (module in ideaProject.modules) {
println("Module: ${module.name}")
for (dependency in module.dependencies) {
println(" Dependency: ${dependency}")
}
}
project.leavesknifeConfigService.enablePlugin =
ideaProject.modules
.filter { it.name == "paper-api-generator" }.isNotEmpty()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ class ProjectConfigService(project: Project) {
}
}
}

val Project.leavesknifeConfigService: ProjectConfigService
get() = this.getService(ProjectConfigService::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cn.xor7.xiaohei.leavesknife.toolWindow

import cn.xor7.xiaohei.leavesknife.MyBundle
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
Expand All @@ -14,18 +15,20 @@ import javax.swing.JButton
class PatchesToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val serverPatchesToolWindow = ServerPatchesToolWindow(toolWindow)
val content = ContentFactory.getInstance().createContent(serverPatchesToolWindow.getContent(), null, false)
val content = ContentFactory.getInstance().createContent(
serverPatchesToolWindow.getContent(),
null,
false
)
toolWindow.contentManager.addContent(content)
}

override suspend fun isApplicableAsync(project: Project): Boolean {
return project.getService(ProjectConfigService::class.java).enablePlugin
}
override suspend fun isApplicableAsync(project: Project): Boolean =
project.leavesknifeConfigService.enablePlugin

override fun shouldBeAvailable(project: Project) = false

class ServerPatchesToolWindow(toolWindow: ToolWindow) {

private val service = toolWindow.project.service<ProjectConfigService>()

fun getContent() = JBPanel<JBPanel<*>>().apply {
Expand Down

0 comments on commit b6f279a

Please sign in to comment.