From 54828a6535a308f7b8f60298b95831a2e9b99f52 Mon Sep 17 00:00:00 2001 From: "liubar.pa" Date: Thu, 4 May 2023 08:09:06 +0300 Subject: [PATCH] SNARK-71: better implementation --- .../src/main/kotlin/DocumentBuilder.kt | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt index 1b26d45..d421599 100644 --- a/snark-document-builder/src/main/kotlin/DocumentBuilder.kt +++ b/snark-document-builder/src/main/kotlin/DocumentBuilder.kt @@ -15,35 +15,25 @@ public suspend fun buildDocument(documentDirectory: Directory) { public suspend fun buildDependencyGraph(root: Directory): DependencyGraph { val nodes = HashMap() - // buildNodes(root, nodes) - - val rootDcoument = root.get(DEFAULT_DOCUMENT_ROOT) - - nodes.put(".", buildDependencyGraphNode(rootDcoument.readAll())) - - val filesToParse = getDependencies(nodes.getValue(".")) - - while (!filesToParse.isEmpty()) - { - val currentFile = filesToParse.remove() - val currentDocument = Directory(currentFile).get(DEFAULT_DOCUMENT_ROOT) - - nodes.put(currentFile, buildDependencyGraphNode(currentDocument.readAll())) - - val currentDependencies = getDependencies(nodes.getValue(currentFile)) - - for (fileName in currentDependencies) { - if (!nodes.containsKey(fileName) && !filesToParse.contains(fileName)) - filesToParse.add(fileName) - } - } + buildNodes(root, nodes) return DependencyGraph(nodes) } -// private suspend fun buildNodes(folder: Directory, nodes: HashMap) { -// assert(!nodes.containsKey(folder.getPath())) -// } +private suspend fun buildNodes(folder: Directory, nodes: HashMap) { + assert(!nodes.containsKey(folder.getPath())) + + val path = folder.getPath() + val rootDcoument = folder.get(DEFAULT_DOCUMENT_ROOT) + nodes.put(path, buildDependencyGraphNode(rootDcoument.readAll())) + + val dependencies = getDependencies(nodes.getValue(path)) + + for (dependency in dependencies) { + if (!nodes.containsKey(dependency)) + buildNodes(folder.getSubdir(dependency), nodes) + } +} public suspend fun getDependencies(node: DependencyGraphNode): Set { TODO()