forked from loafle/openapi-generator-original
Swagr codegen: Moving file tasks from ant to java after reading api configuration from json
This commit is contained in:
parent
ed6e09c52f
commit
3e0ca55e5d
23
build.xml
23
build.xml
@ -19,29 +19,19 @@
|
|||||||
|
|
||||||
<condition property="outputPath.set">
|
<condition property="outputPath.set">
|
||||||
<and>
|
<and>
|
||||||
<isset property="outputDir"/>
|
<isset property="apiConfiguration"/>
|
||||||
<isset property="apiServerRootDir"/>
|
|
||||||
</and>
|
</and>
|
||||||
</condition>
|
</condition>
|
||||||
|
|
||||||
<!-- generates the classes -->
|
<!-- generates the classes -->
|
||||||
<target name="generate-java" depends="compile" description="generates APIs and model classes for java language">
|
<target name="generate-java" depends="compile" description="generates APIs and model classes for java language">
|
||||||
<fail unless="outputPath.set">
|
<fail unless="outputPath.set">
|
||||||
Must specify the parameters: outputPath and apiServerRootDir
|
Must specify the parameter for apiConfiguration
|
||||||
eg. -DoutputDir=../api-server-lib/java/src/main/java/com/wordnik/swagger -DapiServerRootDir=../api-server-lib/java/
|
eg. -DapiConfiguration==../api-server-lib/java/config/apiConfiguration.json
|
||||||
These are the output path of the Apis and the root directory that will be created for the api server (directory where structure is created)
|
|
||||||
</fail>
|
</fail>
|
||||||
<mkdir dir="${outputDir}/api"/>
|
|
||||||
<mkdir dir="${outputDir}/model"/>
|
|
||||||
<echo>
|
<echo>
|
||||||
outputDir for Api = ${outputDir}
|
apiConfiguration to be used : ${apiConfiguration}
|
||||||
</echo>
|
</echo>
|
||||||
<echo>
|
|
||||||
apiServerRootDir = ${apiServerRootDir}
|
|
||||||
</echo>
|
|
||||||
<delete>
|
|
||||||
<fileset dir="${outputDir}" includes="*.java"/>
|
|
||||||
</delete>
|
|
||||||
<java classname="com.wordnik.swagger.codegen.config.java.JavaLibCodeGen">
|
<java classname="com.wordnik.swagger.codegen.config.java.JavaLibCodeGen">
|
||||||
<classpath>
|
<classpath>
|
||||||
<pathelement location="build/main/java" />
|
<pathelement location="build/main/java" />
|
||||||
@ -49,11 +39,8 @@
|
|||||||
<include name="**/*.jar"/>
|
<include name="**/*.jar"/>
|
||||||
</fileset>
|
</fileset>
|
||||||
</classpath>
|
</classpath>
|
||||||
<arg value="${apiServerRootDir}/config/apiConfiguration.json"/>
|
<arg value="${apiConfiguration}"/>
|
||||||
</java>
|
</java>
|
||||||
<copy todir="${apiServerRootDir}" overwrite="true">
|
|
||||||
<fileset dir="conf/java/structure"/>
|
|
||||||
</copy>
|
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -13,6 +13,10 @@ public class LanguageConfiguration {
|
|||||||
|
|
||||||
private String templateLocation;
|
private String templateLocation;
|
||||||
|
|
||||||
|
private String structureLocation;
|
||||||
|
|
||||||
|
private String apiServerRootLocation;
|
||||||
|
|
||||||
private String modelClassLocation;
|
private String modelClassLocation;
|
||||||
|
|
||||||
private String resourceClassLocation;
|
private String resourceClassLocation;
|
||||||
@ -74,5 +78,19 @@ public class LanguageConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getStructureLocation() {
|
||||||
|
return structureLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStructureLocation(String structureLocation) {
|
||||||
|
this.structureLocation = structureLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApiServerRootLocation() {
|
||||||
|
return apiServerRootLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiServerRootLocation(String apiServerRootLocation) {
|
||||||
|
this.apiServerRootLocation = apiServerRootLocation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,7 @@ public class JavaCodeGenRulesProvider implements RulesProvider {
|
|||||||
private List<String> ignoreModels = new ArrayList<String>();
|
private List<String> ignoreModels = new ArrayList<String>();
|
||||||
|
|
||||||
public JavaCodeGenRulesProvider() {
|
public JavaCodeGenRulesProvider() {
|
||||||
ignoreMethods.add("WordAPI.getWordFrequency");
|
|
||||||
ignoreMethods.add("WordAPI.getAudio");
|
|
||||||
ignoreMethods.add("WordAPI.getWordStats");
|
|
||||||
ignoreModels.add("wordStats");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMethodIgnored(String serviceName, String methodName){
|
public boolean isMethodIgnored(String serviceName, String methodName){
|
||||||
|
@ -4,11 +4,13 @@ import com.wordnik.swagger.codegen.LibraryCodeGenerator;
|
|||||||
import com.wordnik.swagger.codegen.config.ApiConfiguration;
|
import com.wordnik.swagger.codegen.config.ApiConfiguration;
|
||||||
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
|
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
|
||||||
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
|
import com.wordnik.swagger.codegen.config.common.CamelCaseNamingPolicyProvider;
|
||||||
|
import com.wordnik.swagger.codegen.util.FileUtil;
|
||||||
import com.wordnik.swagger.exception.CodeGenerationException;
|
import com.wordnik.swagger.exception.CodeGenerationException;
|
||||||
import org.codehaus.jackson.map.DeserializationConfig;
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,18 +34,20 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
|
|||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
this.setApiConfig(readApiConfiguration(configPath, mapper));
|
|
||||||
this.setCodeGenRulesProvider(readRulesProviderConfig(configPath, mapper));
|
final File configFile = new File(configPath);
|
||||||
this.setLanguageConfig( initializeLangConfig(readLanguageConfiguration(configPath, mapper)) );
|
this.setApiConfig(readApiConfiguration(configPath, mapper, configFile));
|
||||||
|
this.setCodeGenRulesProvider(readRulesProviderConfig(configPath, mapper, configFile));
|
||||||
|
this.setLanguageConfig( initializeLangConfig(readLanguageConfiguration(configPath, mapper, configFile)) );
|
||||||
|
|
||||||
this.setDataTypeMappingProvider(new JavaDataTypeMappingProvider());
|
this.setDataTypeMappingProvider(new JavaDataTypeMappingProvider());
|
||||||
this.setNameGenerator(new CamelCaseNamingPolicyProvider());
|
this.setNameGenerator(new CamelCaseNamingPolicyProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper) {
|
private JavaCodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper, File configFile) {
|
||||||
JavaCodeGenRulesProvider javaCodeGenRules = null;
|
JavaCodeGenRulesProvider javaCodeGenRules = null;
|
||||||
try {
|
try {
|
||||||
javaCodeGenRules = mapper.readValue(new File(rulesProviderLocation), JavaCodeGenRulesProvider.class);
|
javaCodeGenRules = mapper.readValue(configFile, JavaCodeGenRulesProvider.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation);
|
throw new CodeGenerationException("Java codegen rules configuration could not be read from the location : " + rulesProviderLocation);
|
||||||
}
|
}
|
||||||
@ -51,10 +55,10 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
|
|||||||
return javaCodeGenRules;
|
return javaCodeGenRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiConfiguration readApiConfiguration(String apiConfigLocation, ObjectMapper mapper) {
|
private ApiConfiguration readApiConfiguration(String apiConfigLocation, ObjectMapper mapper, File configFile) {
|
||||||
ApiConfiguration configuration = null;
|
ApiConfiguration configuration = null;
|
||||||
try {
|
try {
|
||||||
configuration = mapper.readValue(new File(apiConfigLocation), ApiConfiguration.class);
|
configuration = mapper.readValue(configFile, ApiConfiguration.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation);
|
throw new CodeGenerationException("Api configuration could not be read from the location : " + apiConfigLocation);
|
||||||
}
|
}
|
||||||
@ -62,10 +66,10 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
|
|||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LanguageConfiguration readLanguageConfiguration(String langConfigLocation, ObjectMapper mapper) {
|
private LanguageConfiguration readLanguageConfiguration(String langConfigLocation, ObjectMapper mapper, File configFile) {
|
||||||
LanguageConfiguration langConfig = null;
|
LanguageConfiguration langConfig = null;
|
||||||
try {
|
try {
|
||||||
langConfig = mapper.readValue(new File(langConfigLocation), LanguageConfiguration.class);
|
langConfig = mapper.readValue(configFile, LanguageConfiguration.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CodeGenerationException("Language configuration value could not be read from the location : " + langConfigLocation);
|
throw new CodeGenerationException("Language configuration value could not be read from the location : " + langConfigLocation);
|
||||||
}
|
}
|
||||||
@ -76,8 +80,15 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
|
|||||||
private LanguageConfiguration initializeLangConfig(LanguageConfiguration javaConfiguration) {
|
private LanguageConfiguration initializeLangConfig(LanguageConfiguration javaConfiguration) {
|
||||||
javaConfiguration.setClassFileExtension(".java");
|
javaConfiguration.setClassFileExtension(".java");
|
||||||
javaConfiguration.setTemplateLocation("conf/java/templates");
|
javaConfiguration.setTemplateLocation("conf/java/templates");
|
||||||
|
javaConfiguration.setStructureLocation("conf/java/structure");
|
||||||
javaConfiguration.setExceptionPackageName("com.wordnik.swagger.exception");
|
javaConfiguration.setExceptionPackageName("com.wordnik.swagger.exception");
|
||||||
javaConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations");
|
javaConfiguration.setAnnotationPackageName("com.wordnik.swagger.annotations");
|
||||||
|
|
||||||
|
//create ouput directories
|
||||||
|
FileUtil.createOutputDirectories(javaConfiguration.getModelClassLocation(), javaConfiguration.getClassFileExtension());
|
||||||
|
FileUtil.createOutputDirectories(javaConfiguration.getResourceClassLocation(), javaConfiguration.getClassFileExtension());
|
||||||
|
|
||||||
|
FileUtil.copyDirectory(new File(javaConfiguration.getStructureLocation()), new File(javaConfiguration.getApiServerRootLocation()));
|
||||||
return javaConfiguration;
|
return javaConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
76
src/main/java/com/wordnik/swagger/codegen/util/FileUtil.java
Normal file
76
src/main/java/com/wordnik/swagger/codegen/util/FileUtil.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package com.wordnik.swagger.codegen.util;
|
||||||
|
|
||||||
|
import com.wordnik.swagger.exception.CodeGenerationException;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: deepakmichael
|
||||||
|
* Date: 03/08/11
|
||||||
|
* Time: 12:02 AM
|
||||||
|
*/
|
||||||
|
public class FileUtil {
|
||||||
|
|
||||||
|
public static void createOutputDirectories(String classLocation, String fileExtension) {
|
||||||
|
File outputLocation = new File(classLocation);
|
||||||
|
outputLocation.mkdirs(); //make folder if necessary
|
||||||
|
//clear contents
|
||||||
|
clearFolder(classLocation, fileExtension);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean deleteFile(String sFilePath) {
|
||||||
|
File oFile = new File(sFilePath);
|
||||||
|
if (!oFile.exists()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return oFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clears the folder of the files with extension
|
||||||
|
public static void clearFolder(String strFolder, final String strExt) {
|
||||||
|
File fLogDir = new File(strFolder);
|
||||||
|
File[] fLogs = fLogDir.listFiles(new FilenameFilter() {
|
||||||
|
public boolean accept(File fDir, String strName) {
|
||||||
|
return (strName.endsWith(strExt));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (int i = 0; i < fLogs.length; i++) {
|
||||||
|
deleteFile(fLogs[i].getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copyDirectory(File srcPath, File dstPath) {
|
||||||
|
if (srcPath.isDirectory()) {
|
||||||
|
if (!dstPath.exists()) {
|
||||||
|
dstPath.mkdir();
|
||||||
|
}
|
||||||
|
|
||||||
|
String files[] = srcPath.list();
|
||||||
|
for (int i = 0; i < files.length; i++) {
|
||||||
|
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!srcPath.exists()) {
|
||||||
|
throw new CodeGenerationException("Source folder does not exist");
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
InputStream in = new FileInputStream(srcPath);
|
||||||
|
OutputStream out = new FileOutputStream(dstPath);
|
||||||
|
|
||||||
|
// Transfer bytes from in to out
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = in.read(buf)) > 0) {
|
||||||
|
out.write(buf, 0, len);
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CodeGenerationException("Copy directory operation failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user