|
| 1 | +package cc.unitmesh.endpoints.bridge |
| 2 | + |
| 3 | +import cc.unitmesh.devti.bridge.ArchViewCommand |
| 4 | +import cc.unitmesh.devti.provider.toolchain.ToolchainFunctionProvider |
| 5 | +import com.intellij.microservices.endpoints.EndpointsProvider |
| 6 | +import com.intellij.microservices.endpoints.EndpointsUrlTargetProvider |
| 7 | +import com.intellij.microservices.endpoints.ModuleEndpointsFilter |
| 8 | +import com.intellij.openapi.application.runReadAction |
| 9 | +import com.intellij.openapi.project.Project |
| 10 | +import com.intellij.openapi.project.modules |
| 11 | +import com.intellij.spring.model.SpringBeanPointer |
| 12 | +import com.intellij.spring.mvc.mapping.UrlMappingElement |
| 13 | +import org.jetbrains.kotlin.idea.base.facet.isTestModule |
| 14 | + |
| 15 | +class WebApiViewFunctionProvider : ToolchainFunctionProvider { |
| 16 | + override fun funcNames(): List<String> = listOf(ArchViewCommand.WebApiView.name) |
| 17 | + |
| 18 | + override fun isApplicable(project: Project, funcName: String): Boolean = funcName == ArchViewCommand.WebApiView.name |
| 19 | + |
| 20 | + override fun execute( |
| 21 | + project: Project, |
| 22 | + prop: String, |
| 23 | + args: List<Any>, |
| 24 | + allVariables: Map<String, Any?> |
| 25 | + ): Any { |
| 26 | + val model = runReadAction { EndpointsProvider.getAvailableProviders(project).toList() } |
| 27 | + if (model.isEmpty()) return "Cannot find any endpoints" |
| 28 | + |
| 29 | + val availableProviders = model |
| 30 | + .filter { it.getStatus(project) == EndpointsProvider.Status.HAS_ENDPOINTS } |
| 31 | + .filterIsInstance<EndpointsUrlTargetProvider<SpringBeanPointer<*>, UrlMappingElement>>() |
| 32 | + |
| 33 | +// return availableProviders.joinToString("\n") { |
| 34 | +// it.getEndpointGroups(project, ModuleEndpointsFilter(project)) |
| 35 | +// "This project has http endpoints from ${it.presentation.title}" |
| 36 | +// } |
| 37 | + val modules = project.modules |
| 38 | + val groups = modules.map { module -> |
| 39 | + val moduleEndpointsFilter = ModuleEndpointsFilter(module, false, module.isTestModule) |
| 40 | + availableProviders.map { provider -> |
| 41 | + provider.getEndpointGroups(project, moduleEndpointsFilter) |
| 42 | + }.flatten() |
| 43 | + }.flatten() |
| 44 | + |
| 45 | + val map: List<UrlMappingElement> = groups.map { group -> |
| 46 | + availableProviders.map { |
| 47 | + it.getEndpoints(group) |
| 48 | + }.flatten() |
| 49 | + }.flatten() |
| 50 | + |
| 51 | + return map.joinToString("\n") |
| 52 | + } |
| 53 | +} |
0 commit comments