Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat(sca): add first version node tree
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 21, 2022
1 parent efd2a57 commit 93b509e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
Expand Up @@ -7,13 +7,74 @@ import java.io.File

class JavaFinder {
fun getDeclTree(path: String): List<DepDecl> {
return File(path).walk()
return File(path).walk(FileWalkDirection.BOTTOM_UP)
.filter {
it.isFile && it.name == "build.gradle" || it.name == "build.gradle.kts"
}
.flatMap {
val file = DeclFileTree(name = it.name, path = it.absolutePath, content = it.readText())
val file = DeclFileTree(filename = it.name, path = it.absolutePath, content = it.readText())
GradleParser().lookupSource(file)
}.toList()
}

fun buildDeclTree(path: String): DeclFileTree? {
val dirPaths = File(path).walk(FileWalkDirection.BOTTOM_UP)
.filter {
it.isFile && it.name == "build.gradle" || it.name == "build.gradle.kts"
}.map {
it.absolutePath
}
.sortedBy {
it.split(File.separatorChar).size
}
.toMutableList()

val nodesFromPathList = nodesFromPathList(dirPaths)
return nodesFromPathList
}

private fun nodesFromPathList(dirPaths: MutableList<String>): DeclFileTree? {
var ret: DeclFileTree? = null

val nodeMap = mutableMapOf<String, DeclFileTree>()

var isFirst = true
for (dir in dirPaths) {
val newNode = DeclFileTree(nodeName(dir), dir, File(dir).readText())
if (isFirst) {
isFirst = false
ret = newNode
}

nodeMap[dir] = newNode
}

nodeMap.forEach {
val fileName = it.value.filename
val parent = parentPath(it.key) + fileName

try {
nodeMap[parent]!!.childrens += it.value
} catch (e: NullPointerException) {
// println("Parent not found")
}
}

return ret
}

private fun nodeName(path: String): String {
return path.split(File.separator).last()
}

private fun parentPath(path: String): String {
val split = path.split(File.separator)
var ret = ""
for (i in 0..split.size - 3) {
ret += split[i] + File.separator
}

return ret

}
}
Expand Up @@ -4,10 +4,10 @@ package org.archguard.analyser.sca.model
* file for save content ?
*/
class DeclFileTree(
val name: String,
val filename: String,
val path: String,
val content: String,
val childrens: List<DeclFileTree> = listOf()
var childrens: List<DeclFileTree> = listOf()
)

class DepDecl(
Expand Down
Expand Up @@ -6,9 +6,12 @@ import java.io.File
internal class JavaFinderTest {
@Test
internal fun name() {
val parent = File(".")
val declTree = JavaFinder().getDeclTree(parent.absolutePath)

val declTree = JavaFinder().getDeclTree(File(".").absolutePath)

assert(declTree[0].dependencies.size >= 2)

val declTree2 = JavaFinder().buildDeclTree(File("..").canonicalPath)
println(declTree2)
}
}

0 comments on commit 93b509e

Please sign in to comment.