Skip to content

Commit 835996b

Browse files
committed
cli: MVC generator
1 parent d6cca6c commit 835996b

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public class CreateApp extends Command {
5858
private boolean gradle;
5959

6060
@CommandLine.Option(
61-
names = {"-kt", "--kotlin"},
61+
names = {"-k", "--kotlin"},
6262
description = "Generates a Kotlin application"
6363
)
6464
private boolean kotlin;
6565

6666
@CommandLine.Option(
67-
names = {"-stork"},
67+
names = {"-s", "--stork"},
6868
description = "Add Stork Maven plugin to build (Maven only)"
6969
)
7070
private boolean stork;
@@ -76,7 +76,7 @@ public class CreateApp extends Command {
7676
private boolean interactive;
7777

7878
@CommandLine.Option(
79-
names = {"-s", "--server"},
79+
names = {"--server"},
8080
description = "Choose one of the available servers: jetty, netty or undertow"
8181
)
8282
private String server;
@@ -87,6 +87,12 @@ public class CreateApp extends Command {
8787
)
8888
private boolean docker;
8989

90+
@CommandLine.Option(
91+
names = {"-m", "--mvc"},
92+
description = "Generates a MVC application"
93+
)
94+
private boolean mvc;
95+
9096
@Override public void run(CommandContext ctx) throws Exception {
9197
Path projectDir = Paths.get(System.getProperty("user.dir"), name);
9298
if (Files.exists(projectDir)) {
@@ -98,11 +104,9 @@ public class CreateApp extends Command {
98104
String server;
99105
boolean stork = !gradle && this.stork;
100106
if (interactive) {
101-
String useGradle = ctx.reader.readLine("Use Gradle (yes/No):");
102-
gradle = useGradle.equalsIgnoreCase("y") || useGradle.equals("yes");
107+
gradle = yesNo(ctx.reader.readLine("Use Gradle (yes/No): "));
103108

104-
String useKotlin = ctx.reader.readLine("Use Kotlin (yes/No):");
105-
kotlin = useKotlin.equalsIgnoreCase("y") || useKotlin.equals("yes");
109+
kotlin = yesNo(ctx.reader.readLine("Use Kotlin (yes/No): "));
106110

107111
packageName = ctx.reader.readLine("Enter a groupId/package: ");
108112

@@ -111,6 +115,8 @@ public class CreateApp extends Command {
111115
version = "1.0.0";
112116
}
113117

118+
mvc = yesNo(ctx.reader.readLine("Use MVC (yes/No): "));
119+
114120
server = server(ctx.reader.readLine("Choose a server (jetty, netty or undertow): "));
115121

116122
if (!gradle) {
@@ -163,6 +169,7 @@ public class CreateApp extends Command {
163169
model.put("gradle", gradle);
164170
model.put("maven", !gradle);
165171
model.put("docker", docker);
172+
model.put("mvc", mvc);
166173
model.put("finalArtifactId", finalArtifactId);
167174

168175
ctx.writeTemplate(templateName, model, projectDir.resolve(buildFileName));
@@ -191,6 +198,10 @@ public class CreateApp extends Command {
191198
.reduce(javaPath, Path::resolve, Path::resolve);
192199

193200
ctx.writeTemplate("App." + extension, model, packagePath.resolve("App." + extension));
201+
if (mvc) {
202+
ctx.writeTemplate("Controller." + extension, model,
203+
packagePath.resolve("Controller." + extension));
204+
}
194205

195206
/** Test directories: */
196207
Path testPath = projectDir.resolve("src").resolve("test");
@@ -204,6 +215,10 @@ public class CreateApp extends Command {
204215
testPackagePath.resolve("IntegrationTest." + extension));
205216
}
206217

218+
private boolean yesNo(String value) {
219+
return "y".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
220+
}
221+
207222
private void docker(CommandContext ctx, Path dir, Map<String, Object> model) throws IOException {
208223
boolean gradle = (Boolean) model.get("gradle");
209224
String dockerfile = gradle ? "docker.gradle" : "docker.maven";

modules/jooby-cli/src/main/resources/cli/App.java.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import io.jooby.Jooby;
55
public class App extends Jooby {
66

77
{
8+
{{#if mvc}}
9+
mvc(new Controller());
10+
{{else}}
811
get("/", ctx -> "Welcome to Jooby!");
12+
{{/if}}
913
}
1014

1115
public static void main(final String[] args) {

modules/jooby-cli/src/main/resources/cli/App.kt.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import io.jooby.Kooby
44
import io.jooby.runApp
55

66
class App: Kooby({
7+
{{#if mvc}}
8+
mvc(Controller())
9+
{{else}}
710
get("/") {
811
"Welcome to Jooby!"
912
}
13+
{{/if}}
1014
})
1115

1216
fun main(args: Array<String>) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package {{package}};
2+
3+
import io.jooby.annotations.*;
4+
5+
@Path("/")
6+
public class Controller {
7+
8+
@GET
9+
public String sayHi() {
10+
return "Welcome to Jooby!";
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package {{package}}
2+
3+
import io.jooby.annotations.*
4+
5+
@Path("/")
6+
class Controller {
7+
8+
@GET
9+
fun sayHi(): String {
10+
return "Welcome to Jooby!"
11+
}
12+
}

modules/jooby-cli/src/main/resources/cli/pom.xml.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<jooby.version>{{joobyVersion}}</jooby.version>
1919

2020
{{#if kotlin}}
21+
<kotlin.version>1.3.41</kotlin.version>
2122
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
2223
{{/if}}
2324
<maven.compiler.source>1.8</maven.compiler.source>

0 commit comments

Comments
 (0)