Skip to content

Commit

Permalink
refactor: use project graph data structure directly (#2326)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Nov 28, 2024
1 parent b3cc9c5 commit 438659d
Show file tree
Hide file tree
Showing 74 changed files with 732 additions and 509 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Thumbs.db
.qodana
apps/intellij/bin/
.intellijPlatform
.kotlin/


# WDIO
Expand Down
1 change: 0 additions & 1 deletion .nxignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ class NxAngularConfigService(private val project: Project, private val cs: Corou
return
}
val projectFiles =
workspace.workspace.projects.values
workspace.projectGraph.nodes.values
.asSequence()
// TODO: use framework metadata in the future, for now just register all projects
// .filter { project ->
// project.targets.values.any { it.executor.contains("angular") }
// }
.mapNotNull { project ->
workspaceRoot
.findFileByRelativePath(project.root)
.findFileByRelativePath(project.data.root)
?.findFileByRelativePath("project.json")
?.let { Pair(project.name, it) }
}
Expand All @@ -100,7 +100,7 @@ class NxAngularConfigService(private val project: Project, private val cs: Corou
ProjectRootManagerEx.getInstanceEx(project)
.makeRootsChange(
EmptyRunnable.getInstance(),
RootsChangeRescanningInfo.RESCAN_DEPENDENCIES_IF_NEEDED
RootsChangeRescanningInfo.RESCAN_DEPENDENCIES_IF_NEEDED,
)
}
}
Expand All @@ -114,7 +114,7 @@ class NxAngularConfigService(private val project: Project, private val cs: Corou
propertyElementName = "projects",
entryTagName = "project",
keyAttributeName = "name",
valueAttributeName = "file"
valueAttributeName = "file",
)
var projects: MutableMap<String, String> = mutableMapOf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.DumbAware
import com.intellij.util.application
import dev.nx.console.generate.NxGenerateService
import dev.nx.console.nx_toolwindow.NxToolWindowPanel
Expand All @@ -15,7 +16,7 @@ import kotlinx.coroutines.launch

private val logger = logger<NxGenerateUiAction>()

class NxGenerateUiAction : AnAction() {
class NxGenerateUiAction : AnAction(), DumbAware {
override fun getActionUpdateThread() = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
Expand All @@ -36,7 +37,7 @@ class NxGenerateUiAction : AnAction() {
if (ActionPlaces.isPopupPlace(e.place))
TelemetryEventSource.EXPLORER_CONTEXT_MENU
else TelemetryEventSource.COMMAND
)
),
)

