|
| 1 | +package com.tomwyr.command |
| 2 | + |
| 3 | +import com.tomwyr.GodotNodeTreeInput |
| 4 | +import com.tomwyr.common.InvalidGodotProject |
| 5 | +import com.tomwyr.ffi.generateNodeTree |
| 6 | +import org.gradle.api.Project |
| 7 | +import java.io.File |
| 8 | +import java.nio.file.Paths |
| 9 | + |
| 10 | +class GenerateTreeCommand( |
| 11 | + val libPath: String, |
| 12 | + val projectPath: String, |
| 13 | + val outputPath: String, |
| 14 | + val packageName: String?, |
| 15 | +) { |
| 16 | + fun run() { |
| 17 | + val tree = generateNodeTree(libPath = libPath, projectPath = projectPath) |
| 18 | + val content = NodeTreeRenderer().render(packageName, tree) |
| 19 | + NodeTreeWriter().write(content, outputPath) |
| 20 | + } |
| 21 | + |
| 22 | + companion object Factory { |
| 23 | + fun from(project: Project, input: GodotNodeTreeInput): GenerateTreeCommand { |
| 24 | + val rootPath = project.projectDir.absolutePath |
| 25 | + val libPath = "NodeTreeGenerator.dylib" |
| 26 | + val projectPath = getProjectPath(rootPath = rootPath, relativePath = input.projectPath) |
| 27 | + val outputPath = getOutputPath(rootPath = rootPath, packageName = input.packageName) |
| 28 | + |
| 29 | + return GenerateTreeCommand( |
| 30 | + libPath = libPath, |
| 31 | + projectPath = projectPath, |
| 32 | + outputPath = outputPath, |
| 33 | + packageName = input.packageName |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + private fun getProjectPath(rootPath: String, relativePath: String?): String { |
| 38 | + val fileName = "project.godot" |
| 39 | + val path = Paths.get(rootPath, relativePath ?: "", fileName).toString() |
| 40 | + if (!File(path).exists()) throw InvalidGodotProject() |
| 41 | + return path |
| 42 | + } |
| 43 | + |
| 44 | + private fun getOutputPath(rootPath: String, packageName: String?): String { |
| 45 | + val buildPath = listOf("build", "generated", "godotNodeTree", "kotlin").toTypedArray() |
| 46 | + val packagePath = packageName?.split(".")?.toTypedArray() ?: emptyArray() |
| 47 | + val fileName = "GodotNodeTree.kt" |
| 48 | + return Paths.get(rootPath, *buildPath, *packagePath, fileName).toString() |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments