From 8d9de5517aeedccfad97b789390de67c26a749ed Mon Sep 17 00:00:00 2001 From: ansgarz Date: Wed, 11 Dec 2024 22:02:11 +0100 Subject: [PATCH] add version info output --- build.gradle | 17 ++++++++++++++ .../provs/desktop/application/Application.kt | 7 ++++-- .../provs/framework/core/cli/CliUtils.kt | 22 ++++++++++++------- .../provs/server/application/Application.kt | 3 +++ src/main/resources/version.txt | 1 + .../desktop/application/ApplicationKtTest.kt | 12 +++++++--- 6 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 src/main/resources/version.txt diff --git a/build.gradle b/build.gradle index 118397b..038dfde 100644 --- a/build.gradle +++ b/build.gradle @@ -234,3 +234,20 @@ publishing { } } } + + +// create version file to allow Kotlin code to print own version - see https://stackoverflow.com/questions/33020069/how-to-get-version-attribute-from-a-gradle-build-to-be-included-in-runtime-swing +tasks.register('createVersion') { + dependsOn processResources + doLast { + def version = project.version.toString() + " (" + Instant.now().toString().split("\\.")[0] + ")" + def fileName = "src/main/resources/version.txt" + def file = new File(fileName) + file.write(version) + println "Created file: " + fileName + } +} + +classes { + dependsOn createVersion +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt index 6f40e37..796af92 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt @@ -6,6 +6,7 @@ import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig import org.domaindrivenarchitecture.provs.desktop.domain.provisionDesktopCommand import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance +import org.domaindrivenarchitecture.provs.framework.core.cli.printProvsVersion import org.domaindrivenarchitecture.provs.framework.core.cli.quit import java.io.FileNotFoundException import java.nio.file.Files @@ -17,6 +18,8 @@ import kotlin.system.exitProcess */ fun main(args: Array) { + printProvsVersion() + val cmd = CliArgumentsParser("provs-desktop.jar subcommand target").parseCommand(args) if (!cmd.isValid()) { println("Arguments are not valid, pls try option -h for help.") @@ -31,12 +34,12 @@ fun main(args: Array) { val configFileName = cmd.configFile?.fileName ?: defaultConfigFileName try { getConfig(configFileName) - } catch (e: SerializationException) { + } catch (_: SerializationException) { println( "Error: File \"${configFileName}\" has an invalid format and or invalid data." ) null - } catch (e: FileNotFoundException) { + } catch (_: FileNotFoundException) { println( "Error: File\u001b[31m $configFileName \u001b[0m was not found.\n" + "Pls copy file \u001B[31m desktop-config-example.yaml \u001B[0m to file \u001B[31m $configFileName \u001B[0m " + diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/cli/CliUtils.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/cli/CliUtils.kt index 30c9c3f..e03a603 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/cli/CliUtils.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/cli/CliUtils.kt @@ -35,6 +35,20 @@ fun createProvInstance(targetCommand: TargetCliCommand): Prov { } +/** + * Wrapper for exitProcess, which allows e.g. mocking for test purposes + */ +fun quit(status: Int): Nothing { + exitProcess(status) +} + + +fun printProvsVersion() { + val version = object {}.javaClass.getResource("/version.txt")?.readText()?.trim() + println("Provs version: $version") +} + + internal fun createRemoteProvInstance( target: TargetCliCommand.RemoteTarget?, password: Secret? = null @@ -52,11 +66,3 @@ internal fun createRemoteProvInstance( internal fun getPasswordToConfigureSudoWithoutPassword(): Secret { return PromptSecretSource("password to configure sudo without password.").secret() } - - -/** - * Wrapper for exitProcess, which allows e.g. mocking for test purposes - */ -fun quit(status: Int): Nothing { - exitProcess(status) -} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt index afd6ec5..5b7fdcb 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt @@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.server.application import org.domaindrivenarchitecture.provs.configuration.application.ensureSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance +import org.domaindrivenarchitecture.provs.framework.core.cli.printProvsVersion import org.domaindrivenarchitecture.provs.framework.core.cli.quit import org.domaindrivenarchitecture.provs.server.domain.ServerType import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand @@ -17,6 +18,8 @@ import kotlin.system.exitProcess */ fun main(args: Array) { + printProvsVersion() + val checkedArgs = if (args.isEmpty()) arrayOf("-h") else args // validate subcommand diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt new file mode 100644 index 0000000..4ff45a2 --- /dev/null +++ b/src/main/resources/version.txt @@ -0,0 +1 @@ +0.39.3-SNAPSHOT (2024-12-11T20:58:26) \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt index cae0bb7..d36ac4d 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt @@ -15,7 +15,7 @@ import org.domaindrivenarchitecture.provs.framework.core.cli.quit import org.domaindrivenarchitecture.provs.framework.core.processors.DummyProcessor import org.domaindrivenarchitecture.provs.test.setRootLoggingLevel import org.junit.jupiter.api.AfterAll -import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -116,7 +116,10 @@ internal class ApplicationKtTest { val expectedOutput = "Error: File\u001B[31m idontexist.yaml \u001B[0m was not found.Pls copy file \u001B[31m desktop-config-example.yaml \u001B[0m to file \u001B[31m idontexist.yaml \u001B[0m and change the content according to your needs.No suitable config found." - assertEquals(expectedOutput, outContent.toString().replace("\r", "").replace("\n", "")) + assertTrue( + outContent.toString().replace("\r", "").replace("\n", "").contains(expectedOutput), + "$expectedOutput\nnot found in:\n$outContent" + ) verify(exactly = 0) { any().provisionDesktop(any(), any(), any(), any(), any()) } @@ -150,7 +153,10 @@ internal class ApplicationKtTest { val expectedOutput = "Error: File \"src/test/resources/invalid-desktop-config.yaml\" has an invalid format and or invalid data.No suitable config found." - assertEquals(expectedOutput, outContent.toString().replace("\r", "").replace("\n", "")) + assertTrue( + outContent.toString().replace("\r", "").replace("\n", "").contains(expectedOutput), + "$expectedOutput\nnot found in:\n$outContent" + ) verify(exactly = 0) { any().provisionDesktop(any(), any(), any(), any(), any()) }