Add command to display all library templates supported

for a specific language
This commit is contained in:
xhh 2015-08-05 16:37:08 +08:00
parent 4ba521a5c0
commit 07f10a8abc
8 changed files with 98 additions and 4 deletions

View File

@ -116,6 +116,10 @@ OPTIONS
client language to generate (maybe class name in classpath,
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>
where to write the generated files (current dir by default)
@ -128,7 +132,7 @@ OPTIONS
-s , --skip-overwrite
specifies if the existing files should be overwritten during
the generation
```
```
You can then compile and run the client, as well as unit tests against it:
@ -144,6 +148,23 @@ Other languages have petstore samples, too:
./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
It's just as easy--just use the `-i` flag to point to either a server or file.

View File

@ -5,6 +5,7 @@ import io.airlift.airline.Help;
import io.swagger.codegen.cmd.ConfigHelp;
import io.swagger.codegen.cmd.Generate;
import io.swagger.codegen.cmd.Langs;
import io.swagger.codegen.cmd.LibraryHelp;
import io.swagger.codegen.cmd.Meta;
/**
@ -29,7 +30,8 @@ public class SwaggerCodegen {
Meta.class,
Langs.class,
Help.class,
ConfigHelp.class
ConfigHelp.class,
LibraryHelp.class
);
builder.build().parse(args).run();

View File

@ -41,7 +41,8 @@ public class Generate implements Runnable {
private String lang;
@Option(name = {"-L", "--library"}, title = "library",
description = "library template (sub-template) to use")
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",

View File

@ -0,0 +1,55 @@
package io.swagger.codegen.cmd;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import java.util.ServiceLoader;
import static java.util.ServiceLoader.load;
@Command(name = "library-help", description = "Library help for chosen lang")
public class LibraryHelp implements Runnable {
@Option(name = {"-l", "--lang"}, title = "language", required = true,
description = "language to get library help for")
private String lang;
/**
* Tries to load config class with SPI first, then with class name directly from classpath
*
* @param name name of config, or full qualified class name in classpath
* @return config class
*/
private static CodegenConfig forName(String name) {
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
for (CodegenConfig config : loader) {
if (config.getName().equals(name)) {
return config;
}
}
// else try to load directly
try {
return (CodegenConfig) Class.forName(name).newInstance();
} catch (Exception e) {
throw new RuntimeException("Can't load config class with name ".concat(name), e);
}
}
@Override
public void run() {
System.out.println();
CodegenConfig config = forName(lang);
System.out.println("LIBRARY OPTIONS");
for (String library : config.supportedLibraries().keySet()) {
String description = config.supportedLibraries().get(library);
if ("".equals(library))
library = "<default>";
System.out.println("\t" + library);
System.out.println("\t " + description);
System.out.println();
}
}
}

View File

@ -109,6 +109,8 @@ public interface CodegenConfig {
void setSkipOverwrite(boolean skipOverwrite);
Map<String, String> supportedLibraries();
void setLibrary(String library);
/**

View File

@ -81,6 +81,7 @@ public class DefaultCodegen {
protected List<CliOption> cliOptions = new ArrayList<CliOption>();
protected boolean skipOverwrite;
protected boolean supportsInheritance = false;
protected Map<String, String> supportedLibraries = new HashMap<String, String>();
protected String library = null;
public List<CliOption> cliOptions() {
@ -1389,7 +1390,16 @@ public class DefaultCodegen {
this.skipOverwrite = skipOverwrite;
}
/**
* All library templates supported.
*/
public Map<String, String> supportedLibraries() {
return supportedLibraries;
}
public void setLibrary(String library) {
if (library != null && !supportedLibraries.containsKey(library))
throw new RuntimeException("unknown library: " + library);
this.library = library;
}

View File

@ -266,7 +266,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
String templateFile = null;
String library = config.getLibrary();
if (library != null) {
if (library != null && !"".equals(library)) {
String libTemplateFile = config.templateDir() + File.separator +
"libraries" + File.separator + library + File.separator +
support.templateFile;

View File

@ -61,6 +61,9 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
cliOptions.add(new CliOption("artifactId", "artifactId 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"));
supportedLibraries.put("", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
}
public CodegenType getTag() {