Skip to content

Commit

Permalink
Merge pull request #260 from JetBrains/refactor-services
Browse files Browse the repository at this point in the history
Refactor services
  • Loading branch information
rafaelldi authored Sep 30, 2024
2 parents 6f66402 + 6001db1 commit 343c1f9
Show file tree
Hide file tree
Showing 43 changed files with 835 additions and 601 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jetbrains.rider.aspire

import com.intellij.execution.RunManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.jetbrains.rider.build.tasks.BuildProjectBeforeRunTask
import com.jetbrains.rider.build.tasks.BuildSolutionBeforeRunTaskProvider

class MigrateBuildSolutionTaskActivity : ProjectActivity {
override suspend fun execute(project: Project) {
RunManager.getInstance(project).allSettings.forEach { setting ->
if (setting.type.id == "AspireHostConfiguration") {
val buildSolutionTasks = setting.configuration.beforeRunTasks.filter {
it.providerId == BuildSolutionBeforeRunTaskProvider.providerId
}
if (buildSolutionTasks.isNotEmpty()) {
buildSolutionTasks.forEach {
setting.configuration.beforeRunTasks.remove(it)
}
val buildSolutionTask = BuildProjectBeforeRunTask()
buildSolutionTask.isEnabled = true
setting.configuration.beforeRunTasks.add(0, buildSolutionTask)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.jetbrains.rider.aspire.manifest.ManifestService
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.aspire.services.AspireHostManager
import com.jetbrains.rider.aspire.util.ASPIRE_HOST_PATH

class AspireManifestAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val hostPath = event.getData(ASPIRE_HOST_PATH) ?: return
val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
?: return
val hostProjectPath = hostService.projectPath
val hostProjectPath = hostService.hostProjectPath

ManifestService.getInstance(project).generateManifest(hostProjectPath)
}
Expand All @@ -28,9 +28,9 @@ class AspireManifestAction : AnAction() {
return
}

val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
if (hostService == null) {
event.presentation.isEnabledAndVisible = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.intellij.ide.BrowserUtil
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.aspire.services.AspireHostManager
import com.jetbrains.rider.aspire.util.ASPIRE_HOST_PATH

class AspireOpenDashboardAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val hostPath = event.getData(ASPIRE_HOST_PATH) ?: return
val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
?: return
val dashboardUrl = hostService.dashboardUrl
if (dashboardUrl.isNullOrEmpty()) return
Expand All @@ -29,9 +29,9 @@ class AspireOpenDashboardAction : AnAction() {
return
}

val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
if (hostService == null || !hostService.isActive || hostService.dashboardUrl.isNullOrEmpty()) {
event.presentation.isEnabledAndVisible = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.jetbrains.rider.aspire.run.AspireHostRunManager
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.aspire.services.AspireHostManager
import com.jetbrains.rider.aspire.util.ASPIRE_HOST_PATH

class DebugHostAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val hostPath = event.getData(ASPIRE_HOST_PATH) ?: return
val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
?: return

AspireHostRunManager.getInstance(project)
Expand All @@ -28,9 +28,9 @@ class DebugHostAction : AnAction() {
return
}

val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
if (hostService == null) {
event.presentation.isEnabledAndVisible = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.jetbrains.rider.aspire.run.AspireHostRunManager
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.aspire.services.AspireHostManager
import com.jetbrains.rider.aspire.util.ASPIRE_HOST_PATH

class RunHostAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val hostPath = event.getData(ASPIRE_HOST_PATH) ?: return
val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
?: return

AspireHostRunManager.getInstance(project)
Expand All @@ -28,9 +28,9 @@ class RunHostAction : AnAction() {
return
}

val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
if (hostService == null) {
event.presentation.isEnabledAndVisible = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.jetbrains.rider.aspire.AspireService
import com.jetbrains.rider.aspire.run.AspireHostRunManager
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.aspire.services.AspireHostManager
import com.jetbrains.rider.aspire.util.ASPIRE_HOST_PATH
import kotlinx.coroutines.launch

class StopHostAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val hostPath = event.getData(ASPIRE_HOST_PATH) ?: return
val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
?: return

AspireService.getInstance(project).scope.launch {
Expand All @@ -32,9 +32,9 @@ class StopHostAction : AnAction() {
return
}

val hostService = AspireServiceManager
val hostService = AspireHostManager
.getInstance(project)
.getHostService(hostPath)
.getAspireHost(hostPath)
if (hostService == null) {
event.presentation.isEnabledAndVisible = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.jetbrains.rider.aspire.database

import com.intellij.openapi.project.Project
import com.jetbrains.rider.aspire.generated.ResourceType
import com.jetbrains.rider.aspire.services.AspireResourceService
import com.jetbrains.rider.aspire.services.ResourceListener
import com.jetbrains.rider.aspire.services.AspireResource
import java.net.URI

class DatabaseResourceListener(private val project: Project) : ResourceListener {
Expand All @@ -18,15 +18,15 @@ class DatabaseResourceListener(private val project: Project) : ResourceListener
private const val REDIS = "redis"
}

override fun resourceCreated(resource: AspireResourceService) {
override fun resourceCreated(resource: AspireResource) {
applyChanges(resource)
}

override fun resourceUpdated(resource: AspireResourceService) {
override fun resourceUpdated(resource: AspireResource) {
applyChanges(resource)
}

private fun applyChanges(resource: AspireResourceService) {
private fun applyChanges(resource: AspireResource) {
val service = DatabaseService.getInstance(project)
if (resource.type == ResourceType.Project) {
resource.environment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jetbrains.rider.aspire.listeners

import com.intellij.util.messages.Topic
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rider.aspire.generated.AspireSessionHostModel
import com.jetbrains.rider.aspire.run.AspireHostConfig
import java.nio.file.Path

interface AspireSessionHostListener {
companion object {
@Topic.ProjectLevel
val TOPIC = Topic("AspireSessionHostListener", AspireSessionHostListener::class.java)
}

fun configCreated(aspireHostProjectPath: Path, config: AspireHostConfig)
fun modelCreated(aspireHostProjectPath: Path, sessionHostModel: AspireSessionHostModel, lifetime: Lifetime)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ data class AspireHostConfig(
val debugSessionToken: String,
//from env: DEBUG_SESSION_PORT
val debugSessionPort: Int,
val aspireHostProjectPath: Path,
//from env: ASPNETCORE_URLS
val aspireHostProjectUrl: String?,
val debuggingMode: Boolean,
//from env: DOTNET_RESOURCE_SERVICE_ENDPOINT_URL
val resourceServiceEndpointUrl: String?,
//from env: DOTNET_DASHBOARD_RESOURCESERVICE_APIKEY
val resourceServiceApiKey: String?,
val aspireHostLifetime: Lifetime,
val hostRunConfiguration: AspireHostConfiguration?
val aspireHostProjectPath: Path,
val aspireHostProjectUrl: String?,
val aspireHostRunConfiguration: AspireHostConfiguration?
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.jetbrains.rider.aspire.run
import com.intellij.execution.configurations.ConfigurationFactory
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.openapi.project.Project
import com.jetbrains.rider.build.tasks.SolutionRunConfiguration
import com.jetbrains.rider.run.configurations.IAutoSelectableRunConfiguration
import com.jetbrains.rider.run.configurations.IProjectBasedRunConfiguration
import com.jetbrains.rider.run.configurations.RiderAsyncRunConfiguration
import org.jdom.Element

Expand All @@ -19,7 +19,7 @@ class AspireHostConfiguration(
factory,
{ AspireHostSettingsEditor(it) },
AspireHostExecutorFactory(project, parameters)
), SolutionRunConfiguration, IAutoSelectableRunConfiguration {
), IProjectBasedRunConfiguration, IAutoSelectableRunConfiguration {
override fun checkConfiguration() {
parameters.validate()
}
Expand All @@ -46,4 +46,10 @@ class AspireHostConfiguration(
}

override fun getAutoSelectPriority() = 10

override fun getProjectFilePath() = parameters.projectFilePath

override fun setProjectFilePath(path: String) {
parameters.projectFilePath = path
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
import com.jetbrains.rd.util.lifetime.isNotAlive
import com.jetbrains.rider.aspire.services.AspireHostService
import com.jetbrains.rider.aspire.services.AspireHost
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.nio.file.Path
Expand All @@ -38,7 +38,7 @@ class AspireHostRunManager(private val project: Project) {
configurationLifetimes[aspireHostProjectPath] = aspireHostLifetime
}

fun executeConfigurationForHost(host: AspireHostService, underDebug: Boolean) {
fun executeConfigurationForHost(host: AspireHost, underDebug: Boolean) {
val executor =
if (underDebug) DefaultDebugExecutor.getDebugExecutorInstance()
else DefaultRunExecutor.getRunExecutorInstance()
Expand All @@ -47,7 +47,7 @@ class AspireHostRunManager(private val project: Project) {
val selected = runManager.selectedConfiguration
val selectedConfiguration = selected?.configuration
if (selectedConfiguration != null && selectedConfiguration is AspireHostConfiguration) {
if (host.projectPath == Path(selectedConfiguration.parameters.projectFilePath)) {
if (host.hostProjectPath == Path(selectedConfiguration.parameters.projectFilePath)) {
ProgramRunnerUtil.executeConfiguration(selected, executor)
return
}
Expand All @@ -57,15 +57,15 @@ class AspireHostRunManager(private val project: Project) {
.filter {
val path = (it.configuration as? AspireHostConfiguration)?.parameters?.projectFilePath
?: return@filter false
Path(path) == host.projectPath
Path(path) == host.hostProjectPath
}

if (configurations.isEmpty()) {
LOG.warn("Unable to find Aspire host run configurations")
return
}

val configurationName = configurationNames[host.projectPath]
val configurationName = configurationNames[host.hostProjectPath]
if (configurationName != null) {
val configurationWithName = configurations.firstOrNull { it.name == configurationName }
if (configurationWithName != null) {
Expand All @@ -78,10 +78,10 @@ class AspireHostRunManager(private val project: Project) {
ProgramRunnerUtil.executeConfiguration(firstConfiguration, executor)
}

suspend fun stopConfigurationForHost(host: AspireHostService) {
val lifetime = configurationLifetimes[host.projectPath]
suspend fun stopConfigurationForHost(host: AspireHost) {
val lifetime = configurationLifetimes[host.hostProjectPath]
if (lifetime == null || lifetime.lifetime.isNotAlive) {
LOG.warn("Unable to stop configuration for Aspire host ${host.projectPathString}")
LOG.warn("Unable to stop configuration for Aspire host ${host.hostProjectPathString}")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.jetbrains.rider.aspire.AspireService
import com.jetbrains.rider.aspire.run.AspireHostConfig
import com.jetbrains.rider.aspire.run.AspireHostConfiguration
import com.jetbrains.rider.aspire.run.states.AspireHostDebugProfileState
import com.jetbrains.rider.aspire.services.AspireServiceManager
import com.jetbrains.rider.debugger.DebuggerWorkerProcessHandler
import com.jetbrains.rider.debugger.DotNetDebugRunner
import com.jetbrains.rider.model.debuggerWorker.DebuggerWorkerModel
Expand Down Expand Up @@ -54,17 +53,16 @@ class AspireHostDebugProgramRunner : DotNetDebugRunner() {

LOG.trace { "Aspire session host config: $config" }

startSessionHostAndSubscribe(
saveRunConfiguration(
environment.project,
config,
config.aspireHostProjectPath,
config.name,
aspireHostLifetimeDefinition
)

val executionResult = state.execute(environment.executor, this, workerProcessHandler, sessionLifetime)
startSessionHostAndSubscribe(config, environment.project)

AspireServiceManager
.getInstance(environment.project)
.updateAspireHostService(config.aspireHostProjectPath, executionResult)
val executionResult = state.execute(environment.executor, this, workerProcessHandler, sessionLifetime)

connectExecutionHandlerAndLifetime(executionResult, aspireHostLifetimeDefinition)

Expand Down
Loading

0 comments on commit 343c1f9

Please sign in to comment.