Skip to content

Commit ef14b36

Browse files
Different versions of the platform are suggested at the project creating #142
Added ability to process templates into streams
1 parent cfe8e57 commit ef14b36

6 files changed

Lines changed: 59 additions & 18 deletions

File tree

src/main/kotlin/com/haulmont/cuba/cli/EntryPoint.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ fun main(args: Array<String>) {
9595
if (mode == CliMode.SHELL) {
9696
parseLaunchOptions(args)
9797
setupLogger()
98-
99-
val versionManager = kodein.direct.instance<PlatformVersionsManager>()
100-
versionManager.load()
10198
}
10299

100+
val versionManager = kodein.direct.instance<PlatformVersionsManager>()
101+
versionManager.load()
102+
103103
val commandsRegistry = CommandsRegistry()
104104

105105
loadPlugins(commandsRegistry, mode)

src/main/kotlin/com/haulmont/cuba/cli/PlatformVersionsManager.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.net.URL
2222
import java.util.logging.Level
2323
import java.util.logging.Logger
2424
import kotlin.concurrent.thread
25+
import kotlin.properties.Delegates
2526

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

35+
var loadThread: Thread? by Delegates.vetoable<Thread?>(null) { _, oldValue, _ ->
36+
oldValue == null
37+
}
38+
3439
fun load() {
35-
if (!LaunchOptions.skipVersionLoading) thread(isDaemon = true) {
36-
try {
37-
versions = loadInfoJson()
38-
.let(::extractVersions)
39-
.let(::filterVersions)
40-
} catch (e: Throwable) {
41-
logger.log(Level.SEVERE, "Error during platform versions retrieving", e)
40+
if (!LaunchOptions.skipVersionLoading) {
41+
loadThread = thread(isDaemon = true) {
42+
try {
43+
versions = loadInfoJson()
44+
.let(::extractVersions)
45+
.let(::filterVersions)
46+
} catch (e: Throwable) {
47+
logger.log(Level.SEVERE, "Error during platform versions retrieving", e)
48+
}
4249
}
4350
}
4451
}

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/project/ProjectInitCommand.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ import java.nio.file.Files
2929
import java.nio.file.Paths
3030
import java.nio.file.attribute.PosixFilePermission
3131
import java.util.*
32+
import java.util.logging.Level
33+
import java.util.logging.Logger
3234

3335
@Parameters(commandDescription = "Creates new project")
3436
class ProjectInitCommand : GeneratorCommand<ProjectInitModel>(), NonInteractiveInfo {
37+
private val logger = Logger.getLogger(ProjectInitCommand::class.java.name)
38+
3539
private val messages by localMessages()
3640

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

6367
override fun preExecute() {
6468
!context.hasModel("project") || fail("There is an existing project found in current directory.")
69+
70+
try {
71+
platformVersionsManager.loadThread?.join(20_000)
72+
} catch (e: Exception) {
73+
logger.log(Level.SEVERE, e) { "" }
74+
}
6575
}
6676

6777
override fun QuestionsList.prompting() {

src/main/kotlin/com/haulmont/cuba/cli/generation/TemplateProcessor.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.apache.velocity.VelocityContext
2222
import org.apache.velocity.app.Velocity
2323
import org.kodein.di.generic.instance
2424
import java.io.File
25+
import java.io.OutputStream
2526
import java.nio.file.Files
2627
import java.nio.file.Path
2728
import java.nio.file.Paths
@@ -44,8 +45,7 @@ import kotlin.reflect.full.memberProperties
4445
* Packages of all your models should be opened in order to Apache Velocity may access them through reflexion.
4546
*
4647
*/
47-
class TemplateProcessor {
48-
private val bindings: Map<String, Any>
48+
class TemplateProcessor(templateBasePath: Path, private val bindings: Map<String, Any>, version: PlatformVersion = PlatformVersion.findVersion()) {
4949

5050
private val printHelper: PrintHelper by kodein.instance()
5151

@@ -57,11 +57,9 @@ class TemplateProcessor {
5757

5858
private val velocityHelper: VelocityHelper = VelocityHelper()
5959

60-
val templatePath: Path
60+
val templatePath: Path = version.findMostSuitableVersionDirectory(templateBasePath)
6161

62-
private constructor(templateBasePath: Path, bindings: Map<String, Any>, version: PlatformVersion) {
63-
templatePath = version.findMostSuitableVersionDirectory(templateBasePath)
64-
this.bindings = bindings
62+
init {
6563
this.velocityContext = VelocityContext().apply {
6664
bindings.forEach { k, v -> put(k, v) }
6765
}
@@ -167,6 +165,24 @@ class TemplateProcessor {
167165
process(templatePath.resolve(subPath), to, true)
168166
}
169167

168+
fun transform(subPath: String, to: OutputStream) {
169+
val filePath = templatePath.resolve(subPath)
170+
171+
check(Files.isRegularFile(filePath)) { "Only file may be saved to output stream" }
172+
173+
velocityHelper.generate(filePath, velocityContext).let {
174+
to.write(it.toByteArray())
175+
}
176+
}
177+
178+
fun copy(subPath: String, to: OutputStream) {
179+
val filePath = templatePath.resolve(subPath)
180+
181+
check(Files.isRegularFile(filePath)) { "Only file may be saved to output stream" }
182+
183+
Files.newInputStream(filePath).copyTo(to)
184+
}
185+
170186
fun transformWhole(to: Path = projectRoot) {
171187
transform("", to)
172188
}

src/main/kotlin/com/haulmont/cuba/cli/generation/VelocityHelper.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ class VelocityHelper {
3737
}.toString()
3838
}
3939

40-
fun generate(inputPath: Path, outputFile: Path, vc: VelocityContext) {
40+
fun generate(inputPath: Path, vc: VelocityContext): String {
4141
val templateText = Files.newInputStream(inputPath)
4242
.bufferedReader().readText()
4343
val templateName = generate(templateText, inputPath.fileName.toString(), vc)
4444

45-
val output = generate(templateText, templateName, vc)
45+
return generate(templateText, templateName, vc)
46+
}
47+
48+
fun generate(inputPath: Path, outputFile: Path, vc: VelocityContext) {
49+
val output = generate(inputPath, vc)
4650

4751
Files.newBufferedWriter(outputFile).use { writer ->
4852
writer.write(output)

src/main/kotlin/com/haulmont/cuba/cli/prompting/PromptsDsl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class ValidationHelper<T : Any>(val value: T, val answers: Answers) {
241241
fail(failMessage)
242242
}
243243

244+
fun checkIsNotBlank(failMessage: String = "Specify value") {
245+
value is String && value.isBlank() && fail(failMessage)
246+
}
247+
244248
fun checkIsPackage(failMessage: String = "Is not valid package name") =
245249
checkRegex("[a-zA-Z][0-9a-zA-Z]*(\\.[a-zA-Z][0-9a-zA-Z]*)*", failMessage)
246250

0 commit comments

Comments
 (0)