Swagr code-gen: Split config options into gen related config and env related

This commit is contained in:
Deepak Michael 2011-07-23 08:22:39 +05:30
parent 5ec7c8b40e
commit 643971c011
6 changed files with 80 additions and 44 deletions

View File

@ -4,6 +4,7 @@ import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.wordnik.codegen.config.CodeGenConfig;
import com.wordnik.codegen.config.GenerationEnvironmentConfig;
import com.wordnik.codegen.resource.*;
import com.wordnik.exception.CodeGenerationException;
@ -37,6 +38,7 @@ public class DriverCodeGenerator {
private static final String API_LISTING_URL = "apiListResource";
private CodeGenConfig config = null;
private GenerationEnvironmentConfig envConfig = null;
private String baseUrl;
private String apiKey;
private String apiListResource;
@ -49,6 +51,14 @@ public class DriverCodeGenerator {
this.config = config;
}
public GenerationEnvironmentConfig getEnvConfig() {
return envConfig;
}
public void setEnvConfig(GenerationEnvironmentConfig config) {
this.envConfig = config;
}
/**
* Generate classes needed for the model and API invocation
*/
@ -56,7 +66,7 @@ public class DriverCodeGenerator {
readApiConfig();
//read resources and get their documentation
List<Resource> resources = this.readResourceDocumentation(baseUrl);
StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",config.getTemplateLocation());
StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",envConfig.getTemplateLocation());
if(resources.size() > 0) {
generateVersionHelper(resources.get(0).getApiVersion(), aTemplateGroup);
}
@ -208,7 +218,7 @@ public class DriverCodeGenerator {
private void generateVersionHelper(String version, StringTemplateGroup templateGroup) {
StringTemplate template = templateGroup.getInstanceOf(VERSION_OBJECT_TEMPLATE);
template.setAttribute("apiVersion", version);
File aFile = new File(config.getResourceClassLocation() + config.getNameGenerator().getVersionCheckerClassName()
File aFile = new File(envConfig.getResourceClassLocation() + config.getNameGenerator().getVersionCheckerClassName()
+ config.getClassFileExtension());
writeFile(aFile, template.toString(), "Version checker class");
}
@ -236,7 +246,7 @@ public class DriverCodeGenerator {
template.setAttribute("imports", imports);
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
template.setAttribute("className", model.getGenratedClassName());
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
writeFile(aFile, template.toString(), "Model class");
generatedClassNames.add(model.getName());
}
@ -278,7 +288,7 @@ public class DriverCodeGenerator {
template.setAttribute("imports", imports);
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
template.setAttribute("className", model.getGenratedClassName());
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
writeFile(aFile, template.toString(), "Input model class");
generatedClasses.add(model.getName());
}
@ -316,7 +326,7 @@ public class DriverCodeGenerator {
template.setAttribute("methods", filteredMethods);
template.setAttribute("extends", config.getCodeGenOverridingRules().getServiceExtendingClass(className));
File aFile = new File(config.getResourceClassLocation()+ resource.generateClassName(config) +config.getClassFileExtension());
File aFile = new File(envConfig.getResourceClassLocation()+ resource.generateClassName(config) +config.getClassFileExtension());
writeFile(aFile, template.toString(), "API CLasses");
}
}
@ -359,7 +369,7 @@ public class DriverCodeGenerator {
template.setAttribute("imports", imports);
template.setAttribute("extends", config.getCodeGenOverridingRules().getModelExtendingClass());
template.setAttribute("className", model.getGenratedClassName());
File aFile = new File(config.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
File aFile = new File(envConfig.getModelClassLocation()+model.getGenratedClassName()+config.getClassFileExtension());
writeFile(aFile, template.toString(), "Wrapper class for test data file");
}

View File

@ -11,41 +11,28 @@ import java.util.List;
*/
public class CodeGenConfig {
private String templateLocation; //lang config
private String classFileExtension; //lang config
private String modelClassLocation; //output config
private String resourceClassLocation; //output config
private String classFileExtension;
/**
* Default model imports that we need to include in all service classes. This is needed because some times,
* we may need to write custom classes and those classes will not be known to code generation. To import those
* classes in service classes we use this property
*/
private List<String> defaultModelImports; //code gen helper config
private List<String> defaultModelImports;
/**
* Default service imports that we need to include in all service classes. This is needed because some times,
* we may need to write custom classes ans those classes will not be known to code generation. To import those
* classes in service classes we use this property
*/
private List<String> defaultServiceImports; //code gen helper config
private List<String> defaultServiceImports;
private CodeGenOverridingRules codeGenOverridingRules; //code gen helper config
private CodeGenOverridingRules codeGenOverridingRules;
private DataTypeMapper dataTypeMapper; //code gen helper
private DataTypeMapper dataTypeMapper;
private ServiceAndMethodNameGenerator nameGenerator; //code gen helper
public String getTemplateLocation() {
return templateLocation;
}
public void setTemplateLocation(String templateLocation) {
this.templateLocation = templateLocation;
}
private ServiceAndMethodNameGenerator nameGenerator;
public String getClassFileExtension() {
return classFileExtension;
@ -55,22 +42,6 @@ public class CodeGenConfig {
this.classFileExtension = classFileExtension;
}
public String getModelClassLocation() {
return modelClassLocation;
}
public void setModelClassLocation(String modelClassLocation) {
this.modelClassLocation = modelClassLocation;
}
public String getResourceClassLocation() {
return resourceClassLocation;
}
public void setResourceClassLocation(String resourceClassLocation) {
this.resourceClassLocation = resourceClassLocation;
}
public List<String> getDefaultModelImports() {
return defaultModelImports;
}

View File

@ -0,0 +1,40 @@
package com.wordnik.codegen.config;
/**
* User: deepakmichael
* Date: 23/07/11
* Time: 8:01 AM
*/
public class GenerationEnvironmentConfig {
private String templateLocation; //lang config
private String modelClassLocation; //output config
private String resourceClassLocation; //output config
public String getTemplateLocation() {
return templateLocation;
}
public void setTemplateLocation(String templateLocation) {
this.templateLocation = templateLocation;
}
public String getModelClassLocation() {
return modelClassLocation;
}
public void setModelClassLocation(String modelClassLocation) {
this.modelClassLocation = modelClassLocation;
}
public String getResourceClassLocation() {
return resourceClassLocation;
}
public void setResourceClassLocation(String resourceClassLocation) {
this.resourceClassLocation = resourceClassLocation;
}
}

View File

@ -14,9 +14,6 @@ public class JavaCodeGenConfig extends CodeGenConfig {
public JavaCodeGenConfig() {
this.setClassFileExtension(".java");
this.setModelClassLocation("../java/src/main/java/com/wordnik/model/");
this.setResourceClassLocation("../java/src/main/java/com/wordnik/api/");
this.setTemplateLocation("conf/templates/java");
List<String> defaultModelImports = new ArrayList<String>();
defaultModelImports.add("com.wordnik.common.WordListType");
defaultModelImports.add("com.wordnik.common.StringValue");

View File

@ -0,0 +1,17 @@
package com.wordnik.codegen.java;
import com.wordnik.codegen.config.GenerationEnvironmentConfig;
/**
* User: deepakmichael
* Date: 23/07/11
* Time: 8:09 AM
*/
public class JavaGenEnvConfig extends GenerationEnvironmentConfig{
public JavaGenEnvConfig() {
this.setModelClassLocation("../java/src/main/java/com/wordnik/model/");
this.setResourceClassLocation("../java/src/main/java/com/wordnik/api/");
this.setTemplateLocation("conf/templates/java");
}
}

View File

@ -16,5 +16,6 @@ public class JavaLibCodeGen extends DriverCodeGenerator {
public JavaLibCodeGen(){
this.setConfig(new JavaCodeGenConfig());
this.setEnvConfig(new JavaGenEnvConfig());
}
}