[Editor] Improve CLI handling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor
|
||||
|
||||
import com.bartlomiejpluta.base.editor.cli.model.CLIArgs
|
||||
import com.bartlomiejpluta.base.editor.cli.runner.CLIRunner
|
||||
import com.bartlomiejpluta.base.editor.code.build.exception.BuildException
|
||||
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
|
||||
import com.bartlomiejpluta.base.editor.command.service.DefaultUndoRedoService
|
||||
@@ -8,80 +9,67 @@ import com.bartlomiejpluta.base.editor.main.controller.MainController
|
||||
import com.bartlomiejpluta.base.editor.main.view.MainView
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import com.xenomachina.argparser.ArgParser
|
||||
import com.xenomachina.argparser.mainBody
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.CommandLineRunner
|
||||
import org.springframework.boot.SpringApplication
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.context.ConfigurableApplicationContext
|
||||
import tornadofx.App
|
||||
import tornadofx.DIContainer
|
||||
import tornadofx.FX
|
||||
import tornadofx.launch
|
||||
import tornadofx.*
|
||||
import java.io.File
|
||||
import javax.annotation.PostConstruct
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@SpringBootApplication
|
||||
open class EditorApp : App(MainView::class) {
|
||||
private lateinit var context: ConfigurableApplicationContext
|
||||
private lateinit var context: ConfigurableApplicationContext
|
||||
|
||||
@Autowired
|
||||
private lateinit var mainController: MainController
|
||||
@Autowired
|
||||
private lateinit var mainController: MainController
|
||||
|
||||
override fun init() {
|
||||
this.context = SpringApplication.run(this.javaClass)
|
||||
context.autowireCapableBeanFactory.autowireBean(this)
|
||||
@Autowired
|
||||
private lateinit var cliRunner: CLIRunner
|
||||
|
||||
FX.dicontainer = object : DIContainer {
|
||||
override fun <T : Any> getInstance(type: KClass<T>): T = context.getBean(type.java)
|
||||
override fun <T : Any> getInstance(type: KClass<T>, name: String): T = context.getBean(name, type.java)
|
||||
}
|
||||
}
|
||||
override fun init() {
|
||||
this.context = SpringApplication.run(this.javaClass)
|
||||
context.autowireCapableBeanFactory.autowireBean(this)
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
context.close()
|
||||
mainController.clearResources()
|
||||
}
|
||||
FX.dicontainer = object : DIContainer {
|
||||
override fun <T : Any> getInstance(type: KClass<T>): T = context.getBean(type.java)
|
||||
override fun <T : Any> getInstance(type: KClass<T>, name: String): T = context.getBean(name, type.java)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBeforeShow(view: UIComponent) {
|
||||
super.onBeforeShow(view)
|
||||
cliRunner.run(*parameters.raw.toTypedArray())
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
context.close()
|
||||
mainController.clearResources()
|
||||
}
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
open class CLIApp(
|
||||
@Autowired private val context: ProjectContext,
|
||||
@Autowired private val pipelineService: BuildPipelineService
|
||||
@Autowired private val cliRunner: CLIRunner
|
||||
) : CommandLineRunner {
|
||||
override fun run(vararg args: String) {
|
||||
ArgParser(args).parseInto(::CLIArgs).run {
|
||||
project?.let { context.open(it) }
|
||||
|
||||
if (build) {
|
||||
pipelineService.initStreams(System.out, System.err)
|
||||
val startTime = System.currentTimeMillis()
|
||||
|
||||
try {
|
||||
context.project?.let { project ->
|
||||
output?.let { project.changeBuildDirectory(it) }
|
||||
pipelineService.runPipeline(project)
|
||||
}
|
||||
|
||||
println("Build completed")
|
||||
} catch (e: BuildException) {
|
||||
System.err.println("Build failed")
|
||||
} finally {
|
||||
val buildingTime = (System.currentTimeMillis() - startTime) / 1000.0
|
||||
println("Finished in [${buildingTime}s]")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun run(vararg args: String) {
|
||||
cliRunner.run(*args)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
ArgParser(args).parseInto(::CLIArgs).run {
|
||||
if (headless) {
|
||||
mainBody {
|
||||
ArgParser(args).parseInto(::CLIArgs).run {
|
||||
if (headless) {
|
||||
SpringApplication.run(CLIApp::class.java, *args)
|
||||
} else {
|
||||
} else {
|
||||
launch<EditorApp>(args)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,9 @@ import com.xenomachina.argparser.default
|
||||
import java.io.File
|
||||
|
||||
class CLIArgs(parser: ArgParser) {
|
||||
val headless by parser.flagging("-H", "--headless", help = "run editor in headless mode (without GUI)").default(false)
|
||||
val project by parser.storing("-p", "--project", help = "project file") { File(this) }.default(null)
|
||||
val build by parser.flagging("-b", "--build", help = "build project").default(false)
|
||||
val output by parser.storing("-o", "--output", help = "define build output folder") { File(this) }.default(null)
|
||||
val headless by parser.flagging("-H", "--headless", help = "run editor in headless mode (without GUI)")
|
||||
.default(false)
|
||||
val project by parser.storing("-p", "--project", help = "project file") { File(this) }.default(null)
|
||||
val build by parser.flagging("-b", "--build", help = "build project").default(false)
|
||||
val output by parser.storing("-o", "--output", help = "override build output path") { File(this) }.default(null)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.bartlomiejpluta.base.editor.cli.runner
|
||||
|
||||
import com.bartlomiejpluta.base.editor.cli.model.CLIArgs
|
||||
import com.bartlomiejpluta.base.editor.code.build.exception.BuildException
|
||||
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
import com.xenomachina.argparser.ArgParser
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
import java.io.File
|
||||
|
||||
@Component
|
||||
class CLIRunner(
|
||||
@Autowired private val context: ProjectContext,
|
||||
@Autowired private val pipelineService: BuildPipelineService
|
||||
) {
|
||||
|
||||
fun run(vararg args: String) = ArgParser(args).parseInto(::CLIArgs).run {
|
||||
handleOpenProject(project)
|
||||
handleChangeOutput(output)
|
||||
handleRunBuild(build)
|
||||
}
|
||||
|
||||
private fun handleOpenProject(project: File?) = project?.let {
|
||||
context.open(it)
|
||||
}
|
||||
|
||||
private fun handleChangeOutput(output: File?) = output?.let {
|
||||
context.project?.changeBuildDirectory(it)
|
||||
}
|
||||
|
||||
private fun handleRunBuild(build: Boolean) {
|
||||
if (!build) {
|
||||
return
|
||||
}
|
||||
|
||||
pipelineService.initStreams(System.out, System.err)
|
||||
val startTime = System.currentTimeMillis()
|
||||
|
||||
try {
|
||||
context.project?.let { project -> pipelineService.runPipeline(project) }
|
||||
println("Build completed")
|
||||
} catch (e: BuildException) {
|
||||
System.err.println("Build failed")
|
||||
} finally {
|
||||
val buildingTime = (System.currentTimeMillis() - startTime) / 1000.0
|
||||
println("Finished in [${buildingTime}s]")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,19 @@ import java.io.PrintStream
|
||||
import java.io.PrintWriter
|
||||
|
||||
interface JarPackager {
|
||||
fun pack(sourceDirectory: File, targetJar: File, root: String = ".", stdout: PrintStream = System.out, stderr: PrintStream = System.err)
|
||||
fun copy(file: File, targetJar: File, root: String = ".", stdout: PrintStream = System.out, stderr: PrintStream = System.err)
|
||||
fun pack(
|
||||
sourceDirectory: File,
|
||||
targetJar: File,
|
||||
root: String = ".",
|
||||
stdout: PrintStream = System.out,
|
||||
stderr: PrintStream = System.err
|
||||
)
|
||||
|
||||
fun copy(
|
||||
file: File,
|
||||
targetJar: File,
|
||||
root: String = ".",
|
||||
stdout: PrintStream = System.out,
|
||||
stderr: PrintStream = System.err
|
||||
)
|
||||
}
|
||||
@@ -19,92 +19,92 @@ import java.io.PrintStream
|
||||
@Component
|
||||
open class BuildPipelineService {
|
||||
|
||||
@Autowired
|
||||
private lateinit var dependenciesProvider: DependenciesProvider
|
||||
@Autowired
|
||||
private lateinit var dependenciesProvider: DependenciesProvider
|
||||
|
||||
@Autowired
|
||||
private lateinit var generators: List<CodeGenerator>
|
||||
@Autowired
|
||||
private lateinit var generators: List<CodeGenerator>
|
||||
|
||||
@Autowired
|
||||
private lateinit var compiler: Compiler
|
||||
@Autowired
|
||||
private lateinit var compiler: Compiler
|
||||
|
||||
@Autowired
|
||||
private lateinit var packager: JarPackager
|
||||
@Autowired
|
||||
private lateinit var packager: JarPackager
|
||||
|
||||
@Autowired
|
||||
private lateinit var engineProvider: GameEngineProvider
|
||||
@Autowired
|
||||
private lateinit var engineProvider: GameEngineProvider
|
||||
|
||||
@Autowired
|
||||
private lateinit var assetSerializer: AssetSerializer
|
||||
@Autowired
|
||||
private lateinit var assetSerializer: AssetSerializer
|
||||
|
||||
@Autowired
|
||||
private lateinit var projectAssembler: ProjectAssembler
|
||||
@Autowired
|
||||
private lateinit var projectAssembler: ProjectAssembler
|
||||
|
||||
@Autowired
|
||||
private lateinit var databaseAssembler: DatabaseAssembler
|
||||
@Autowired
|
||||
private lateinit var databaseAssembler: DatabaseAssembler
|
||||
|
||||
@Autowired
|
||||
private lateinit var projectContext: ProjectContext
|
||||
@Autowired
|
||||
private lateinit var projectContext: ProjectContext
|
||||
|
||||
private lateinit var stdout: OutputStream
|
||||
private lateinit var stderr: OutputStream
|
||||
private var out: PrintStream = System.out
|
||||
private var err: PrintStream = System.err
|
||||
private lateinit var stdout: OutputStream
|
||||
private lateinit var stderr: OutputStream
|
||||
private var out: PrintStream = System.out
|
||||
private var err: PrintStream = System.err
|
||||
|
||||
fun initStreams(stdout: OutputStream, stderr: OutputStream) {
|
||||
this.stdout = stdout
|
||||
this.stderr = stderr
|
||||
this.out = PrintStream(stdout)
|
||||
this.err = PrintStream(stderr)
|
||||
}
|
||||
|
||||
fun runPipeline(project: Project) {
|
||||
prepareBuildDirectory(project)
|
||||
fun initStreams(stdout: OutputStream, stderr: OutputStream) {
|
||||
this.stdout = stdout
|
||||
this.stderr = stderr
|
||||
this.out = PrintStream(stdout)
|
||||
this.err = PrintStream(stderr)
|
||||
}
|
||||
|
||||
val outputFile = project.buildOutputJarFile
|
||||
fun runPipeline(project: Project) {
|
||||
prepareBuildDirectory(project)
|
||||
|
||||
out.println("Providing compile-time dependencies...")
|
||||
val dependencies = dependenciesProvider.provideDependenciesTo(project.buildDependenciesDirectory)
|
||||
val outputFile = project.buildOutputJarFile
|
||||
|
||||
out.println("Generating sources...")
|
||||
generators.forEach(CodeGenerator::generate)
|
||||
out.println("Providing compile-time dependencies...")
|
||||
val dependencies = dependenciesProvider.provideDependenciesTo(project.buildDependenciesDirectory)
|
||||
|
||||
out.println("Compiling sources...")
|
||||
compiler.compile(
|
||||
arrayOf(project.codeFSNode, FileSystemNode(project.buildGeneratedCodeDirectory)),
|
||||
project.buildClassesDirectory,
|
||||
dependencies.toTypedArray(),
|
||||
out,
|
||||
err
|
||||
)
|
||||
out.println("Generating sources...")
|
||||
generators.forEach(CodeGenerator::generate)
|
||||
|
||||
out.println("Assembling game engine...")
|
||||
engineProvider.provideBaseGameEngine(outputFile, out, err)
|
||||
out.println("Compiling sources...")
|
||||
compiler.compile(
|
||||
arrayOf(project.codeFSNode, FileSystemNode(project.buildGeneratedCodeDirectory)),
|
||||
project.buildClassesDirectory,
|
||||
dependencies.toTypedArray(),
|
||||
out,
|
||||
err
|
||||
)
|
||||
|
||||
out.println("Linking compilation units...")
|
||||
packager.pack(project.buildClassesDirectory, outputFile, "BOOT-INF/classes")
|
||||
out.println("Assembling game engine...")
|
||||
engineProvider.provideBaseGameEngine(outputFile, out, err)
|
||||
|
||||
out.println("Serializing project assets...")
|
||||
assetSerializer.serializeAssets(project)
|
||||
out.println("Linking compilation units...")
|
||||
packager.pack(project.buildClassesDirectory, outputFile, "BOOT-INF/classes")
|
||||
|
||||
out.println("Assembling project assets...")
|
||||
projectAssembler.assembly(project, outputFile, out, err)
|
||||
out.println("Serializing project assets...")
|
||||
assetSerializer.serializeAssets(project)
|
||||
|
||||
out.println("Assembling database...")
|
||||
databaseAssembler.assembly(project, outputFile, out, err)
|
||||
}
|
||||
out.println("Assembling project assets...")
|
||||
projectAssembler.assembly(project, outputFile, out, err)
|
||||
|
||||
private fun prepareBuildDirectory(project: Project) {
|
||||
project.buildDirectory.deleteRecursively()
|
||||
out.println("Assembling database...")
|
||||
databaseAssembler.assembly(project, outputFile, out, err)
|
||||
}
|
||||
|
||||
project.buildClassesDirectory.mkdirs()
|
||||
project.buildOutDirectory.mkdirs()
|
||||
project.buildDependenciesDirectory.mkdirs()
|
||||
project.buildGeneratedCodeDirectory.mkdirs()
|
||||
}
|
||||
private fun prepareBuildDirectory(project: Project) {
|
||||
project.buildDirectory.deleteRecursively()
|
||||
|
||||
fun clean() {
|
||||
projectContext.project?.apply { buildDirectory.deleteRecursively() }
|
||||
out.println("Cleaning done")
|
||||
}
|
||||
project.buildClassesDirectory.mkdirs()
|
||||
project.buildOutDirectory.mkdirs()
|
||||
project.buildDependenciesDirectory.mkdirs()
|
||||
project.buildGeneratedCodeDirectory.mkdirs()
|
||||
}
|
||||
|
||||
fun clean() {
|
||||
projectContext.project?.apply { buildDirectory.deleteRecursively() }
|
||||
out.println("Cleaning done")
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ class DefaultBuildService : BuildService {
|
||||
|
||||
@Autowired
|
||||
private lateinit var pipelineService: BuildPipelineService
|
||||
|
||||
@Autowired
|
||||
private lateinit var projectContext: ProjectContext
|
||||
|
||||
@@ -38,7 +39,12 @@ class DefaultBuildService : BuildService {
|
||||
private var err: PrintStream = System.err
|
||||
|
||||
override fun initStreams(stdout: OutputStream, stderr: OutputStream) {
|
||||
pipelineService.initStreams(stdout, stderr)
|
||||
pipelineService.initStreams(stdout, stderr)
|
||||
this.stdout = stdout
|
||||
this.stderr = stderr
|
||||
this.out = PrintStream(stdout)
|
||||
this.err = PrintStream(stderr)
|
||||
|
||||
}
|
||||
|
||||
override val isRunningProperty = false.toProperty()
|
||||
|
||||
@@ -26,7 +26,7 @@ class DefaultProjectAssembler : ProjectAssembler {
|
||||
private fun tryToAssembly(project: Project, targetJar: File, stdout: PrintStream, stderr: PrintStream) {
|
||||
stdout.println("[$TAG] Assembling project")
|
||||
packager.copy(project.binaryProjectFile, targetJar, "BOOT-INF/classes/project", stdout, stderr)
|
||||
|
||||
|
||||
stdout.println("[$TAG] Assembling maps")
|
||||
packager.pack(project.buildAssetsMapsDir, targetJar, "BOOT-INF/classes/project/maps", stdout, stderr)
|
||||
|
||||
|
||||
@@ -47,7 +47,8 @@ class Project {
|
||||
val widgets = observableListOf<WidgetAsset>()
|
||||
val sounds = observableListOf<SoundAsset>()
|
||||
|
||||
val assetLists = listOf(maps, tileSets, autoTiles, images, characterSets, animations, iconSets, fonts, widgets, sounds)
|
||||
val assetLists =
|
||||
listOf(maps, tileSets, autoTiles, images, characterSets, animations, iconSets, fonts, widgets, sounds)
|
||||
|
||||
val mapsDirectoryProperty = SimpleObjectProperty<File>()
|
||||
var mapsDirectory by mapsDirectoryProperty
|
||||
@@ -131,7 +132,8 @@ class Project {
|
||||
var buildAssetsDir by buildAssetsDirProperty
|
||||
private set
|
||||
|
||||
val binaryProjectFileProperty = createObjectBinding({ File(buildAssetsDir, BINARY_PROJECT_FILE) }, buildAssetsDirProperty)
|
||||
val binaryProjectFileProperty =
|
||||
createObjectBinding({ File(buildAssetsDir, BINARY_PROJECT_FILE) }, buildAssetsDirProperty)
|
||||
val binaryProjectFile by binaryProjectFileProperty
|
||||
|
||||
val buildAssetsMapsDirProperty = createObjectBinding({ File(buildAssetsDir, MAPS_DIR) }, buildAssetsDirProperty)
|
||||
|
||||
Reference in New Issue
Block a user