2323import 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 }
0 commit comments