val generateService = project.service<NxGenerateService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ open class NxReMoveProjectActionBase(val mode: String) : AnAction() {
val projectsWithType =
nxlsService
.workspace()
?.workspace
?.projects
?.projectGraph
?.nodes
?.entries
?.map { entry -> entry.key to (entry.value.projectType) }
?.map { entry -> entry.key to (entry.value.data.projectType) }
?.associate { it }

val workspaceLayoutPair =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NxGraphBrowser(project: Project) : NxGraphBrowserBase(project) {

val errorsToShow = this@NxGraphBrowser.errors ?: nxWorkspace?.errors

val hasProjects = nxWorkspace?.workspace?.projects?.isNotEmpty() == true
val hasProjects = nxWorkspace?.projectGraph?.nodes?.isNotEmpty() == true
val needsNonExistentProject =
lastGraphCommand?.let {
val projectName =
Expand All @@ -75,7 +75,7 @@ class NxGraphBrowser(project: Project) : NxGraphBrowserBase(project) {
}

projectName != null &&
nxWorkspace?.workspace?.projects?.contains(projectName) != true
nxWorkspace?.projectGraph?.nodes?.contains(projectName) != true
}
?: false

Expand Down Expand Up @@ -118,7 +118,7 @@ class NxGraphBrowser(project: Project) : NxGraphBrowserBase(project) {
executeWhenLoaded {
if (browser.isDisposed) return@executeWhenLoaded
val nxWorkspace = NxlsService.getInstance(project).workspace()
if (nxWorkspace?.workspace?.projects?.contains(projectName) != true) {
if (nxWorkspace?.projectGraph?.nodes?.contains(projectName) != true) {
logger<NxGraphBrowser>().warn("Project $projectName not found in workspace")
setErrorsAndRefresh(arrayOf(NxError("Project $projectName not found in workspace")))
return@executeWhenLoaded
Expand All @@ -144,7 +144,7 @@ class NxGraphBrowser(project: Project) : NxGraphBrowserBase(project) {
executeWhenLoaded {
if (browser.isDisposed) return@executeWhenLoaded
val nxWorkspace = NxlsService.getInstance(project).workspace()
if (nxWorkspace?.workspace?.projects?.contains(projectName) != true) {
if (nxWorkspace?.projectGraph?.nodes?.contains(projectName) != true) {
logger<NxGraphBrowser>().warn("Project $projectName not found in workspace")
setErrorsAndRefresh(arrayOf(NxError("Project $projectName not found in workspace")))
return@executeWhenLoaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
browser.jbCefClient.addDownloadHandler(NxGraphDownloadHandler(), browser.cefBrowser)
browser.jbCefClient.addContextMenuHandler(
OpenDevToolsContextMenuHandler(),
browser.cefBrowser
browser.cefBrowser,
)
browser.setOpenLinksInExternalBrowser(true)

Expand Down Expand Up @@ -113,14 +113,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
protected fun loadGraphHtmlBase(): String {

val nxPackagePath = getNxPackagePath(project, project.nxBasePath)
val graphBasePath =
Paths.get(
nxPackagePath,
"src",
"core",
"graph",
)
.toString() + "/"
val graphBasePath = Paths.get(nxPackagePath, "src", "core", "graph").toString() + "/"

val graphIndexHtmlPath = Paths.get(graphBasePath, "index.html").toString()

Expand All @@ -134,13 +127,13 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
"<base\\b[^>]*>".toRegex(),
"""
<base href="${Matcher.quoteReplacement(graphBasePath)}">
"""
""",
)

htmlText =
htmlText.replace(
"<script(\\s[^>]*?)\\stype=\"module\"([^>]*?)>".toRegex(),
"<script$1$2>"
"<script$1$2>",
)

htmlText =
Expand Down Expand Up @@ -228,7 +221,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
</script>
</head>"""
)
),
)

setColors()
Expand All @@ -252,7 +245,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
Notifier.notifyAnything(
project,
"Couldn't find file at path $fullPath",
NotificationType.ERROR
NotificationType.ERROR,
)
return true
}
Expand All @@ -268,12 +261,13 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
TelemetryService.getInstance(project)
.featureUsed(
TelemetryEvent.MISC_SHOW_PROJECT_CONFIGURATION,
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION)
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION),
)

event.payload?.projectName?.also {
project.nxWorkspace()?.workspace?.projects?.get(it)?.apply {
val path = nxProjectConfigurationPath(project, root) ?: return@apply
project.nxWorkspace()?.projectGraph?.nodes?.get(it)?.apply {
val path =
nxProjectConfigurationPath(project, data.root) ?: return@apply
val file =
LocalFileSystem.getInstance().findFileByPath(path) ?: return@apply
ApplicationManager.getApplication().invokeLater {
Expand Down Expand Up @@ -357,7 +351,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
TelemetryService.getInstance(project)
.featureUsed(
TelemetryEvent.MISC_REFRESH_WORKSPACE,
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION)
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION),
)
NxRefreshWorkspaceService.getInstance(project).refreshWorkspace()
null
Expand Down Expand Up @@ -465,7 +459,7 @@ abstract class NxGraphBrowserBase(protected val project: Project) : Disposable {
@Serializable
data class NxGraphInteractionEvent(
val type: String,
val payload: NxGraphInteractionPayload? = null
val payload: NxGraphInteractionPayload? = null,
)

@Serializable
Expand All @@ -484,7 +478,7 @@ data class NxGraphRequest(
val type: String,
val id: String,
val payload: String? = null,
val error: String? = null
val error: String? = null,
)

@Service(Service.Level.PROJECT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ class OldNxGraphBrowser(
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION),
)

project.nxWorkspace()?.workspace?.projects?.get(msg)?.apply {
val path = nxProjectConfigurationPath(project, root) ?: return@apply
project.nxWorkspace()?.projectGraph?.nodes?.get(msg)?.apply {
val path = nxProjectConfigurationPath(project, data.root) ?: return@apply
val file =
LocalFileSystem.getInstance().findFileByPath(path) ?: return@apply
ApplicationManager.getApplication().invokeLater {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ShowNxProjectConfigurationAction : DumbAwareAction(AllIcons.Actions.EditSo
init {
registerCustomShortcutSet(
ActionManager.getInstance().getAction(IdeActions.ACTION_EDIT_SOURCE).shortcutSet,
null
null,
)
}

Expand Down Expand Up @@ -63,7 +63,7 @@ class ShowNxProjectConfigurationAction : DumbAwareAction(AllIcons.Actions.EditSo
else if (ActionPlaces.isPopupPlace(e.place))
TelemetryEventSource.EXPLORER_CONTEXT_MENU
else TelemetryEventSource.COMMAND
)
),
)

val path = e.dataContext.getData(CommonDataKeys.VIRTUAL_FILE)?.path
Expand All @@ -88,9 +88,10 @@ class ShowNxProjectConfigurationAction : DumbAwareAction(AllIcons.Actions.EditSo

NxlsService.getInstance(project)
.workspace()
?.workspace
?.projects
?.projectGraph
?.nodes
?.get(selectedNxProject)
?.data
}
?: return@launch

Expand All @@ -117,7 +118,7 @@ class ShowNxProjectConfigurationAction : DumbAwareAction(AllIcons.Actions.EditSo
findLineNumberForTargetAndConfiguration(
psiFile,
nxTarget,
nxTargetConfiguration
nxTargetConfiguration,
)
?: return@withContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.nx.console.models
data class NxProject(
val name: String,
val root: String,
val targets: Map<String, NxTarget>,
val targets: Map<String, NxTarget>?,
val sourceRoot: String?,
val projectType: String,
val metadata: NxProjectMetadata?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.serialization.descriptors.*

data class NxWorkspace(
val validWorkspaceJson: Boolean,
val workspace: NxWorkspaceConfiguration,
val projectGraph: NxProjectGraph,
val daemonEnabled: Boolean?,
val workspacePath: String,
val errors: Array<NxError>?,
Expand All @@ -23,7 +23,7 @@ data class NxWorkspace(
other as NxWorkspace

if (validWorkspaceJson != other.validWorkspaceJson) return false
if (workspace != other.workspace) return false
if (projectGraph != other.projectGraph) return false
if (daemonEnabled != other.daemonEnabled) return false
if (workspacePath != other.workspacePath) return false
if (errors != null) {
Expand All @@ -40,7 +40,7 @@ data class NxWorkspace(

override fun hashCode(): Int {
var result = validWorkspaceJson.hashCode()
result = 31 * result + workspace.hashCode()
result = 31 * result + projectGraph.hashCode()
result = 31 * result + (daemonEnabled?.hashCode() ?: 0)
result = 31 * result + workspacePath.hashCode()
result = 31 * result + (errors?.contentHashCode() ?: 0)
Expand All @@ -54,9 +54,9 @@ data class NxWorkspace(

data class WorkspaceLayout(val appsDir: String?, val libsDir: String?)

data class NxWorkspaceConfiguration(val projects: Map<String, NxProject>
// val sourceMaps: Map<String, Map<String, SourceInformation>>?,
) {}
data class NxProjectGraph(val nodes: Map<String, NxProjectGraphProjectNode>)

data class NxProjectGraphProjectNode(val name: String, val type: String, val data: NxProject)

@Serializable()
data class NxError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class NxToolWindowPanel(private val project: Project) : SimpleToolWindowPanel(tr
val cloudStatus = nxlsService.cloudStatus()

withContext(Dispatchers.EDT) {
val hasProjects = workspace?.workspace?.projects?.isNotEmpty() == true
val hasProjects = workspace?.projectGraph?.nodes?.isNotEmpty() == true
val hasNodeInterpreter =
try {
project.nodeInterpreter
Expand All @@ -118,7 +118,7 @@ class NxToolWindowPanel(private val project: Project) : SimpleToolWindowPanel(tr
it.second
}
}
} else if (workspace == null || workspace.workspace.projects.isEmpty()) {
} else if (workspace == null || workspace.projectGraph.nodes.isEmpty()) {
noProjectsComponent
} else {
projectTreeComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NxTreeStructure(val tree: NxProjectsTree, val project: Project) : SimpleTr
private suspend fun getTreeBuilder(nxWorkspace: NxWorkspace?): NxTreeBuilderBase {
val toolWindowStyle: ToolWindowStyles =
NxConsoleProjectSettingsProvider.getInstance(project).toolwindowStyle
val numProjects = nxWorkspace?.workspace?.projects?.size ?: 1
val numProjects = nxWorkspace?.projectGraph?.nodes?.size ?: 1
return if (
toolWindowStyle == ToolWindowStyles.LIST ||
(toolWindowStyle == ToolWindowStyles.AUTOMATIC && numProjects < 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class NxListTreeBuilder(private val nxWorkspace: NxWorkspace?) : NxTreeBuilderBa
if (nxWorkspace == null) {
return emptyArray()
}
return nxWorkspace.workspace.projects.values
.map { NxSimpleNode.Project(it.name, projectsSectionNode) }
.toTypedArray()
return nxWorkspace.projectGraph
?.nodes
?.values
?.map { NxSimpleNode.Project(it.name, projectsSectionNode) }
?.toTypedArray()
?: emptyArray()
}
}
Loading

0 comments on commit 438659d

Please sign in to comment.