Skip to content

Commit

Permalink
Different versions of the platform are suggested at the project creat…
Browse files Browse the repository at this point in the history
…ing #142

Added ability to process templates into streams
  • Loading branch information
alexander-shustanov committed Sep 3, 2018
1 parent cfe8e57 commit ef14b36
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/com/haulmont/cuba/cli/EntryPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ fun main(args: Array<String>) {
if (mode == CliMode.SHELL) {
parseLaunchOptions(args)
setupLogger()

val versionManager = kodein.direct.instance<PlatformVersionsManager>()
versionManager.load()
}

val versionManager = kodein.direct.instance<PlatformVersionsManager>()
versionManager.load()

val commandsRegistry = CommandsRegistry()

loadPlugins(commandsRegistry, mode)
Expand Down
21 changes: 14 additions & 7 deletions src/main/kotlin/com/haulmont/cuba/cli/PlatformVersionsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.net.URL
import java.util.logging.Level
import java.util.logging.Logger
import kotlin.concurrent.thread
import kotlin.properties.Delegates

class PlatformVersionsManager {
private val messages by localMessages()
Expand All @@ -31,14 +32,20 @@ class PlatformVersionsManager {
var versions: List<String> = messages["platformVersions"].split(",").map { it.trim() }
private set

var loadThread: Thread? by Delegates.vetoable<Thread?>(null) { _, oldValue, _ ->
oldValue == null
}

fun load() {
if (!LaunchOptions.skipVersionLoading) thread(isDaemon = true) {
try {
versions = loadInfoJson()
.let(::extractVersions)
.let(::filterVersions)
} catch (e: Throwable) {
logger.log(Level.SEVERE, "Error during platform versions retrieving", e)
if (!LaunchOptions.skipVersionLoading) {
loadThread = thread(isDaemon = true) {
try {
versions = loadInfoJson()
.let(::extractVersions)
.let(::filterVersions)
} catch (e: Throwable) {
logger.log(Level.SEVERE, "Error during platform versions retrieving", e)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.PosixFilePermission
import java.util.*
import java.util.logging.Level
import java.util.logging.Logger

@Parameters(commandDescription = "Creates new project")
class ProjectInitCommand : GeneratorCommand<ProjectInitModel>(), NonInteractiveInfo {
private val logger = Logger.getLogger(ProjectInitCommand::class.java.name)

private val messages by localMessages()

private val resources by Resources.fromMyPlugin()
Expand Down Expand Up @@ -62,6 +66,12 @@ class ProjectInitCommand : GeneratorCommand<ProjectInitModel>(), NonInteractiveI

override fun preExecute() {
!context.hasModel("project") || fail("There is an existing project found in current directory.")

try {
platformVersionsManager.loadThread?.join(20_000)
} catch (e: Exception) {
logger.log(Level.SEVERE, e) { "" }
}
}

override fun QuestionsList.prompting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.kodein.di.generic.instance
import java.io.File
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand All @@ -44,8 +45,7 @@ import kotlin.reflect.full.memberProperties
* Packages of all your models should be opened in order to Apache Velocity may access them through reflexion.
*
*/
class TemplateProcessor {
private val bindings: Map<String, Any>
class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String, Any>, version: PlatformVersion = PlatformVersion.findVersion()) {

private val printHelper: PrintHelper by kodein.instance()

Expand All @@ -57,11 +57,9 @@ class TemplateProcessor {

private val velocityHelper: VelocityHelper = VelocityHelper()

val templatePath: Path
val templatePath: Path = version.findMostSuitableVersionDirectory(templateBasePath)

private constructor(templateBasePath: Path, bindings: Map<String, Any>, version: PlatformVersion) {
templatePath = version.findMostSuitableVersionDirectory(templateBasePath)
this.bindings = bindings
init {
this.velocityContext = VelocityContext().apply {
bindings.forEach { k, v -> put(k, v) }
}
Expand Down Expand Up @@ -167,6 +165,24 @@ class TemplateProcessor {
process(templatePath.resolve(subPath), to, true)
}

fun transform(subPath: String, to: OutputStream) {
val filePath = templatePath.resolve(subPath)

check(Files.isRegularFile(filePath)) { "Only file may be saved to output stream" }

velocityHelper.generate(filePath, velocityContext).let {
to.write(it.toByteArray())
}
}

fun copy(subPath: String, to: OutputStream) {
val filePath = templatePath.resolve(subPath)

check(Files.isRegularFile(filePath)) { "Only file may be saved to output stream" }

Files.newInputStream(filePath).copyTo(to)
}

fun transformWhole(to: Path = projectRoot) {
transform("", to)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ class VelocityHelper {
}.toString()
}

fun generate(inputPath: Path, outputFile: Path, vc: VelocityContext) {
fun generate(inputPath: Path, vc: VelocityContext): String {
val templateText = Files.newInputStream(inputPath)
.bufferedReader().readText()
val templateName = generate(templateText, inputPath.fileName.toString(), vc)

val output = generate(templateText, templateName, vc)
return generate(templateText, templateName, vc)
}

fun generate(inputPath: Path, outputFile: Path, vc: VelocityContext) {
val output = generate(inputPath, vc)

Files.newBufferedWriter(outputFile).use { writer ->
writer.write(output)
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/haulmont/cuba/cli/prompting/PromptsDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ class ValidationHelper<T : Any>(val value: T, val answers: Answers) {
fail(failMessage)
}

fun checkIsNotBlank(failMessage: String = "Specify value") {
value is String && value.isBlank() && fail(failMessage)
}

fun checkIsPackage(failMessage: String = "Is not valid package name") =
checkRegex("[a-zA-Z][0-9a-zA-Z]*(\\.[a-zA-Z][0-9a-zA-Z]*)*", failMessage)

Expand Down

0 comments on commit ef14b36

Please sign in to comment.