org.babyfish.jimmer.ksp.dto.DtoContext.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-ksp Show documentation
Show all versions of jimmer-ksp Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.ksp.dto
import com.google.devtools.ksp.symbol.KSFile
import org.babyfish.jimmer.dto.compiler.DtoFile
import org.babyfish.jimmer.dto.compiler.OsFile
import java.io.File
class DtoContext(anyFile: KSFile?, dtoDirs: Collection) {
val dtoFiles: List
init {
var file: File? = anyFile?.let { File(it.filePath) }
val dtoDirFileMap = mutableMapOf()
var projectDir: String? = null
while (file != null) {
val prjDir = collectDtoDirFileMap(file, dtoDirs, dtoDirFileMap)
if (projectDir === null) {
projectDir = prjDir
}
file = file.parentFile
}
val dtoFiles = mutableListOf()
for ((key, value) in dtoDirFileMap) {
val subFiles = value.listFiles()
if (subFiles != null) {
for (subFile in subFiles) {
collectDtoFiles(projectDir!!, key, subFile, mutableListOf(), dtoFiles)
}
}
}
this.dtoFiles = dtoFiles
}
private fun collectDtoDirFileMap(
baseFile: File,
dtoDirs: Collection,
dtoDirFileMap: MutableMap
) : String? {
var projectDir: String? = null
for (dtoDir in dtoDirs) {
var subFile: File? = baseFile
for (part in dtoDir.split("/").toTypedArray()) {
subFile = File(subFile, part)
if (!subFile.isDirectory) {
subFile = null
break
}
}
if (subFile != null) {
dtoDirFileMap[dtoDir] = subFile
projectDir = baseFile.name
}
}
return projectDir
}
private fun collectDtoFiles(projectDir: String, dtoDir: String, file: File, paths: MutableList, dtoFiles: MutableList) {
if (file.isFile() && file.getName().endsWith(".dto")) {
dtoFiles += DtoFile(OsFile.of(file), projectDir, dtoDir, paths, file.name)
} else {
val subFiles = file.listFiles()
if (subFiles != null) {
paths += file.getName()
for (subFile in subFiles) {
collectDtoFiles(projectDir, dtoDir, subFile, paths, dtoFiles)
}
paths.removeAt(paths.size - 1)
}
}
}
}