Skip to content

Commit e69d1a9

Browse files
Print parameter names for command with a special command #131
1 parent 521251e commit e69d1a9

12 files changed

Lines changed: 179 additions & 23 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ShellCli(commandsRegistry: CommandsRegistry) : Cli {
5959
command("version", VersionCommand)
6060
command("exit", ExitCommand)
6161
command("cd", CdCommand())
62+
command("parameters", ShowNonInteractiveParameters(commandsRegistry))
6263
}
6364

6465
commandParser = CommandParser(commandsRegistry, shellMode = true)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ private fun registerBaseCommands(commandsRegistry: CommandsRegistry) =
7171
commandsRegistry {
7272
command("help", HelpCommand)
7373
command("version", VersionCommand)
74+
command("parameters", ShowNonInteractiveParameters(commandsRegistry))
7475
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2008-2018 Haulmont.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.haulmont.cuba.cli.commands
18+
19+
interface NonInteractiveInfo {
20+
fun getNonInteractiveParameters() : Map<String, String>
21+
22+
fun List<String>.printOptions() = joinToString(prefix = "[", postfix = "]")
23+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2008-2018 Haulmont.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.haulmont.cuba.cli.commands
18+
19+
import com.beust.jcommander.Parameter
20+
import com.beust.jcommander.Parameters
21+
import com.haulmont.cuba.cli.bgRed
22+
import com.haulmont.cuba.cli.green
23+
import com.haulmont.cuba.cli.kodein
24+
import org.kodein.di.generic.instance
25+
import java.io.PrintWriter
26+
27+
@Parameters(commandDescription = "Prints commandPath parameters for non interactive mode")
28+
class ShowNonInteractiveParameters(private val commandsRegistry: CommandsRegistry) : AbstractCommand() {
29+
30+
@Parameter(variableArity = true)
31+
private var commandPath: List<String> = mutableListOf()
32+
33+
private val printWriter: PrintWriter by kodein.instance()
34+
35+
override fun run() {
36+
val currentPath = mutableListOf<String>()
37+
38+
commandsRegistry.traverse(object : CommandVisitor {
39+
override fun enterCommand(name: String, command: CliCommand) {
40+
currentPath.add(name)
41+
42+
if (currentPath == commandPath) {
43+
printParameters(command)
44+
45+
abort()
46+
}
47+
}
48+
49+
override fun exitCommand() {
50+
currentPath.removeAt(currentPath.lastIndex)
51+
}
52+
})
53+
54+
fail("Command \"${commandPath.joinToString(" ")}\" not found")
55+
}
56+
57+
private fun printParameters(command: CliCommand) {
58+
if (command is NonInteractiveInfo) {
59+
printWriter.println("Non interactive parameters for command ${commandPath.joinToString(" ").green()}:")
60+
command.getNonInteractiveParameters().forEach { param, description ->
61+
printWriter.println("$param\t$description")
62+
}
63+
} else printWriter.println("Command doesn't provide information about non interactive parameters".bgRed())
64+
}
65+
}

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/appcomponentxml/AppComponentCommand.kt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.haulmont.cuba.cli.ModuleStructure.Companion.CORE_MODULE
2121
import com.haulmont.cuba.cli.ModuleStructure.Companion.WEB_MODULE
2222
import com.haulmont.cuba.cli.PrintHelper
2323
import com.haulmont.cuba.cli.commands.GeneratorCommand
24+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2425
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2526
import com.haulmont.cuba.cli.generation.Properties
2627
import com.haulmont.cuba.cli.generation.Snippets
@@ -33,7 +34,7 @@ import org.kodein.di.generic.instance
3334
import java.nio.file.Path
3435

3536
@Parameters(commandDescription = "Generates app-component.xml")
36-
class AppComponentCommand : GeneratorCommand<AppComponentModel>() {
37+
class AppComponentCommand : GeneratorCommand<AppComponentModel>(), NonInteractiveInfo {
3738
private val messages by localMessages()
3839

3940
private val printHelper: PrintHelper by kodein.instance()
@@ -48,30 +49,44 @@ class AppComponentCommand : GeneratorCommand<AppComponentModel>() {
4849

4950
override fun getModelName(): String = AppComponentModel.MODEL_NAME
5051

52+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
53+
"modulePrefix" to "If specified, changes module prefix for project"
54+
)
55+
5156
override fun QuestionsList.prompting() {
52-
if (projectModel.modulePrefix == "app") {
57+
if (isNonInteractiveMode()) {
58+
askPrefix(projectModel.modulePrefix)
59+
} else if (projectModel.modulePrefix == "app") {
5360
confirmation("changePrefix", messages["changePrefix"])
5461

55-
question("modulePrefix", "New prefix") {
56-
askIf("changePrefix")
62+
askPrefix()
63+
}
64+
}
65+
66+
private fun QuestionsList.askPrefix(defaultPrefix: String? = null) {
67+
question("modulePrefix", "New prefix") {
68+
askIf("changePrefix")
69+
70+
defaultPrefix?.let {
71+
default(it)
72+
}
5773

58-
validate {
59-
if (value.isBlank())
60-
fail("Empty project prefix is not allowed")
74+
validate {
75+
if (value.isBlank())
76+
fail("Empty project prefix is not allowed")
6177

62-
val invalidNameRegex = Regex("[^\\w\\-]")
78+
val invalidNameRegex = Regex("[^\\w\\-]")
6379

64-
if (invalidNameRegex.find(value) != null) {
65-
fail("Project name should contain only Latin letters, digits, dashes and underscores.")
66-
}
80+
if (invalidNameRegex.find(value) != null) {
81+
fail("Project name should contain only Latin letters, digits, dashes and underscores.")
6782
}
6883
}
6984
}
7085
}
7186

7287
override fun createModel(answers: Answers): AppComponentModel {
7388
val modulePrefix: String = answers["modulePrefix"] as String? ?: projectModel.modulePrefix
74-
val changePrefix = answers["changePrefix"] as Boolean? ?: false
89+
val changePrefix = modulePrefix != projectModel.modulePrefix
7590
return AppComponentModel(changePrefix, modulePrefix)
7691
}
7792

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/browsescreen/CreateBrowseScreenCommand.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.haulmont.cuba.cli.cubaplugin.browsescreen
1818

1919
import com.beust.jcommander.Parameters
2020
import com.haulmont.cuba.cli.ModuleStructure
21+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2122
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2223
import com.haulmont.cuba.cli.cubaplugin.ScreenCommandBase
2324
import com.haulmont.cuba.cli.generation.Properties
@@ -29,13 +30,21 @@ import com.haulmont.cuba.cli.prompting.QuestionsList
2930
import net.sf.practicalxml.DomUtil
3031

3132
@Parameters(commandDescription = "Creates new browse screen")
32-
class CreateBrowseScreenCommand : ScreenCommandBase<BrowseScreenModel>() {
33+
class CreateBrowseScreenCommand : ScreenCommandBase<BrowseScreenModel>(), NonInteractiveInfo {
3334
override fun getModelName(): String = BrowseScreenModel.MODEL_NAME
3435

3536
override fun preExecute() {
3637
checkProjectExistence()
3738
}
3839

40+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
41+
"entityName" to "Fully qualified entity name",
42+
"packageName" to "Package name",
43+
"screenId" to "Screen id",
44+
"descriptorName" to "Descriptor name",
45+
"controllerName" to "Controller name"
46+
)
47+
3948
override fun QuestionsList.prompting() {
4049
val persistenceXml = projectStructure.getModule(ModuleStructure.GLOBAL_MODULE).persistenceXml
4150
val entitiesList = parse(persistenceXml).documentElement

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/componentbean/CreateComponentBeanCommand.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ package com.haulmont.cuba.cli.cubaplugin.componentbean
1818

1919
import com.beust.jcommander.Parameters
2020
import com.haulmont.cuba.cli.commands.GeneratorCommand
21+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2122
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2223
import com.haulmont.cuba.cli.generation.TemplateProcessor
2324
import com.haulmont.cuba.cli.prompting.Answers
2425
import com.haulmont.cuba.cli.prompting.QuestionsList
2526

2627
@Parameters(commandDescription = "Creates new Spring bean")
27-
class CreateComponentBeanCommand : GeneratorCommand<ComponentBeanModel>() {
28+
class CreateComponentBeanCommand : GeneratorCommand<ComponentBeanModel>(), NonInteractiveInfo {
2829
override fun getModelName(): String = ComponentBeanModel.MODEL_NAME
2930

3031
override fun preExecute() = checkProjectExistence()
3132

33+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
34+
"name" to "Class name",
35+
"packageName" to "Package name",
36+
"beanName" to "Bean name",
37+
"module" to "Target module, web or core."
38+
)
3239

3340
override fun QuestionsList.prompting() {
3441
question("name", "Class name") {

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/editscreen/CreateEditScreenCommand.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.haulmont.cuba.cli.cubaplugin.editscreen
1818

1919
import com.beust.jcommander.Parameters
2020
import com.haulmont.cuba.cli.ModuleStructure
21+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2122
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2223
import com.haulmont.cuba.cli.cubaplugin.ScreenCommandBase
2324
import com.haulmont.cuba.cli.generation.Properties
@@ -29,13 +30,21 @@ import com.haulmont.cuba.cli.prompting.QuestionsList
2930
import net.sf.practicalxml.DomUtil
3031

3132
@Parameters(commandDescription = "Creates new edit screen")
32-
class CreateEditScreenCommand : ScreenCommandBase<EditScreenModel>() {
33+
class CreateEditScreenCommand : ScreenCommandBase<EditScreenModel>(), NonInteractiveInfo {
3334
override fun getModelName(): String = EditScreenModel.MODEL_NAME
3435

3536
override fun preExecute() {
3637
checkProjectExistence()
3738
}
3839

40+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
41+
"entityName" to "Fully qualified entity name",
42+
"packageName" to "Package name",
43+
"screenId" to "Screen id",
44+
"descriptorName" to "Descriptor name",
45+
"controllerName" to "Controller name"
46+
)
47+
3948
override fun QuestionsList.prompting() {
4049
val persistenceXml = projectStructure.getModule(ModuleStructure.GLOBAL_MODULE).persistenceXml
4150
val entitiesList = parse(persistenceXml).documentElement

src/main/kotlin/com/haulmont/cuba/cli/cubaplugin/entity/CreateEntityCommand.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.haulmont.cuba.cli.ModuleStructure.Companion.GLOBAL_MODULE
2222
import com.haulmont.cuba.cli.PrintHelper
2323
import com.haulmont.cuba.cli.ProjectStructure
2424
import com.haulmont.cuba.cli.commands.GeneratorCommand
25+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2526
import com.haulmont.cuba.cli.commands.from
2627
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2728
import com.haulmont.cuba.cli.cubaplugin.NamesUtils
@@ -37,7 +38,7 @@ import java.nio.file.Files
3738
import java.util.*
3839

3940
@Parameters(commandDescription = "Creates new entity")
40-
class CreateEntityCommand : GeneratorCommand<EntityModel>() {
41+
class CreateEntityCommand : GeneratorCommand<EntityModel>(), NonInteractiveInfo {
4142
private val entityTypes = listOf("Persistent", "Persistent embedded", "Not persistent")
4243

4344
private val namesUtils: NamesUtils by kodein.instance()
@@ -54,6 +55,12 @@ class CreateEntityCommand : GeneratorCommand<EntityModel>() {
5455

5556
override fun getModelName(): String = EntityModel.MODEL_NAME
5657

58+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
59+
"entityName" to "Entity Name",
60+
"packageName" to "Package Name",
61+
"entityType" to "Entity type. Might be one of ${entityTypes.printOptions()}"
62+
)
63+
5764
override fun QuestionsList.prompting() {
5865
question("entityName", "Entity Name") {
5966
validate {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.haulmont.cuba.cli.cubaplugin.project
1919
import com.beust.jcommander.Parameters
2020
import com.haulmont.cuba.cli.*
2121
import com.haulmont.cuba.cli.commands.GeneratorCommand
22+
import com.haulmont.cuba.cli.commands.NonInteractiveInfo
2223
import com.haulmont.cuba.cli.cubaplugin.CubaPlugin
2324
import com.haulmont.cuba.cli.generation.TemplateProcessor
2425
import com.haulmont.cuba.cli.prompting.Answers
@@ -31,7 +32,7 @@ import java.nio.file.attribute.PosixFilePermission
3132
import java.util.*
3233

3334
@Parameters(commandDescription = "Creates new project")
34-
class ProjectInitCommand : GeneratorCommand<ProjectInitModel>() {
35+
class ProjectInitCommand : GeneratorCommand<ProjectInitModel>(), NonInteractiveInfo {
3536
private val messages by localMessages()
3637

3738
private val workingDirectoryManager: WorkingDirectoryManager by kodein.instance()
@@ -48,6 +49,14 @@ class ProjectInitCommand : GeneratorCommand<ProjectInitModel>() {
4849

4950
private val writer: PrintWriter by kodein.instance()
5051

52+
override fun getNonInteractiveParameters(): Map<String, String> = mapOf(
53+
"projectName" to "Project name",
54+
"namespace" to "Project namespace",
55+
"rootPackage" to "Root package",
56+
"platformVersion" to "Platform version",
57+
"database" to "Project database. Might be one of ${databasesAliases.printOptions()}."
58+
)
59+
5160
override fun getModelName(): String = "project"
5261

5362
override fun preExecute() {
@@ -56,9 +65,11 @@ class ProjectInitCommand : GeneratorCommand<ProjectInitModel>() {
5665

5766
override fun QuestionsList.prompting() {
5867
question("projectName", "Project Name") {
59-
default(
60-
ADJECTIVES.random() + "-" + ANIMALS.random()
61-
)
68+
if (!isNonInteractiveMode()) {
69+
default(
70+
ADJECTIVES.random() + "-" + ANIMALS.random()
71+
)
72+
}
6273

6374
validate {
6475
val invalidNameRegex = Regex("[^\\w\\-]")

0 commit comments

Comments
 (0)