Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

#474 add create-cli-app command that creates CLI app with the correct main class #59

Merged
merged 1 commit into from Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/features/picocli-groovy/feature.yml
@@ -0,0 +1,7 @@
description: Creates a picocli-based command line application using Groovy
dependentFeatures:
- groovy
- picocli
dependencies:
- scope: compile
coords: io.micronaut:runtime
@@ -0,0 +1,28 @@
package @defaultPackage@

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

@Command(name = '@project.name@', description = '...',
mixinStandardHelpOptions = true)
class @project.className@Command implements Runnable {

@Option(names = ['-v', '--verbose'], description = '...')
boolean verbose

static void main(String[] args) throws Exception {
PicocliRunner.run(@project.className@Command.class, args)
}

void run() {
// business logic here
if (verbose) {
println "Hi!"
}
}
}
9 changes: 9 additions & 0 deletions cli/features/picocli-java/feature.yml
@@ -0,0 +1,9 @@
description: Creates a picocli-based command line application using Java
dependentFeatures:
- java
- picocli
dependencies:
- scope: compile
coords: io.micronaut:runtime
- scope: testCompile
coords: io.micronaut:inject-java
@@ -0,0 +1,28 @@
package @defaultPackage@;

import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = "@project.name@", description = "...",
mixinStandardHelpOptions = true)
public class @project.className@Command implements Runnable {

@Option(names = {"-v", "--verbose"}, description = "...")
boolean verbose;

public static void main(String[] args) throws Exception {
PicocliRunner.run(@project.className@Command.class, args);
}

public void run() {
// business logic here
if (verbose) {
System.out.println("Hi!");
}
}
}
9 changes: 9 additions & 0 deletions cli/features/picocli-kotlin/feature.yml
@@ -0,0 +1,9 @@
description: Creates a picocli-based command line application using Kotlin
dependentFeatures:
- kotlin
- picocli
dependencies:
- scope: compile
coords: io.micronaut:runtime
- scope: testCompile
coords: io.micronaut:inject-java
@@ -0,0 +1,30 @@
package @defaultPackage@

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

@Command(name = "@project.name@", description = ["..."],
mixinStandardHelpOptions = true)
class @project.className@Command : Runnable {

@Option(names = ["-v", "--verbose"], description = ["..."])
private var verbose : Boolean = false

override fun run() {
// business logic here
if (verbose) {
println("Hi!")
}
}

companion object {
@JvmStatic fun main(args: Array<String>) {
PicocliRunner.run(@project.className@Command::class.java, *args)
}
}
}
11 changes: 11 additions & 0 deletions cli/features/test-picocli-junit/feature.yml
@@ -0,0 +1,11 @@
description: Creates JUnit test for a picocli-based command line application
dependentFeatures:
- junit
- picocli
#dependencies:
# - scope: testCompile
# coords: io.micronaut:function-client
# - scope: testRuntime
# coords: io.micronaut:http-server-netty
# - scope: testRuntime
# coords: io.micronaut:function-web
@@ -0,0 +1,28 @@
package @defaultPackage@;

import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;

import static org.junit.Assert.*;

public class @project.className@CommandTest {

@Test
public void testWithCommandLineOption() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));

try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) {
String[] args = new String[] { "-v" };
PicocliRunner.run(@project.className@Command.class, ctx, args);

// @project.name@
assertTrue(baos.toString(), baos.toString().contains("Hi!"));
}
}
}
13 changes: 13 additions & 0 deletions cli/features/test-picocli-spek/feature.yml
@@ -0,0 +1,13 @@
description: Creates Spek test for a picocli-based command line application
dependentFeatures:
- spek
- picocli
#dependencies:
# - scope: testCompile
# coords: io.micronaut:function-client
# - scope: testCompile
# coords: io.micronaut:http-client
# - scope: testRuntime
# coords: io.micronaut:http-server-netty
# - scope: testRuntime
# coords: io.micronaut:function-web
@@ -0,0 +1,37 @@
package @defaultPackage@

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.Environment

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import org.junit.jupiter.api.Assertions.*

import java.io.ByteArrayOutputStream
import java.io.PrintStream

object @project.className@CommandTest : Spek({

describe("@project.name@") {
val ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)

on("invocation with -v") {
val baos = ByteArrayOutputStream()
System.setOut(PrintStream(baos))

val args = arrayOf("-v")
PicocliRunner.run(@project.className@Command::class.java, ctx, *args)

it("should display greeting") {
assertTrue(baos.toString().contains("Hi!"))
}
}

on("other") {
// add more tests...
}
}
})
11 changes: 11 additions & 0 deletions cli/features/test-picocli-spock/feature.yml
@@ -0,0 +1,11 @@
description: Creates Spock test for a picocli-based command line application
dependentFeatures:
- spock
- picocli
#dependencies:
# - scope: testCompile
# coords: io.micronaut:function-client
# - scope: testRuntime
# coords: io.micronaut:http-server-netty
# - scope: testRuntime
# coords: io.micronaut:function-web
@@ -0,0 +1,29 @@
package @defaultPackage@

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.Environment

import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

import java.io.ByteArrayOutputStream
import java.io.PrintStream

class @project.className@CommandSpec extends Specification {

@Shared @AutoCleanup ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)

void "test @project.name@ with command line option"() {
given:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
System.setOut(new PrintStream(baos))

String[] args = ['-v'] as String[]
PicocliRunner.run(@project.className@Command, ctx, args)

expect:
baos.toString().contains('Hi!')
}
}
8 changes: 7 additions & 1 deletion cli/skeleton/gradle-build/build.gradle
@@ -1,2 +1,8 @@
mainClassName = "@defaultPackage@.@project.className@Command"
applicationDefaultJvmArgs = [""]

mainClassName = "@defaultPackage@.Application"
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
2 changes: 1 addition & 1 deletion cli/skeleton/maven-build/pom.xml
@@ -1,6 +1,6 @@
<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">
<properties>
<exec.mainClass>@defaultPackage@.Application</exec.mainClass>
<exec.mainClass>@defaultPackage@.@project.className@Command</exec.mainClass>
</properties>
</project>
7 changes: 6 additions & 1 deletion cli/templates/kotlin/Command.kt
Expand Up @@ -21,5 +21,10 @@ class ${className} : Runnable {
println("Hi!")
}
}

companion object {
@JvmStatic fun main(args: Array<String>) {
PicocliRunner.run(${className}::class.java, *args)
}
}
}
fun main(args: Array<String>) = PicocliRunner.run(${className}::class.java, *args)