[Editor] Improve CLI handling
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.editor
|
package com.bartlomiejpluta.base.editor
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.cli.model.CLIArgs
|
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.exception.BuildException
|
||||||
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
|
import com.bartlomiejpluta.base.editor.code.build.pipeline.BuildPipelineService
|
||||||
import com.bartlomiejpluta.base.editor.command.service.DefaultUndoRedoService
|
import com.bartlomiejpluta.base.editor.command.service.DefaultUndoRedoService
|
||||||
@@ -8,17 +9,16 @@ import com.bartlomiejpluta.base.editor.main.controller.MainController
|
|||||||
import com.bartlomiejpluta.base.editor.main.view.MainView
|
import com.bartlomiejpluta.base.editor.main.view.MainView
|
||||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||||
import com.xenomachina.argparser.ArgParser
|
import com.xenomachina.argparser.ArgParser
|
||||||
|
import com.xenomachina.argparser.mainBody
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.CommandLineRunner
|
import org.springframework.boot.CommandLineRunner
|
||||||
import org.springframework.boot.SpringApplication
|
import org.springframework.boot.SpringApplication
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
import org.springframework.context.ConfigurableApplicationContext
|
import org.springframework.context.ConfigurableApplicationContext
|
||||||
import tornadofx.App
|
import tornadofx.*
|
||||||
import tornadofx.DIContainer
|
|
||||||
import tornadofx.FX
|
|
||||||
import tornadofx.launch
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import javax.annotation.PostConstruct
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@@ -28,6 +28,9 @@ open class EditorApp : App(MainView::class) {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var mainController: MainController
|
private lateinit var mainController: MainController
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private lateinit var cliRunner: CLIRunner
|
||||||
|
|
||||||
override fun init() {
|
override fun init() {
|
||||||
this.context = SpringApplication.run(this.javaClass)
|
this.context = SpringApplication.run(this.javaClass)
|
||||||
context.autowireCapableBeanFactory.autowireBean(this)
|
context.autowireCapableBeanFactory.autowireBean(this)
|
||||||
@@ -38,6 +41,11 @@ open class EditorApp : App(MainView::class) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onBeforeShow(view: UIComponent) {
|
||||||
|
super.onBeforeShow(view)
|
||||||
|
cliRunner.run(*parameters.raw.toTypedArray())
|
||||||
|
}
|
||||||
|
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
super.stop()
|
super.stop()
|
||||||
context.close()
|
context.close()
|
||||||
@@ -47,36 +55,15 @@ open class EditorApp : App(MainView::class) {
|
|||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
open class CLIApp(
|
open class CLIApp(
|
||||||
@Autowired private val context: ProjectContext,
|
@Autowired private val cliRunner: CLIRunner
|
||||||
@Autowired private val pipelineService: BuildPipelineService
|
|
||||||
) : CommandLineRunner {
|
) : CommandLineRunner {
|
||||||
override fun run(vararg args: String) {
|
override fun run(vararg args: String) {
|
||||||
ArgParser(args).parseInto(::CLIArgs).run {
|
cliRunner.run(*args)
|
||||||
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]")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
|
mainBody {
|
||||||
ArgParser(args).parseInto(::CLIArgs).run {
|
ArgParser(args).parseInto(::CLIArgs).run {
|
||||||
if (headless) {
|
if (headless) {
|
||||||
SpringApplication.run(CLIApp::class.java, *args)
|
SpringApplication.run(CLIApp::class.java, *args)
|
||||||
@@ -84,4 +71,5 @@ fun main(args: Array<String>) {
|
|||||||
launch<EditorApp>(args)
|
launch<EditorApp>(args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,8 @@ import com.xenomachina.argparser.default
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class CLIArgs(parser: ArgParser) {
|
class CLIArgs(parser: ArgParser) {
|
||||||
val headless by parser.flagging("-H", "--headless", help = "run editor in headless mode (without GUI)").default(false)
|
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 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 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 output by parser.storing("-o", "--output", help = "define build output folder") { 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
|
import java.io.PrintWriter
|
||||||
|
|
||||||
interface JarPackager {
|
interface JarPackager {
|
||||||
fun pack(sourceDirectory: File, targetJar: File, root: String = ".", stdout: PrintStream = System.out, stderr: PrintStream = System.err)
|
fun pack(
|
||||||
fun copy(file: File, targetJar: File, root: String = ".", stdout: PrintStream = System.out, stderr: PrintStream = System.err)
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,7 @@ class DefaultBuildService : BuildService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var pipelineService: BuildPipelineService
|
private lateinit var pipelineService: BuildPipelineService
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var projectContext: ProjectContext
|
private lateinit var projectContext: ProjectContext
|
||||||
|
|
||||||
@@ -39,6 +40,11 @@ class DefaultBuildService : BuildService {
|
|||||||
|
|
||||||
override fun initStreams(stdout: OutputStream, stderr: OutputStream) {
|
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()
|
override val isRunningProperty = false.toProperty()
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ class Project {
|
|||||||
val widgets = observableListOf<WidgetAsset>()
|
val widgets = observableListOf<WidgetAsset>()
|
||||||
val sounds = observableListOf<SoundAsset>()
|
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>()
|
val mapsDirectoryProperty = SimpleObjectProperty<File>()
|
||||||
var mapsDirectory by mapsDirectoryProperty
|
var mapsDirectory by mapsDirectoryProperty
|
||||||
@@ -131,7 +132,8 @@ class Project {
|
|||||||
var buildAssetsDir by buildAssetsDirProperty
|
var buildAssetsDir by buildAssetsDirProperty
|
||||||
private set
|
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 binaryProjectFile by binaryProjectFileProperty
|
||||||
|
|
||||||
val buildAssetsMapsDirProperty = createObjectBinding({ File(buildAssetsDir, MAPS_DIR) }, buildAssetsDirProperty)
|
val buildAssetsMapsDirProperty = createObjectBinding({ File(buildAssetsDir, MAPS_DIR) }, buildAssetsDirProperty)
|
||||||
|
|||||||
Reference in New Issue
Block a user