forked from loafle/openapi-generator-original
added help, dynamic loading
This commit is contained in:
@@ -68,6 +68,18 @@
|
||||
<directory>target</directory>
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.wordnik.swagger.codegen.Codegen</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
@@ -343,7 +355,7 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-legacy-spec-parser</artifactId>
|
||||
<version>${swagger-parser-version}</version>
|
||||
<scope>compile</scope>
|
||||
|
||||
@@ -87,8 +87,8 @@ public class PetApi {
|
||||
}
|
||||
|
||||
|
||||
public void addPet (Pet pet) throws ApiException {
|
||||
Object postBody = pet;
|
||||
public void addPet (Pet body) throws ApiException {
|
||||
Object postBody = body;
|
||||
|
||||
|
||||
// create path and map variables
|
||||
|
||||
@@ -2,17 +2,30 @@ package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.codegen.languages.*;
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
import com.wordnik.swagger.parser.SwaggerParser;
|
||||
import io.swagger.parser.SwaggerParser;
|
||||
import com.wordnik.swagger.util.*;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class Codegen extends DefaultGenerator {
|
||||
public static void main(String[] args) {
|
||||
List<CodegenConfig> extensions = getExtensions();
|
||||
Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(CodegenConfig config : extensions) {
|
||||
if(sb.toString().length() != 0)
|
||||
sb.append(", ");
|
||||
sb.append(config.getName());
|
||||
configs.put(config.getName(), config);
|
||||
}
|
||||
|
||||
Options options = new Options();
|
||||
options.addOption("l", "lang", true, "client language to generate");
|
||||
options.addOption("h", "help", false, "shows this message");
|
||||
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + sb.toString() + "]");
|
||||
options.addOption("o", "output", true, "where to write the generated files");
|
||||
options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file");
|
||||
options.addOption("t", "template-dir", true, "folder containing the template files");
|
||||
@@ -24,23 +37,32 @@ public class Codegen extends DefaultGenerator {
|
||||
CommandLine cmd = null;
|
||||
try {
|
||||
CommandLineParser parser = new BasicParser();
|
||||
|
||||
cmd = parser.parse(options, args);
|
||||
if (cmd.hasOption("l"))
|
||||
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l")));
|
||||
|
||||
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l"), configs));
|
||||
if (cmd.hasOption("o"))
|
||||
clientOptInput.getConfig().setOutputDir(cmd.getOptionValue("o"));
|
||||
if (cmd.hasOption("i")) {
|
||||
swagger = new SwaggerParser().read(cmd.getOptionValue("i"));
|
||||
}
|
||||
if (cmd.hasOption("t")) {
|
||||
clientOpts.getProperties().put("templateDir",
|
||||
String.valueOf(cmd.getOptionValue("t")));
|
||||
}
|
||||
if (cmd.hasOption("h")) {
|
||||
if(cmd.hasOption("l")) {
|
||||
CodegenConfig config = getConfig(String.valueOf(cmd.getOptionValue("l")), configs);
|
||||
if(config != null) {
|
||||
options.addOption("h", "help", true, config.getHelp());
|
||||
usage(options);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// cmd = parser.parse(options, args);
|
||||
options.addOption("h", "help", true, "config.getHelp()");
|
||||
}
|
||||
usage(options);
|
||||
return;
|
||||
}
|
||||
if (cmd.hasOption("i"))
|
||||
swagger = new SwaggerParser().read(cmd.getOptionValue("i"));
|
||||
if (cmd.hasOption("t"))
|
||||
clientOpts.getProperties().put("templateDir", String.valueOf(cmd.getOptionValue("t")));
|
||||
}
|
||||
catch (Exception e) {
|
||||
usage(options);
|
||||
@@ -57,31 +79,27 @@ public class Codegen extends DefaultGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<CodegenConfig> getExtensions() {
|
||||
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
|
||||
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
|
||||
Iterator<CodegenConfig> itr = loader.iterator();
|
||||
while(itr.hasNext()) {
|
||||
output.add(itr.next());
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
static void usage(Options options) {
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
formatter.printHelp( "Codegen", options );
|
||||
}
|
||||
|
||||
static CodegenConfig getConfig(String name) {
|
||||
if("objc".equals(name))
|
||||
return new ObjcClientCodegen();
|
||||
else if("android".equals(name))
|
||||
return new AndroidClientCodegen();
|
||||
else if("java".equals(name))
|
||||
return new JavaClientCodegen();
|
||||
else if("jaxrs".equals(name))
|
||||
return new JaxRSServerCodegen();
|
||||
else if("nodejs".equals(name))
|
||||
return new NodeJSServerCodegen();
|
||||
else if("scalatra".equals(name))
|
||||
return new ScalatraServerCodegen();
|
||||
else if("html".equals(name))
|
||||
return new StaticHtmlGenerator();
|
||||
else if("swagger".equals(name))
|
||||
return new SwaggerGenerator();
|
||||
else if("tizen".equals(name))
|
||||
return new TizenClientCodegen();
|
||||
else if(name.indexOf(".") > 0) {
|
||||
static CodegenConfig getConfig(String name, Map<String, CodegenConfig> configs) {
|
||||
if(configs.containsKey(name)) {
|
||||
return configs.get(name);
|
||||
}
|
||||
else {
|
||||
// see if it's a class
|
||||
try {
|
||||
System.out.println("loading class " + name);
|
||||
@@ -93,7 +111,5 @@ public class Codegen extends DefaultGenerator {
|
||||
throw new RuntimeException("can't load class " + name);
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new RuntimeException("unsupported client type");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.wordnik.swagger.models.properties.*;
|
||||
import java.util.*;
|
||||
|
||||
public interface CodegenConfig {
|
||||
String getName();
|
||||
String getHelp();
|
||||
Map<String, Object> additionalProperties();
|
||||
String apiPackage();
|
||||
String apiFileFolder();
|
||||
|
||||
@@ -13,6 +13,14 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "src/main/java";
|
||||
|
||||
public String getName() {
|
||||
return "android";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates an Android client library.";
|
||||
}
|
||||
|
||||
public AndroidClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/android";
|
||||
|
||||
@@ -13,6 +13,14 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "src/main/java";
|
||||
|
||||
public String getName() {
|
||||
return "java";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Java client library.";
|
||||
}
|
||||
|
||||
public JavaClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/java";
|
||||
|
||||
@@ -17,6 +17,14 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
||||
protected String sourceFolder = "src/main/java";
|
||||
protected String title = "Swagger Server";
|
||||
|
||||
public String getName() {
|
||||
return "jaxrs";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Java JAXRS Server application.";
|
||||
}
|
||||
|
||||
public JaxRSServerCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/javaJaxRS";
|
||||
|
||||
@@ -12,6 +12,14 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
|
||||
protected String artifactId = "swagger-client";
|
||||
protected String artifactVersion = "1.0.0";
|
||||
|
||||
public String getName() {
|
||||
return "nodejs";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a node.js server application compatible with the 1.2 swagger specification.";
|
||||
}
|
||||
|
||||
public NodeJSServerCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/nodejs";
|
||||
|
||||
@@ -12,6 +12,14 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String sourceFolder = "client";
|
||||
protected static String PREFIX = "SWG";
|
||||
|
||||
public String getName() {
|
||||
return "objc";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates an Objective-C client library.";
|
||||
}
|
||||
|
||||
public ObjcClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/objc";
|
||||
|
||||
@@ -14,6 +14,14 @@ public class ScalatraServerCodegen extends DefaultCodegen implements CodegenConf
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "src/main/scala";
|
||||
|
||||
public String getName() {
|
||||
return "scalatra";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Scala server application with Scalatra.";
|
||||
}
|
||||
|
||||
public ScalatraServerCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/scalatra";
|
||||
|
||||
@@ -13,6 +13,14 @@ public class StaticDocCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "docs";
|
||||
|
||||
public String getName() {
|
||||
return "dynamic-html";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a dynamic HTML site.";
|
||||
}
|
||||
|
||||
public StaticDocCodegen() {
|
||||
super();
|
||||
outputFolder = "docs";
|
||||
|
||||
@@ -14,6 +14,14 @@ public class StaticHtmlGenerator extends DefaultCodegen implements CodegenConfig
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "src/main/scala";
|
||||
|
||||
public String getName() {
|
||||
return "html";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a static HTML file.";
|
||||
}
|
||||
|
||||
public StaticHtmlGenerator() {
|
||||
super();
|
||||
outputFolder = "docs";
|
||||
|
||||
@@ -9,6 +9,13 @@ import org.apache.commons.io.FileUtils;
|
||||
import java.io.File;
|
||||
|
||||
public class SwaggerGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
public String getName() {
|
||||
return "swagger";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Creates a static swagger.json file.";
|
||||
}
|
||||
|
||||
public SwaggerGenerator() {
|
||||
super();
|
||||
|
||||
@@ -13,6 +13,14 @@ public class TizenClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
protected static String PREFIX = "Sami";
|
||||
protected Map<String, String> namespaces = new HashMap<String, String>();
|
||||
|
||||
public String getName() {
|
||||
return "tizen";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Samsung Tizen C++ client library.";
|
||||
}
|
||||
|
||||
public TizenClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/tizen";
|
||||
|
||||
Reference in New Issue
Block a user