forked from loafle/openapi-generator-original
added cli parser, input opts
This commit is contained in:
parent
370c22cddb
commit
d5e62086c8
8
pom.xml
8
pom.xml
@ -382,7 +382,12 @@
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons-lang-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-cli</groupId>
|
||||
<artifactId>commons-cli</artifactId>
|
||||
<version>${commons-cli-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scalatest</groupId>
|
||||
<artifactId>scalatest_2.11</artifactId>
|
||||
@ -402,6 +407,7 @@
|
||||
<swagger-core-version>1.5.0-SNAPSHOT</swagger-core-version>
|
||||
<scala-test-version>2.1.4</scala-test-version>
|
||||
<commons-io-version>2.3</commons-io-version>
|
||||
<commons-cli-version>1.2</commons-cli-version>
|
||||
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright 2014 Reverb, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.codegen.ClientOpts;
|
||||
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
|
||||
public class ClientOptInput {
|
||||
private ClientOpts opts;
|
||||
private Swagger model;
|
||||
protected CodegenConfig config;
|
||||
|
||||
public ClientOptInput model(Swagger model) {
|
||||
this.setModel(model);
|
||||
return this;
|
||||
}
|
||||
public ClientOptInput opts(ClientOpts opts) {
|
||||
this.setOpts(opts);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CodegenConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
public void setConfig(CodegenConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void setOpts(ClientOpts opts) {
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
public ClientOpts getOpts() {
|
||||
return opts;
|
||||
}
|
||||
|
||||
public void setModel(Swagger model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Swagger getModel() {
|
||||
return model;
|
||||
}
|
||||
}
|
52
src/main/java/com/wordnik/swagger/codegen/ClientOpts.java
Normal file
52
src/main/java/com/wordnik/swagger/codegen/ClientOpts.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.codegen.auth.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ClientOpts {
|
||||
protected String uri;
|
||||
protected String target;
|
||||
protected AuthMethod auth;
|
||||
protected Map<String, String> properties = new HashMap<String, String>();
|
||||
protected String outputDirectory;
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
public void setProperties(Map<String, String> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String getOutputDirectory() {
|
||||
return outputDirectory;
|
||||
}
|
||||
public void setOutputDirectory(String outputDirectory) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("ClientOpts: {\n");
|
||||
sb.append(" uri: ").append(uri).append(",");
|
||||
sb.append(" auth: ").append(auth).append(",");
|
||||
sb.append(properties);
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -1,38 +1,59 @@
|
||||
package com.wordnik.swagger.codegen;
|
||||
|
||||
import com.wordnik.swagger.codegen.languages.*;
|
||||
import com.wordnik.swagger.codegen.languages.*;
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
import com.wordnik.swagger.util.Json;
|
||||
import com.wordnik.swagger.util.*;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Codegen extends DefaultGenerator {
|
||||
public static void main(String[] args) {
|
||||
CodegenConfig config = null;
|
||||
if(args != null && args.length > 0) {
|
||||
String lang = args[0];
|
||||
if("java".equals(lang)) {
|
||||
JavaClientCodegen javaConfig = new JavaClientCodegen();
|
||||
javaConfig.setTemplateDir("src/main/resources/Java");
|
||||
config = javaConfig;
|
||||
}
|
||||
else if ("objc".equals(lang)) {
|
||||
ObjcClientCodegen objcConfig = new ObjcClientCodegen();
|
||||
objcConfig.setTemplateDir("src/main/resources/objc");
|
||||
config = objcConfig;
|
||||
}
|
||||
Options options = new Options();
|
||||
options.addOption("l", "lang", true, "client language to generate");
|
||||
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");
|
||||
|
||||
ClientOptInput codegenInput = new ClientOptInput();
|
||||
ClientOpts clientArgs = new ClientOpts();
|
||||
Swagger swagger = null;
|
||||
|
||||
CommandLine cmd = null;
|
||||
try {
|
||||
CommandLineParser parser = new BasicParser();
|
||||
cmd = parser.parse(options, args);
|
||||
if (cmd.hasOption("l"))
|
||||
codegenInput.setConfig(getConfig(cmd.getOptionValue("l")));
|
||||
if (cmd.hasOption("o"))
|
||||
codegenInput.getConfig().setOutputDir(cmd.getOptionValue("o"));
|
||||
if (cmd.hasOption("i"))
|
||||
swagger = new SwaggerLoader().read(cmd.getOptionValue("i"));
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
Swagger swagger = (Swagger) Json.mapper()
|
||||
.readValue(new File("swagger.json"), Swagger.class);
|
||||
codegenInput
|
||||
.opts(clientArgs)
|
||||
.model(swagger);
|
||||
|
||||
new Codegen()
|
||||
.config(config)
|
||||
.generate(swagger);
|
||||
new Codegen().opts(codegenInput).generate();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static CodegenConfig getConfig(String name) {
|
||||
if("objc".equals(name))
|
||||
return new ObjcClientCodegen();
|
||||
else if("java".equals(name))
|
||||
return new JavaClientCodegen();
|
||||
else
|
||||
throw new RuntimeException("unsupported client type");
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ public interface CodegenConfig {
|
||||
String toModelName(String name);
|
||||
String toParamName(String name);
|
||||
String escapeReservedWord(String name);
|
||||
|
||||
String getTypeDeclaration(Property p);
|
||||
String getTypeDeclaration(String name);
|
||||
|
||||
@ -26,6 +25,9 @@ public interface CodegenConfig {
|
||||
|
||||
List<SupportingFile> supportingFiles();
|
||||
|
||||
void setOutputDir(String dir);
|
||||
String getOutputDir();
|
||||
|
||||
CodegenModel fromModel(String name, Model model);
|
||||
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation);
|
||||
Set<String> defaultIncludes();
|
||||
|
@ -71,6 +71,13 @@ public class DefaultCodegen {
|
||||
return outputFolder;
|
||||
}
|
||||
|
||||
public void setOutputDir(String dir) {
|
||||
this.outputFolder = dir;
|
||||
}
|
||||
public String getOutputDir() {
|
||||
return outputFolder();
|
||||
}
|
||||
|
||||
public void setTemplateDir(String templateDir) {
|
||||
this.templateDir = templateDir;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.wordnik.swagger.models.*;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
import com.wordnik.swagger.util.*;
|
||||
|
||||
import com.wordnik.swagger.codegen.languages.*;
|
||||
import com.samskivert.mustache.*;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@ -13,13 +14,23 @@ import java.io.*;
|
||||
|
||||
public class DefaultGenerator implements Generator {
|
||||
private CodegenConfig config;
|
||||
private ClientOptInput opts = null;
|
||||
private Swagger swagger = null;
|
||||
|
||||
public Generator opts(ClientOptInput opts) {
|
||||
this.opts = opts;
|
||||
|
||||
this.swagger = opts.getModel();
|
||||
ClientOpts clientOpts = opts.getOpts();
|
||||
this.config = opts.getConfig();
|
||||
|
||||
public Generator config(CodegenConfig config) {
|
||||
this.config = config;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void generate(Swagger swagger) {
|
||||
public void generate() {
|
||||
if(swagger == null || config == null) {
|
||||
throw new RuntimeException("missing swagger input or config!");
|
||||
}
|
||||
try {
|
||||
Map<String, Object> models = null;
|
||||
Map<String, Object> operations = null;
|
||||
|
@ -3,6 +3,6 @@ package com.wordnik.swagger.codegen;
|
||||
import com.wordnik.swagger.models.Swagger;
|
||||
|
||||
public interface Generator {
|
||||
Generator config(CodegenConfig config);
|
||||
void generate(Swagger swagger);
|
||||
Generator opts(ClientOptInput opts);
|
||||
void generate();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user