Skip to content

Commit 0691900

Browse files
committed
cli: add stork/assembly + code cleanup
1 parent a615fa5 commit 0691900

File tree

7 files changed

+263
-74
lines changed

7 files changed

+263
-74
lines changed

modules/jooby-cli/pom.xml

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
<scope>provided</scope>
2020
</dependency>
2121

22-
<dependency>
23-
<groupId>io.jooby</groupId>
24-
<artifactId>jooby</artifactId>
25-
<version>${jooby.version}</version>
26-
</dependency>
27-
2822
<!-- https://mvnrepository.com/artifact/com.github.jknack/handlebars -->
2923
<dependency>
3024
<groupId>com.github.jknack</groupId>
@@ -39,14 +33,9 @@
3933
</dependency>
4034

4135
<dependency>
42-
<groupId>commons-io</groupId>
43-
<artifactId>commons-io</artifactId>
44-
<version>2.6</version>
45-
</dependency>
46-
47-
<dependency>
48-
<groupId>ch.qos.logback</groupId>
49-
<artifactId>logback-classic</artifactId>
36+
<groupId>org.slf4j</groupId>
37+
<artifactId>slf4j-simple</artifactId>
38+
<version>${slf4j.version}</version>
5039
</dependency>
5140

5241
<!-- https://mvnrepository.com/artifact/org.jline/jline -->
@@ -89,25 +78,34 @@
8978
</configuration>
9079
</plugin>
9180
<plugin>
92-
<artifactId>maven-shade-plugin</artifactId>
81+
<groupId>com.fizzed</groupId>
82+
<artifactId>stork-maven-plugin</artifactId>
83+
<version>3.0.0</version>
84+
<executions>
85+
<execution>
86+
<id>stork-launcher</id>
87+
<goals>
88+
<goal>launcher</goal>
89+
</goals>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
<plugin>
94+
<artifactId>maven-assembly-plugin</artifactId>
95+
<version>3.1.1</version>
9396
<executions>
9497
<execution>
95-
<id>fat-jar</id>
98+
<id>make-assembly</id>
9699
<phase>package</phase>
97100
<goals>
98-
<goal>shade</goal>
101+
<goal>single</goal>
99102
</goals>
100103
<configuration>
101-
<minimizeJar>true</minimizeJar>
102-
<createDependencyReducedPom>false</createDependencyReducedPom>
103-
<transformers>
104-
<transformer
105-
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
106-
<transformer
107-
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
108-
<mainClass>io.jooby.cli.Cli</mainClass>
109-
</transformer>
110-
</transformers>
104+
<finalName>${project.artifactId}-${project.version}</finalName>
105+
<appendAssemblyId>false</appendAssemblyId>
106+
<descriptors>
107+
<descriptor>src/assembly/cli.xml</descriptor>
108+
</descriptors>
111109
</configuration>
112110
</execution>
113111
</executions>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
4+
<id>jooby-cli</id>
5+
6+
<baseDirectory>/</baseDirectory>
7+
<includeBaseDirectory>false</includeBaseDirectory>
8+
9+
<formats>
10+
<format>zip</format>
11+
</formats>
12+
13+
<fileSets>
14+
<fileSet>
15+
<directory>${project.build.directory}${file.separator}stork${file.separator}bin</directory>
16+
<outputDirectory>bin</outputDirectory>
17+
<filtered>true</filtered>
18+
<includes>
19+
<include>**/*</include>
20+
</includes>
21+
</fileSet>
22+
</fileSets>
23+
24+
<dependencySets>
25+
<dependencySet>
26+
<outputDirectory>lib</outputDirectory>
27+
<scope>runtime</scope>
28+
</dependencySet>
29+
</dependencySets>
30+
31+
</assembly>

modules/jooby-cli/src/main/java/io/jooby/cli/Cli.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.util.List;
20+
import java.util.Objects;
21+
import java.util.stream.Collectors;
2022

2123
@CommandLine.Command(
2224
name = "jooby",
@@ -28,8 +30,23 @@ public class Cli extends Command {
2830
@CommandLine.Spec CommandLine.Model.CommandSpec spec;
2931
@CommandLine.Unmatched List<String> args;
3032

31-
@Override public void run(CommandContext ctx) {
32-
ctx.out.println(spec.commandLine().getUsageMessage());
33+
@Override public void run(CommandContext ctx) throws Exception {
34+
List<String> args = this.args.stream()
35+
.filter(Objects::nonNull)
36+
.map(String::trim)
37+
.filter(it -> it.length() > 0)
38+
.collect(Collectors.toList());
39+
if (args.size() > 0) {
40+
String arg = args.get(0);
41+
if ("-h".equals(arg) || "--help".equals(arg)) {
42+
ctx.out.println(spec.commandLine().getUsageMessage());
43+
} else if ("-V".equalsIgnoreCase(arg) || "--version".equals(arg)) {
44+
ctx.out.println(VersionProvider.version());
45+
} else {
46+
ctx.out.println(
47+
"Unknown command or option(s): " + args.stream().collect(Collectors.joining(" ")));
48+
}
49+
}
3350
}
3451

3552
public static void main(String[] args) throws IOException {

modules/jooby-cli/src/main/java/io/jooby/cli/Command.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
*/
66
package io.jooby.cli;
77

