[Editor] Create :api module which provides a common classes for both :game module and user project's Java code developed with :editor
This commit is contained in:
@@ -47,15 +47,21 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
}
|
||||
|
||||
task copyGameEngine(type: Copy) {
|
||||
task provideGameEngine(type: Copy) {
|
||||
dependsOn(":game:build")
|
||||
|
||||
from project(':game').file('build/libs/game.jar')
|
||||
into file("build/resources/main/engine")
|
||||
}
|
||||
|
||||
task provideApi(type: Copy) {
|
||||
from project(':api').file('src/main/java')
|
||||
into file('build/resources/main/api')
|
||||
}
|
||||
|
||||
processResources {
|
||||
dependsOn(copyGameEngine)
|
||||
dependsOn(provideGameEngine)
|
||||
dependsOn(provideApi)
|
||||
}
|
||||
|
||||
build {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.bartlomiejpluta.base.editor.code.build.compiler
|
||||
|
||||
import com.bartlomiejpluta.base.editor.code.build.model.ClasspathResource
|
||||
import com.bartlomiejpluta.base.editor.code.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.event.UpdateCompilationLogEvent
|
||||
import org.codehaus.commons.compiler.CompileException
|
||||
import org.codehaus.commons.compiler.util.resource.FileResource
|
||||
import org.codehaus.commons.compiler.util.resource.Resource
|
||||
import org.codehaus.janino.CompilerFactory
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Component
|
||||
import tornadofx.FX
|
||||
import java.io.File
|
||||
@@ -15,15 +19,31 @@ import java.util.stream.Collectors.toList
|
||||
class JaninoCompiler : ScriptCompiler {
|
||||
private val compilerFactory = CompilerFactory()
|
||||
|
||||
@Value("classpath:api/**/*.java")
|
||||
private lateinit var apiFiles: Array<org.springframework.core.io.Resource>
|
||||
|
||||
override fun compile(sourceDirectory: FileSystemNode, targetDirectory: File) {
|
||||
val files = sourceDirectory.allChildren.map(FileSystemNode::file).filter(File::isFile)
|
||||
val sources = sourceDirectory
|
||||
.allChildren
|
||||
.map(FileSystemNode::file)
|
||||
.filter(File::isFile)
|
||||
.map(::FileResource)
|
||||
.toTypedArray<Resource>()
|
||||
|
||||
val api = apiFiles
|
||||
.map(::ClasspathResource)
|
||||
.toTypedArray()
|
||||
|
||||
val resources = sources + api
|
||||
|
||||
val compiler = compilerFactory.newCompiler()
|
||||
|
||||
|
||||
// FIXME:
|
||||
// For some reason the compiler's error handler does not want to catch
|
||||
// syntax errors. The only way to catch it is just catching CompileExceptions
|
||||
try {
|
||||
compiler.compile(files.toTypedArray())
|
||||
compiler.compile(resources)
|
||||
FX.eventbus.fire(UpdateCompilationLogEvent(UpdateCompilationLogEvent.Severity.INFO, "Compilation success"))
|
||||
moveClassFilesToTargetDirectory(sourceDirectory.file, targetDirectory)
|
||||
} catch (e: CompileException) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.bartlomiejpluta.base.editor.code.build.model
|
||||
|
||||
import org.codehaus.commons.compiler.util.resource.Resource
|
||||
import java.io.InputStream
|
||||
|
||||
class ClasspathResource(private val resource: org.springframework.core.io.Resource) : Resource {
|
||||
|
||||
override fun open(): InputStream = resource.inputStream
|
||||
|
||||
override fun getFileName(): String = resource.filename ?: ""
|
||||
|
||||
override fun lastModified(): Long = resource.lastModified()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.bartlomiejpluta.base.editor.code.build.model
|
||||
|
||||
import org.codehaus.commons.compiler.util.resource.Resource
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
|
||||
class FileResource(private val file: File) : Resource {
|
||||
|
||||
override fun open(): InputStream = file.inputStream()
|
||||
|
||||
override fun getFileName(): String = file.name
|
||||
|
||||
override fun lastModified(): Long = file.lastModified()
|
||||
}
|
||||
Reference in New Issue
Block a user