Skip to content

Commit

Permalink
Prompt for plugin specific config during 'plugin initialize' (no proj…
Browse files Browse the repository at this point in the history
…ect specific prompts yet)
  • Loading branch information
martinvisser committed Sep 11, 2023
1 parent 6b750e8 commit baad546
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 92 deletions.
100 changes: 47 additions & 53 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.rabobank</groupId>
Expand Down Expand Up @@ -66,7 +67,6 @@
<quarkus-jgit.version>3.0.2</quarkus-jgit.version>
<quarkus-junit5-mockk.version>2.1.0</quarkus-junit5-mockk.version>
<sshd-core.version>2.10.0</sshd-core.version>
<testcontainers.version>1.19.0</testcontainers.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -107,30 +107,12 @@
<version>${quarkus-junit5-mockk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito.kotlin</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>${mockito-kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
Expand Down Expand Up @@ -169,7 +151,8 @@
<consoleOutputReporter>
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<statelessTestsetInfoReporter
implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<theme>UNICODE</theme>
</statelessTestsetInfoReporter>
</configuration>
Expand All @@ -189,54 +172,34 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<executions>
<!-- Replacing default-compile as it is treated specially by Maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by Maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<dependencies>
Expand All @@ -259,6 +222,37 @@
</pluginOptions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<executions>
<!-- Replacing default-compile as it is treated specially by Maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by Maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions ret-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import io.mockk.mockk
import io.mockk.verify
import io.rabobank.ret.RetConsole
import io.rabobank.ret.autocompletion.zsh.ZshAutocompletionGenerator
import io.rabobank.ret.configuration.Answer
import io.rabobank.ret.configuration.Config
import io.rabobank.ret.configuration.ConfigurationProperty
import io.rabobank.ret.configuration.Question
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import picocli.CommandLine.Model.CommandSpec
Expand Down Expand Up @@ -70,6 +72,10 @@ internal class ConfigureCommandTest {
configProps.forEach(function)
}

override fun prompt(function: (Question) -> Answer): List<Answer> {
TODO("Not yet implemented")
}

override fun configFile(): Path = Path.of("test-configuration")
}
}
3 changes: 2 additions & 1 deletion ret-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.rabobank</groupId>
Expand Down
2 changes: 1 addition & 1 deletion ret-core/src/main/kotlin/io/rabobank/ret/RetConsole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RetConsole(parseResult: ParseResult) {
*/
fun prompt(message: String, currentValue: String?): String {
val messageWithDefault = if (currentValue.isNullOrEmpty()) message else "$message [$currentValue]"
out(messageWithDefault)
out("$messageWithDefault:")
return readln()
}
}
12 changes: 12 additions & 0 deletions ret-core/src/main/kotlin/io/rabobank/ret/configuration/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ interface Config {
operator fun set(key: String, value: String)
fun configure(function: (ConfigurationProperty) -> Unit)
fun configFile(): Path
fun prompt(function: (Question) -> Answer): List<Answer>
}

data class Question(
val key: String,
val prompt: String,
val required: Boolean = false,
)

data class Answer(
val key: String,
val answer: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ package io.rabobank.ret.configuration
* and inputs are saved in the RET configuration file.
*/
interface Configurable {
fun properties(): List<ConfigurationProperty>
fun properties(): List<ConfigurationProperty> = emptyList()

fun prompts(): List<Question> = emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ class RetConfig(
* This is automatically called when initializing a plugin, so you normally do not call this yourself.
*/
override fun configure(function: (ConfigurationProperty) -> Unit) {
configurables.flatMap { it.properties() }.forEach(function)
configurables.flatMap(Configurable::properties).forEach(function)
save()
}

override fun prompt(function: (Question) -> Answer): List<Answer> =
configurables.flatMap(Configurable::prompts).map(function)

private fun save() {
properties[RET_VERSION] = retVersion
properties.store(configFile.outputStream(), "")
Expand Down
2 changes: 0 additions & 2 deletions ret-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jboss.jandex</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.rabobank.ret.commands

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.rabobank.ret.IntrospectionUtil
import io.rabobank.ret.RetConsole
import io.rabobank.ret.configuration.Answer
import io.rabobank.ret.configuration.Config
import io.rabobank.ret.util.OsUtils
import picocli.CommandLine.Command
Expand Down Expand Up @@ -43,18 +45,7 @@ class PluginInitializeCommand(
override fun run() {
val plugin = File(pluginName)
createPluginInformationFile(plugin)
configurePlugin(plugin.nameWithoutExtension)
}

private fun configurePlugin(pluginName: String) {
retConsole.out("Hello! Let's start configuring the $pluginName plugin.")

config.configure {
promptForOverride("${it.prompt}:", it.key)
}

retConsole.out("Done! Feel free to run this command again to make changes.")
retConsole.out("Wrote configuration to ${config.configFile()}")
configurePlugin(plugin)
}

private fun createPluginInformationFile(plugin: File) {
Expand All @@ -66,9 +57,48 @@ class PluginInitializeCommand(
}
}

private fun configurePlugin(plugin: File) {
val pluginName = plugin.nameWithoutExtension
retConsole.out("Hello! Let's start configuring the $pluginName plugin.")

config.configure { promptForOverride(it.prompt, it.key) }

storePluginAnswers(plugin)

retConsole.out("Done! Feel free to run this command again to make changes.")
retConsole.out("Wrote configuration to ${config.configFile()}")
}

private fun promptForOverride(message: String, key: String) {
val currentValue = config[key]
val input = retConsole.prompt(message, currentValue)
config[key] = input.ifEmpty { currentValue.orEmpty() }
}

private fun storePluginAnswers(plugin: File) {
var hasPluginSpecificConfig = false
val pluginConfigFile = pluginDirectory.resolve("${plugin.nameWithoutExtension}.json").toFile()
val pluginConfig = if (pluginConfigFile.exists()) objectMapper.readValue<Map<String, String>>(pluginConfigFile) else emptyMap()

val answers = config.prompt {
hasPluginSpecificConfig = true
val message = "${it.prompt}${if (it.required) " (required)" else ""}"
val currentValue = pluginConfig[it.key]
var input = retConsole.prompt(message, currentValue)

while (it.required && input.ifEmpty { currentValue.orEmpty() }.isEmpty()) {
retConsole.out("Please fill in an answer")
input = retConsole.prompt(message, currentValue)
}

Answer(it.key, input.ifEmpty { currentValue.orEmpty() })
}.associateBy { it.key }
.mapValues { it.value.answer }

if (hasPluginSpecificConfig) {
objectMapper.writeValue(pluginConfigFile, answers)

retConsole.out("Wrote configuration to $pluginConfigFile")
}
}
}
Loading

0 comments on commit baad546

Please sign in to comment.