Change the "--library" option into a config option

This commit is contained in:
xhh 2015-08-07 22:16:32 +08:00
parent 167e95bb3a
commit a41361c959
7 changed files with 31 additions and 34 deletions

View File

@ -116,10 +116,6 @@ OPTIONS
client language to generate (maybe class name in classpath, client language to generate (maybe class name in classpath,
required) required)
-L <library>, --library <library>
Library template (sub-template) to use. Run library-help -l {lang}
command for a list of supported libraries.
-o <output directory>, --output <output directory> -o <output directory>, --output <output directory>
where to write the generated files (current dir by default) where to write the generated files (current dir by default)
@ -148,23 +144,6 @@ Other languages have petstore samples, too:
./bin/objc-petstore.sh ./bin/objc-petstore.sh
``` ```
Various library templates (sub-templates) might be available for a specific language. Running `library-help -l {lang}` will show all library templates supported.
```
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar library-help -l java
```
Output
```
LIBRARY OPTIONS
<default>
HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2
jersey2
HTTP client: Jersey client 2.6
```
### Generating libraries from your server ### Generating libraries from your server
It's just as easy--just use the `-i` flag to point to either a server or file. It's just as easy--just use the `-i` flag to point to either a server or file.
@ -270,6 +249,11 @@ CONFIG OPTIONS
sourceFolder sourceFolder
source folder for generated code source folder for generated code
library
library template (sub-template) to use:
<default> - HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2
jersey2 - HTTP client: Jersey client 2.6
``` ```
Your config file for java can look like Your config file for java can look like

View File

@ -1 +1,4 @@
{"artifactId": "swagger-petstore-jersey2"} {
"library": "jersey2",
"artifactId": "swagger-petstore-jersey2"
}

View File

@ -26,6 +26,6 @@ fi
# if you've executed sbt assembly previously it will use that instead. # if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java --library jersey2 -c bin/java-petstore-jersey2.json -o samples/client/petstore/java/jersey2" ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java -c bin/java-petstore-jersey2.json -o samples/client/petstore/java/jersey2"
java $JAVA_OPTS -jar $executable $ags java $JAVA_OPTS -jar $executable $ags

View File

@ -45,7 +45,7 @@ public class ConfigHelp implements Runnable {
System.out.println("CONFIG OPTIONS"); System.out.println("CONFIG OPTIONS");
for (CliOption langCliOption : config.cliOptions()) { for (CliOption langCliOption : config.cliOptions()) {
System.out.println("\t" + langCliOption.getOpt()); System.out.println("\t" + langCliOption.getOpt());
System.out.println("\t " + langCliOption.getDescription()); System.out.println("\t " + langCliOption.getDescription().replaceAll("\n", "\n\t "));
System.out.println(); System.out.println();
} }
} }

View File

@ -40,11 +40,6 @@ public class Generate implements Runnable {
description = "client language to generate (maybe class name in classpath, required)") description = "client language to generate (maybe class name in classpath, required)")
private String lang; private String lang;
@Option(name = {"-L", "--library"}, title = "library",
description = "Library template (sub-template) to use. Run library-help -l {lang} " +
"command for a list of supported libraries.")
private String library;
@Option(name = {"-o", "--output"}, title = "output directory", @Option(name = {"-o", "--output"}, title = "output directory",
description = "where to write the generated files (current dir by default)") description = "where to write the generated files (current dir by default)")
private String output = ""; private String output = "";
@ -110,7 +105,6 @@ public class Generate implements Runnable {
} }
CodegenConfig config = forName(lang); CodegenConfig config = forName(lang);
config.setLibrary(library);
config.setOutputDir(new File(output).getAbsolutePath()); config.setOutputDir(new File(output).getAbsolutePath());
if (null != templateDir) { if (null != templateDir) {
@ -121,8 +115,13 @@ public class Generate implements Runnable {
Config genConfig = ConfigParser.read(configFile); Config genConfig = ConfigParser.read(configFile);
if (null != genConfig) { if (null != genConfig) {
for (CliOption langCliOption : config.cliOptions()) { for (CliOption langCliOption : config.cliOptions()) {
if (genConfig.hasOption(langCliOption.getOpt())) { String opt = langCliOption.getOpt();
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt())); if (genConfig.hasOption(opt)) {
config.additionalProperties().put(opt, genConfig.getOption(opt));
// the "library" config option is for library template (sub-template)
if ("library".equals(opt)) {
config.setLibrary(genConfig.getOption(opt));
}
} }
} }
} }

View File

@ -55,6 +55,7 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -81,7 +82,7 @@ public class DefaultCodegen {
protected List<CliOption> cliOptions = new ArrayList<CliOption>(); protected List<CliOption> cliOptions = new ArrayList<CliOption>();
protected boolean skipOverwrite; protected boolean skipOverwrite;
protected boolean supportsInheritance = false; protected boolean supportsInheritance = false;
protected Map<String, String> supportedLibraries = new HashMap<String, String>(); protected Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
protected String library = null; protected String library = null;
public List<CliOption> cliOptions() { public List<CliOption> cliOptions() {
@ -1392,6 +1393,7 @@ public class DefaultCodegen {
/** /**
* All library templates supported. * All library templates supported.
* (key: library name, value: library description)
*/ */
public Map<String, String> supportedLibraries() { public Map<String, String> supportedLibraries() {
return supportedLibraries; return supportedLibraries;
@ -1409,4 +1411,12 @@ public class DefaultCodegen {
public String getLibrary() { public String getLibrary() {
return library; return library;
} }
protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries) {
StringBuilder sb = new StringBuilder("library template (sub-template) to use:");
for (String lib : supportedLibraries.keySet()) {
sb.append("\n").append(lib).append(" - ").append(supportedLibraries.get(lib));
}
return new CliOption("library", sb.toString());
}
} }

View File

@ -62,8 +62,9 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
cliOptions.add(new CliOption("artifactVersion", "artifact version in generated pom.xml")); cliOptions.add(new CliOption("artifactVersion", "artifact version in generated pom.xml"));
cliOptions.add(new CliOption("sourceFolder", "source folder for generated code")); cliOptions.add(new CliOption("sourceFolder", "source folder for generated code"));
supportedLibraries.put("", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2"); supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6"); supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
cliOptions.add(buildLibraryCliOption(supportedLibraries));
} }
public CodegenType getTag() { public CodegenType getTag() {