8-
import io.jooby.SneakyThrows;
9-
10-
public abstract class Command implements SneakyThrows.Runnable {
8+
public abstract class Command implements Runnable {
119
private CommandContext context;
1210

13-
@Override public void tryRun() throws Exception {
14-
run(context);
11+
@Override public void run() {
12+
try {
13+
run(context);
14+
} catch (Throwable x) {
15+
sneakyThrow0(x);
16+
}
1517
}
1618

1719
public abstract void run(CommandContext context) throws Exception;
1820

1921
public void setContext(CommandContext context) {
2022
this.context = context;
2123
}
24+
25+
@SuppressWarnings("unchecked")
26+
private static <E extends Throwable> void sneakyThrow0(final Throwable x) throws E {
27+
throw (E) x;
28+
}
2229
}

modules/jooby-cli/src/main/java/io/jooby/cli/CreateApp.java

Lines changed: 113 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,106 @@
2323
import java.util.stream.Stream;
2424

2525
@CommandLine.Command(name = "create", description = "Creates a new application")
26-
public class CreateApp extends Command implements Runnable {
27-
@CommandLine.Parameters
26+
public class CreateApp extends Command {
27+
@CommandLine.Parameters(
28+
description = "Application name or coordinates (groupId:artifactId:version)"
29+
)
2830
private String name;
2931

30-
@CommandLine.Option(names = {"-g", "--gradle"}, description = "Generates a gradle project")
32+
@CommandLine.Option(
33+
names = {"-g", "--gradle"},
34+
description = "Generates a Gradle project"
35+
)
3136
private boolean gradle;
3237

33-
@CommandLine.Option(names = {"-kt", "--kotlin"}, description = "Generates a Kotlin application")
38+
@CommandLine.Option(
39+
names = {"-kt", "--kotlin"},
40+
description = "Generates a Kotlin application"
41+
)
3442
private boolean kotlin;
3543

36-
@CommandLine.Option(names = {"-X"}, description = "Print debug information")
37-
private boolean debug;
38-
39-
@CommandLine.Option(names = {"-stork"}, description = "Generates a zip like distribution using stork (Maven only)")
44+
@CommandLine.Option(
45+
names = {"-stork"},
46+
description = "Add Stork Maven plugin to build (Maven only)"
47+
)
4048
private boolean stork;
4149

50+
@CommandLine.Option(
51+
names = {"-i"},
52+
description = "Start interactive mode"
53+
)
54+
private boolean interactive;
55+
56+
@CommandLine.Option(
57+
names = {"-s", "--server"},
58+
description = "Choose one of the available servers: jetty, netty or undertow"
59+
)
60+
private String server;
61+
4262
@Override public void run(CommandContext ctx) throws Exception {
43-
if (debug) {
44-
debug(ctx);
45-
}
4663
Path projectDir = Paths.get(System.getProperty("user.dir"), name);
4764
if (Files.exists(projectDir)) {
4865
throw new IOException("Project directory already exists: " + projectDir);
4966
}
5067
Files.createDirectory(projectDir);
68+
String packageName;
69+
String version;
70+
String server;
71+
boolean stork = !gradle && this.stork;
72+
if (interactive) {
73+
String useGradle = ctx.reader.readLine("Use Gradle (yes/No):");
74+
gradle = useGradle.equalsIgnoreCase("y") || useGradle.equals("yes");
75+
76+
String useKotlin = ctx.reader.readLine("Use Kotlin (yes/No):");
77+
kotlin = useKotlin.equalsIgnoreCase("y") || useKotlin.equals("yes");
78+
79+
packageName = ctx.reader.readLine("Enter a groupId/package: ");
80+
81+
version = ctx.reader.readLine("Enter a version (1.0.0): ");
82+
if (version == null || version.trim().length() == 0) {
83+
version = "1.0.0";
84+
}
85+
86+
server = server(ctx.reader.readLine("Choose a server (jetty, netty or undertow): "));
87+
88+
if (!gradle) {
89+
stork = distribution(ctx.reader.readLine("Distribution (uber/fat jar or stork): "))
90+
.equals("stork");
91+
}
92+
} else {
93+
String[] parts = name.split(":");
94+
switch (parts.length) {
95+
case 3:
96+
packageName = parts[0];
97+
name = parts[1];
98+
version = parts[2];
99+
break;
100+
case 2:
101+
packageName = parts[0];
102+
name = parts[1];
103+
version = "1.0.0";
104+
break;
105+
default:
106+
packageName = "app";
107+
version = "1.0.0";
108+
}
109+
server = server(this.server);
110+
}
51111
String templateName = gradle ? "build.gradle" : "maven";
52112
String buildFileName = gradle ? "build.gradle" : "pom.xml";
53-
String packageName = "app";
54113
String language = kotlin ? "kotlin" : "java";
55-
String extension = kotlin ? "kt" : "java";
56-
57-
boolean stork = !gradle && this.stork;
114+
String extension = language.equalsIgnoreCase("kotlin") ? "kt" : "java";
58115

59116
Map<String, Object> model = new HashMap<>();
60117
model.put("package", packageName);
61118
model.put("groupId", packageName);
62119
model.put("artifactId", name);
63-
model.put("version", "1.0");
64-
model.put("joobyVersion", new VersionProvider().getVersion()[0]);
65-
model.put("server", "netty");
120+
model.put("version", version);
121+
model.put("joobyVersion", VersionProvider.version());
122+
model.put("server", server);
66123
model.put("kotlin", kotlin);
67-
model.put("dependencies", dependencies("netty", kotlin));
124+
model.put("dependencies", dependencies(server, kotlin));
68125
model.put("testDependencies", testDependencies(kotlin));
69-
// Stork is available on maven only
70126
model.put("stork", stork);
71127

72128
ctx.writeTemplate(templateName, model, projectDir.resolve(buildFileName));
@@ -104,9 +160,45 @@ public class CreateApp extends Command implements Runnable {
104160
testPackagePath.resolve("IntegrationTest." + extension));
105161
}
106162

163+
private Object distribution(String value) {
164+
if (value == null || value.trim().length() == 0) {
165+
return "uber";
166+
}
167+
switch (value.toLowerCase()) {
168+
case "fat":
169+
case "uber":
170+
return "uber";
171+
case "stork":
172+
return "stork";
173+
default:
174+
throw new IllegalArgumentException("Unknown distribution option: " + value);
175+
}
176+
}
177+
178+
private String server(String value) {
179+
if (value == null || value.trim().length() == 0) {
180+
return "netty";
181+
}
182+
switch (value.toLowerCase()) {
183+
case "j":
184+
case "jetty":
185+
return "jetty";
186+
case "n":
187+
case "netty":
188+
return "netty";
189+
case "u":
190+
case "utow":
191+
case "undertow":
192+
return "utow";
193+
default:
194+
throw new IllegalArgumentException("Unknown server option: " + value);
195+
}
196+
}
197+
107198
private void stork(CommandContext ctx, Path projectDir, Map<String, Object> model)
108199
throws IOException {
109-
ctx.writeTemplate("stork.yml", model, projectDir.resolve("src").resolve("etc").resolve("stork.yml"));
200+
ctx.writeTemplate("stork.yml", model,
201+
projectDir.resolve("src").resolve("etc").resolve("stork.yml"));
110202
}
111203

112204
private void gradleWrapper(CommandContext ctx, Path projectDir, Map<String, Object> model)
@@ -125,22 +217,6 @@ private void gradleWrapper(CommandContext ctx, Path projectDir, Map<String, Obje
125217
wrapperDir.resolve("gradle-wrapper.properties"));
126218
}
127219

128-
private void debug(CommandContext ctx) {
129-
StringBuilder buffer = new StringBuilder();
130-
buffer.append("name: ").append(name);
131-
if (gradle) {
132-
buffer.append(" --gradle");
133-
} else {
134-
buffer.append(" --maven");
135-
}
136-
if (kotlin) {
137-
buffer.append(" --kotlin");
138-
} else {
139-
buffer.append(" --java");
140-
}
141-
ctx.out.println(buffer.toString());
142-
}
143-
144220
private void copyResource(String source, Path dest) throws IOException {
145221
copyResource(source, dest, Collections.emptySet());
146222
}

modules/jooby-cli/src/main/java/io/jooby/cli/VersionProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
public class VersionProvider implements CommandLine.IVersionProvider {
1414

15-
@Override public String[] getVersion() throws Exception {
16-
String version = Optional.ofNullable(getClass().getPackage())
15+
@Override public String[] getVersion() {
16+
return new String[] {version()};
17+
}
18+
19+
public static String version() {
20+
return Optional.ofNullable(VersionProvider.class.getPackage())
1721
.map(Package::getImplementationVersion)
1822
.filter(Objects::nonNull)
1923
.orElse("0.0.0");
20-
return new String[] {version};
2124
}
2225
}

0 commit comments

Comments
 (0)