[Editor] Enable compiling to build/classes directory
It has been done by moving the *.class files from the source directory to project.buildClassesDirectory right after the compilation is done
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.bartlomiejpluta.base.editor.code.compiler
|
||||
|
||||
import com.bartlomiejpluta.base.editor.code.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
@@ -15,8 +14,8 @@ class DefaultCompilingService : CompilingService {
|
||||
private lateinit var projectContext: ProjectContext
|
||||
|
||||
override fun compile() {
|
||||
projectContext.project?.codeDirectory
|
||||
?.let { FileSystemNode(it) }
|
||||
?.let { compiler.compile(it) }
|
||||
projectContext.project?.let {
|
||||
compiler.compile(it.codeFSNode, it.buildClassesDirectory)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,15 @@ import org.codehaus.janino.CompilerFactory
|
||||
import org.springframework.stereotype.Component
|
||||
import tornadofx.FX
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.stream.Collectors.toList
|
||||
|
||||
@Component
|
||||
class JaninoCompiler : ScriptCompiler {
|
||||
private val compilerFactory = CompilerFactory()
|
||||
|
||||
override fun compile(sourceDirectory: FileSystemNode) {
|
||||
override fun compile(sourceDirectory: FileSystemNode, targetDirectory: File) {
|
||||
val files = sourceDirectory.allChildren.map(FileSystemNode::file).filter(File::isFile)
|
||||
val compiler = compilerFactory.newCompiler()
|
||||
|
||||
@@ -22,6 +25,7 @@ class JaninoCompiler : ScriptCompiler {
|
||||
try {
|
||||
compiler.compile(files.toTypedArray())
|
||||
FX.eventbus.fire(UpdateCompilationLogEvent(UpdateCompilationLogEvent.Severity.INFO, "Compilation success"))
|
||||
moveClassFilesToTargetDirectory(sourceDirectory.file, targetDirectory)
|
||||
} catch (e: CompileException) {
|
||||
|
||||
// Because the Janino compiler assemblies the message with the location
|
||||
@@ -32,4 +36,24 @@ class JaninoCompiler : ScriptCompiler {
|
||||
FX.eventbus.fire(UpdateCompilationLogEvent(UpdateCompilationLogEvent.Severity.ERROR, message, e.location))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun moveClassFilesToTargetDirectory(sourceDirectory: File, targetDirectory: File) {
|
||||
if (targetDirectory.exists() && !targetDirectory.isDirectory) {
|
||||
throw IllegalStateException("Target directory is actually a file")
|
||||
}
|
||||
|
||||
targetDirectory.mkdirs()
|
||||
|
||||
val files = Files.walk(sourceDirectory.toPath())
|
||||
.filter(Files::isRegularFile)
|
||||
.map(Path::toFile)
|
||||
.collect(toList())
|
||||
|
||||
files.filter { it.extension == "class" }.forEach {
|
||||
val relative = it.toRelativeString(sourceDirectory)
|
||||
val targetFile = File(targetDirectory, relative)
|
||||
it.copyTo(targetFile, overwrite = true)
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.bartlomiejpluta.base.editor.code.compiler
|
||||
|
||||
import com.bartlomiejpluta.base.editor.code.model.FileSystemNode
|
||||
import java.io.File
|
||||
|
||||
interface ScriptCompiler {
|
||||
fun compile(sourceDirectory: FileSystemNode)
|
||||
fun compile(sourceDirectory: FileSystemNode, targetDirectory: File)
|
||||
}
|
||||
@@ -43,6 +43,10 @@ class Project {
|
||||
val codeFSNodeProperty = Bindings.createObjectBinding({ FileSystemNode(codeDirectory) }, codeDirectoryProperty)
|
||||
val codeFSNode by codeFSNodeProperty
|
||||
|
||||
val buildClassesDirectoryProperty = SimpleObjectProperty<File>()
|
||||
var buildClassesDirectory by buildClassesDirectoryProperty
|
||||
private set
|
||||
|
||||
init {
|
||||
sourceDirectoryProperty.addListener { _, _, dir ->
|
||||
dir?.let {
|
||||
@@ -50,6 +54,7 @@ class Project {
|
||||
tileSetsDirectory = File(it, TILESETS_DIR)
|
||||
imagesDirectory = File(it, IMAGES_DIR)
|
||||
codeDirectory = File(it, CODE_DIR)
|
||||
buildClassesDirectory = File(it, BUILD_CLASSES_DIR)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,5 +72,6 @@ class Project {
|
||||
const val TILESETS_DIR = "tilesets"
|
||||
const val IMAGES_DIR = "images"
|
||||
const val CODE_DIR = "code"
|
||||
const val BUILD_CLASSES_DIR = "build/classes"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user