Skip to content

Commit

Permalink
CORE-17369: Enable display of CLI command versions (#201)
Browse files Browse the repository at this point in the history
Read version information from manifest.

Example output:

```text
corda-cli-plugin-host $ ./build/generatedScripts/corda-cli.sh --version
Corda CLI 5.1.0-SNAPSHOT

Provider: R3

Commit ID: 25201df

corda-cli-plugin-host $ ./build/generatedScripts/corda-cli.sh example-plugin --version
example-plugin 5.1.0-SNAPSHOT

Provider: R3

Commit ID: 25201df
```
  • Loading branch information
josephzunigadaly authored Nov 13, 2023
1 parent af3f87f commit cacb7c1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.corda.cli.api

import picocli.CommandLine.IVersionProvider
import java.util.jar.Manifest

/**
* An abstract class that will read version information out of the plugin manifest.
*
* Builds version information using these attributes:
* - Plugin-Name
* - Plugin-Version
* - Plugin-Provider
* - Plugin-Git-Commit
*
* To apply, inherit from this class:
*
* class VersionProvider : AbstractCordaCliVersionProvider()
*
* Then add to your command annotation:
*
* versionProvider = VersionProvider::class
*/
abstract class AbstractCordaCliVersionProvider : IVersionProvider {
override fun getVersion(): Array<String> = this.javaClass
.getResourceAsStream("/META-INF/MANIFEST.MF")
?.use {
Manifest(it).mainAttributes.run {
arrayOf(
"${getValue("Plugin-Name")} ${getValue("Plugin-Version")}",
"Provider: ${getValue("Plugin-Provider")}",
"Commit ID: ${getValue("Plugin-Git-Commit")}"
)
}
} ?: emptyArray()
}
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ def cordaCLi = tasks.named('jar', Jar) {
}
from(mergeLog4j2Plugins)

var cliHostCommitId = com.gradle.Utils.execAndGetStdOut "git", "rev-parse", "--verify", "HEAD"

manifest {
attributes["Main-Class"] = appMainClass
attributes["Multi-Release"] = true
attributes["Plugin-Name"] = "Corda CLI"
attributes["Plugin-Version"] = "${project.version}"
attributes["Plugin-Provider"] = "R3"
attributes["Plugin-Git-Commit"] = "$cliHostCommitId"
}

archiveBaseName = 'corda-cli'
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/kotlin/net/corda/cli/application/Boot.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.corda.cli.application

import net.corda.cli.api.CordaCliPlugin
import net.corda.cli.api.AbstractCordaCliVersionProvider
import net.corda.cli.application.logger.LoggerStream
import org.pf4j.CompoundPluginDescriptorFinder
import org.pf4j.DefaultPluginManager
Expand All @@ -14,13 +15,19 @@ fun main(vararg args: String) {
Boot.run(*args)
}

class VersionProvider : AbstractCordaCliVersionProvider()

@CommandLine.Command(
name = "corda-cli"
name = "corda-cli",
versionProvider = VersionProvider::class
)
class App {
@CommandLine.Option(names = ["-h", "--help", "-?", "-help"], usageHelp = true, description = ["Display help and exit."])
@Suppress("unused")
var help = false

@CommandLine.Option(names = ["-V", "--version"], versionHelp = true, description = ["Display version and exit"])
var showVersion = false
}

/**
Expand Down
4 changes: 4 additions & 0 deletions plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ subprojects { subProject ->

archiveBaseName = "plugin-${subProject.pluginId}"

var commitId = com.gradle.Utils.execAndGetStdOut "git", "rev-parse", "--verify", "HEAD"

manifest {
attributes['Plugin-Class'] = subProject.pluginClass
attributes["Plugin-Name"] = subProject.pluginId
attributes['Plugin-Id'] = subProject.pluginId
attributes['Plugin-Version'] = archiveVersion
attributes['Plugin-Provider'] = subProject.pluginProvider
attributes['Plugin-Description'] = subProject.pluginDescription
attributes["Plugin-Git-Commit"] = "$commitId"
}

from sourceSets.main.output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.corda.cli.plugins.examples
import org.pf4j.Extension
import org.pf4j.Plugin
import net.corda.cli.api.CordaCliPlugin
import net.corda.cli.api.AbstractCordaCliVersionProvider
import picocli.CommandLine

/**
Expand All @@ -16,12 +17,15 @@ class ExamplePlugin : Plugin() {
override fun stop() {
}

class VersionProvider : AbstractCordaCliVersionProvider()

@Extension
@CommandLine.Command(
name = "example-plugin",
subcommands = [SubCommandOne::class],
description = ["Example plugin using class based subcommands"],
mixinStandardHelpOptions = true
mixinStandardHelpOptions = true,
versionProvider = VersionProvider::class
)
class ExamplePluginEntry : CordaCliPlugin
}
8 changes: 8 additions & 0 deletions plugins/example/src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Main-Class: net.corda.cli.application.BootKt
Multi-Release: true
Plugin-Name: Corda CLI
Plugin-Version: 5.1.0-TEST
Plugin-Provider: R3
Plugin-Git-Commit: TEST

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.corda.cli.plugins.examples

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import picocli.CommandLine
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -61,6 +63,18 @@ class ExamplePluginTest {
) { outText }
}

@Test
fun testVersionOption() {

val app = ExamplePlugin.ExamplePluginEntry()

val outText = tapSystemOut {
CommandLine(app).execute("--version")
}

assertEquals("Corda CLI 5.1.0-TEST\nProvider: R3\nCommit ID: TEST\n", outText)
}

@Test
fun testSubCommand() {

Expand Down

0 comments on commit cacb7c1

Please sign in to comment.