Created
April 16, 2013 08:10
-
-
Save ralscha/5394271 to your computer and use it in GitHub Desktop.
ExtDirectSpring Command Line Implementation for the Model Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import org.springframework.beans.factory.config.BeanDefinition; | |
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | |
import org.springframework.core.type.filter.AnnotationTypeFilter; | |
import org.springframework.util.StringUtils; | |
import ch.ralscha.extdirectspring.util.ExtDirectSpringUtil; | |
import com.fasterxml.jackson.core.JsonGenerationException; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class ModelGeneratorCLI { | |
private final static ObjectMapper mapper = new ObjectMapper(); | |
static { | |
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); | |
} | |
public static void main(String[] args) throws ClassNotFoundException { | |
OutputConfig outputConfig = new OutputConfig(); | |
outputConfig.setIncludeValidation(IncludeValidation.NONE); | |
String baseDirectory = null; | |
boolean createBaseAndSubclass = false; | |
if (args.length >= 1) { | |
baseDirectory = args[0]; | |
} | |
if (args.length >= 2) { | |
// outputformat | |
String outputFormatString = args[1]; | |
outputConfig.setOutputFormat(OutputFormat.EXTJS4); | |
if (StringUtils.hasText(outputFormatString)) { | |
if (OutputFormat.TOUCH2.name().equalsIgnoreCase(outputFormatString)) { | |
outputConfig.setOutputFormat(OutputFormat.TOUCH2); | |
} | |
} | |
} | |
if (args.length >= 3) { | |
// debug | |
outputConfig.setDebug(!"false".equals(args[2])); | |
} | |
if (args.length >= 4) { | |
String includeValidationString = args[3]; | |
outputConfig.setIncludeValidation(IncludeValidation.NONE); | |
if (StringUtils.hasText(includeValidationString)) { | |
if (IncludeValidation.ALL.name().equalsIgnoreCase(includeValidationString)) { | |
outputConfig.setIncludeValidation(IncludeValidation.ALL); | |
} else if (IncludeValidation.BUILTIN.name().equalsIgnoreCase(includeValidationString)) { | |
outputConfig.setIncludeValidation(IncludeValidation.BUILTIN); | |
} | |
} | |
} | |
if (args.length >= 5) { | |
createBaseAndSubclass = "true".equals(args[4]); | |
} | |
if (args.length >= 6) { | |
outputConfig.setUseSingleQuotes("true".equals(args[5])); | |
} | |
if (args.length == 7) { | |
outputConfig.setSurroundApiWithQuotes("true".equals(args[6])); | |
} | |
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); | |
scanner.addIncludeFilter(new AnnotationTypeFilter(Model.class)); | |
for (BeanDefinition bd : scanner.findCandidateComponents("ch.ralscha.extdirectspring.generator")) { | |
System.out.println(bd.getBeanClassName()); | |
String qualifiedName = bd.getBeanClassName(); | |
Class<?> modelClass = Class.forName(qualifiedName); | |
String code = ModelGenerator.generateJavascript(modelClass, outputConfig); | |
Model modelAnnotation = modelClass.getAnnotation(Model.class); | |
String modelName = modelAnnotation.value(); | |
String fileName; | |
String packageName = ""; | |
if (StringUtils.hasText(modelName)) { | |
int lastDot = modelName.lastIndexOf('.'); | |
if (lastDot != -1) { | |
fileName = modelName.substring(lastDot + 1); | |
int firstDot = modelName.indexOf('.'); | |
if (firstDot < lastDot) { | |
packageName = modelName.substring(firstDot + 1, lastDot); | |
} | |
} else { | |
fileName = modelName; | |
} | |
} else { | |
fileName = modelClass.getSimpleName().toString(); | |
} | |
System.out.println(fileName); | |
System.out.println(qualifiedName); | |
if (createBaseAndSubclass) { | |
code = code.replaceFirst("(Ext.define\\(\"[^\"]+?)(\",)", "$1Base$2"); | |
write(code, baseDirectory, packageName, fileName + "Base"); | |
File checkFile = new File(new File(baseDirectory, packageName.replace(".", "/")), fileName + ".js"); | |
System.out.println(checkFile); | |
if (!checkFile.exists()) { | |
String subClassCode = generateSubclassCode(modelClass, outputConfig.isDebug()); | |
write(subClassCode, baseDirectory, packageName, fileName); | |
} | |
} else { | |
write(code, baseDirectory, packageName, fileName); | |
} | |
} | |
} | |
private static void write(String code, String baseDirectory, String packageName, String fileName) { | |
File outputDir = new File(baseDirectory, packageName.replace(".", "/")); | |
File outputFile = new File(outputDir, fileName + ".js"); | |
FileOutputStream fos = null; | |
try { | |
outputDir.mkdirs(); | |
fos = new FileOutputStream(outputFile); | |
fos.write(code.getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (fos != null) { | |
try { | |
fos.close(); | |
} catch (IOException e) { | |
// ignore | |
} | |
} | |
} | |
} | |
private static String generateSubclassCode(Class<?> clazz, boolean debug) { | |
Model modelAnnotation = clazz.getAnnotation(Model.class); | |
String name; | |
if (modelAnnotation != null && StringUtils.hasText(modelAnnotation.value())) { | |
name = modelAnnotation.value(); | |
} else { | |
name = clazz.getName(); | |
} | |
Map<String, Object> modelObject = new LinkedHashMap<String, Object>(); | |
modelObject.put("extend", name + "Base"); | |
StringBuilder sb = new StringBuilder(100); | |
sb.append("Ext.define(\"").append(name).append("\","); | |
if (debug) { | |
sb.append("\n"); | |
} | |
String configObjectString; | |
try { | |
if (debug) { | |
configObjectString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(modelObject); | |
} else { | |
configObjectString = mapper.writeValueAsString(modelObject); | |
} | |
} catch (JsonGenerationException e) { | |
throw new RuntimeException(e); | |
} catch (JsonMappingException e) { | |
throw new RuntimeException(e); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
sb.append(configObjectString); | |
sb.append(");"); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>exec-maven-plugin</artifactId> | |
<version>1.2.1</version> | |
<executions> | |
<execution> | |
<phase>generate-resources</phase> | |
<goals> | |
<goal>java</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<mainClass>ModelGeneratorCLI</mainClass> | |
<arguments> | |
<!-- base directory --> | |
<argument>c:/output</argument> | |
<!-- output format --> | |
<argument>extjs4</argument> | |
<!-- debug --> | |
<argument>true</argument> | |
<!-- includeValidation --> | |
<argument>NONE</argument> | |
<!-- createBaseAndSubclass --> | |
<argument>false</argument> | |
<!-- useSingleQuotes --> | |
<argument>false</argument> | |
<!-- surroundApiWithQuotes --> | |
<argument>true</argument> | |
</arguments> | |
</configuration> | |
</plugin> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment