Skip to content

Commit

Permalink
Merge pull request #212 from JetBrains/fix-209
Browse files Browse the repository at this point in the history
Get the focused project from the context while restarting/stopping a resource
  • Loading branch information
rafaelldi authored Jul 19, 2024
2 parents 7a57a95 + d6875bb commit 61ef2ae
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@file:Suppress("UnstableApiUsage")

package me.rafaelldi.aspire.actions.dashboard

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.progress.currentThreadCoroutineScope
import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.workspaceModel.ide.toPath
import com.jetbrains.rider.projectView.workspace.containingProjectEntity
import com.jetbrains.rider.projectView.workspace.getProjectModelEntity
import kotlinx.coroutines.launch
import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.AspireService
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
Expand All @@ -17,17 +22,31 @@ import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID
class RestartDebugResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID) ?: return
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE) ?: return
val resourceState = event.getData(ASPIRE_RESOURCE_STATE) ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

AspireService.getInstance(project).scope.launch {
withBackgroundProgress(project, AspireBundle.message("progress.restart.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, true)
if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, true)
}
}
}
} else {
val projectEntity = event.dataContext
.getProjectModelEntity(true)
?.containingProjectEntity()
?: return
val projectPath = projectEntity.url?.toPath() ?: return
val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath) ?: return

currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceId, true)
}
}
}
}
Expand All @@ -40,24 +59,49 @@ class RestartDebugResourceAction : AnAction() {
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)
if (resourceUid == null || resourceType == null || resourceState == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
} else {
val projectEntity = event.dataContext.getProjectModelEntity(true)?.containingProjectEntity()

if (projectEntity == null) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
val projectPath = projectEntity.url?.toPath()
if (projectPath == null) {
event.presentation.isEnabledAndVisible = false
return
}

val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath)
if (resourceId == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceId)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
}
}

override fun getActionUpdateThread() = ActionUpdateThread.EDT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@file:Suppress("UnstableApiUsage")

package me.rafaelldi.aspire.actions.dashboard

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.progress.currentThreadCoroutineScope
import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.workspaceModel.ide.toPath
import com.jetbrains.rider.projectView.workspace.containingProjectEntity
import com.jetbrains.rider.projectView.workspace.getProjectModelEntity
import kotlinx.coroutines.launch
import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.AspireService
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
Expand All @@ -17,17 +22,31 @@ import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID
class RestartResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID) ?: return
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE) ?: return
val resourceState = event.getData(ASPIRE_RESOURCE_STATE) ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

AspireService.getInstance(project).scope.launch {
withBackgroundProgress(project, AspireBundle.message("progress.restart.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, false)
if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceUid, false)
}
}
}
} else {
val projectEntity = event.dataContext
.getProjectModelEntity(true)
?.containingProjectEntity()
?: return
val projectPath = projectEntity.url?.toPath() ?: return
val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath) ?: return

currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).restartResource(resourceId, false)
}
}
}
}
Expand All @@ -40,24 +59,49 @@ class RestartResourceAction : AnAction() {
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)
if (resourceUid == null || resourceType == null || resourceState == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
} else {
val projectEntity = event.dataContext.getProjectModelEntity(true)?.containingProjectEntity()

if (projectEntity == null) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
val projectPath = projectEntity.url?.toPath()
if (projectPath == null) {
event.presentation.isEnabledAndVisible = false
return
}

val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath)
if (resourceId == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceId)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
}
}

override fun getActionUpdateThread() = ActionUpdateThread.EDT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@file:Suppress("UnstableApiUsage")

package me.rafaelldi.aspire.actions.dashboard

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.progress.currentThreadCoroutineScope
import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.workspaceModel.ide.toPath
import com.jetbrains.rider.projectView.workspace.containingProjectEntity
import com.jetbrains.rider.projectView.workspace.getProjectModelEntity
import kotlinx.coroutines.launch
import me.rafaelldi.aspire.AspireBundle
import me.rafaelldi.aspire.AspireService
import me.rafaelldi.aspire.generated.ResourceState
import me.rafaelldi.aspire.generated.ResourceType
import me.rafaelldi.aspire.sessionHost.SessionManager
Expand All @@ -17,17 +22,31 @@ import me.rafaelldi.aspire.util.ASPIRE_RESOURCE_UID
class StopResourceAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID) ?: return
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE) ?: return
val resourceState = event.getData(ASPIRE_RESOURCE_STATE) ?: return
val resourceUid = event.getData(ASPIRE_RESOURCE_UID)

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

AspireService.getInstance(project).scope.launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).stopResource(resourceUid)
if (resourceType == ResourceType.Project && resourceState == ResourceState.Running) {
currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).stopResource(resourceUid)
}
}
}
} else {
val projectEntity = event.dataContext
.getProjectModelEntity(true)
?.containingProjectEntity()
?: return
val projectPath = projectEntity.url?.toPath() ?: return
val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath) ?: return

currentThreadCoroutineScope().launch {
withBackgroundProgress(project, AspireBundle.message("progress.stop.resource")) {
SessionManager.getInstance(project).stopResource(resourceId)
}
}
}
}
Expand All @@ -40,24 +59,49 @@ class StopResourceAction : AnAction() {
}

val resourceUid = event.getData(ASPIRE_RESOURCE_UID)
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)
if (resourceUid == null || resourceType == null || resourceState == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceUid != null) {
val resourceType = event.getData(ASPIRE_RESOURCE_TYPE)
val resourceState = event.getData(ASPIRE_RESOURCE_STATE)

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}
if (resourceType != ResourceType.Project || resourceState != ResourceState.Running) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceUid)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
} else {
val projectEntity = event.dataContext.getProjectModelEntity(true)?.containingProjectEntity()

if (projectEntity == null) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
val projectPath = projectEntity.url?.toPath()
if (projectPath == null) {
event.presentation.isEnabledAndVisible = false
return
}

val resourceId = SessionManager.getInstance(project).getResourceIdByProject(projectPath)
if (resourceId == null) {
event.presentation.isEnabledAndVisible = false
return
}

if (!SessionManager.getInstance(project).isResourceRunning(resourceId)) {
event.presentation.isEnabledAndVisible = false
return
}

event.presentation.isEnabledAndVisible = true
}
}

override fun getActionUpdateThread() = ActionUpdateThread.EDT
Expand Down
Loading

0 comments on commit 61ef2ae

Please sign in to comment.