提交 080097a9 编写于 作者: 黄勇

【I】代码重构

上级 3e8d0e71
......@@ -13,6 +13,8 @@ public abstract class Command {
private static final Logger logger = Logger.getLogger(Command.class);
private static Properties config;
protected String smartHome;
public final boolean exec(String... params) {
......@@ -73,8 +75,10 @@ public abstract class Command {
private String getConfigProperty(String appPath, String property) {
String appName;
try {
Properties config = new Properties();
config.load(new FileInputStream(appPath + "/src/main/resources/config.properties"));
if (config == null) {
config = new Properties();
config.load(new FileInputStream(appPath + "/src/main/resources/config.properties"));
}
appName = config.getProperty(property);
} catch (FileNotFoundException e) {
throw new RuntimeException("无法找到 config.properties 文件!");
......
......@@ -24,22 +24,18 @@ public class CreateActionCommand extends Command {
@Override
public void generateFiles() {
generateAction();
}
private void generateAction() {
String appPackage = getAppPackage(appPath);
String packageName = appPackage.replace('.', '/');
String actionNameCamelhump = StringUtil.toCamelhumpStyle(actionName);
String actionNameUnderline = StringUtil.toUnderlineStyle(actionName);
String actionNamePascal = StringUtil.toPascalStyle(actionName, "-");
String actionNameUnderline = StringUtil.toUnderlineStyle(actionName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_package", appPackage);
dataMap.put("action_name_c", actionNameCamelhump);
dataMap.put("action_name_p", actionNamePascal);
dataMap.put("action_name_u", actionNameUnderline);
String vmPath = "create-action/action.java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/action/" + actionNameCamelhump + "Action.java";
String vmPath = "create-action/action_java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/action/" + actionNamePascal + "Action.java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
}
......@@ -67,7 +67,7 @@ public class CreateAppCommand extends Command {
dataMap.put("app_name", appName);
dataMap.put("app_group", appGroup);
String pomVMPath = "create-app/pom.xml.vm";
String pomVMPath = "create-app/pom_xml.vm";
String pomFilePath = appPath + "/pom.xml";
VelocityUtil.mergeTemplateIntoFile(pomVMPath, dataMap, pomFilePath);
}
......@@ -85,7 +85,7 @@ public class CreateAppCommand extends Command {
dataMap.put("app_package", appPackage);
dataMap.put("db_name", dbName);
String vmPath = "create-app/config.properties.vm";
String vmPath = "create-app/config_properties.vm";
String filePath = appPath + "/src/main/resources/config.properties";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
......@@ -96,7 +96,7 @@ public class CreateAppCommand extends Command {
dataMap.put("app_package", appPackage);
dataMap.put("db_name", dbName + "_test");
String vmPath = "create-app/config.properties.vm";
String vmPath = "create-app/config_properties.vm";
String filePath = appPath + "/src/test/resources/config.properties";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
......@@ -106,18 +106,18 @@ public class CreateAppCommand extends Command {
dataMap.put("app_name", appName);
dataMap.put("app_package", appPackage);
String vmPath = "create-app/log4j.properties.vm";
String vmPath = "create-app/log4j_properties.vm";
String filePath = appPath + "/src/main/resources/log4j.properties";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
private void generateIndexFile() {
String pageNameDisplay = StringUtil.toDisplayStyle(appName);
String pageNameDisplay = StringUtil.toDisplayStyle(appName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("page_name_c", pageNameDisplay);
String vmPath = "create-app/index.html.vm";
String vmPath = "create-app/index_html.vm";
String filePath = appPath + "/src/main/webapp/www/page/index.html";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
......
package com.smart.generator.command.impl;
import com.smart.framework.util.StringUtil;
import com.smart.framework.util.VelocityUtil;
import com.smart.generator.command.Command;
import java.util.HashMap;
import java.util.Map;
public class CreateCRUDCommand extends Command {
......@@ -39,9 +43,19 @@ public class CreateCRUDCommand extends Command {
}
private void generateAction() {
Command command = new CreateActionCommand();
command.initVariables(appPath, crudName);
command.generateFiles();
String appPackage = getAppPackage(appPath);
String packageName = appPackage.replace('.', '/');
String actionNamePascal = StringUtil.toPascalStyle(crudName, "-");
String actionNameUnderline = StringUtil.toUnderlineStyle(crudName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_package", appPackage);
dataMap.put("action_name_p", actionNamePascal);
dataMap.put("action_name_u", actionNameUnderline);
String vmPath = "create-crud/action_java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/action/" + actionNamePascal + "Action.java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
private void generatePage() {
......
......@@ -24,20 +24,16 @@ public class CreateEntityCommand extends Command {
@Override
public void generateFiles() {
generateEntity();
}
private void generateEntity() {
String appPackage = getAppPackage(appPath);
String packageName = appPackage.replace('.', '/');
String entityNameCamelhump = StringUtil.toCamelhumpStyle(entityName);
String entityNamePascal = StringUtil.toPascalStyle(entityName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_package", appPackage);
dataMap.put("entity_name_c", entityNameCamelhump);
dataMap.put("entity_name_p", entityNamePascal);
String vmPath = "create-entity/entity.java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/entity/" + entityNameCamelhump + ".java";
String vmPath = "create-entity/entity_java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/entity/" + entityNamePascal + ".java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
}
......@@ -24,21 +24,17 @@ public class CreatePageCommand extends Command {
@Override
public void generateFiles() {
generatePage();
}
private void generatePage() {
String appName = getAppName(appPath);
String appNameDisplay = StringUtil.toDisplayStyle(appName);
String pageNameDisplay = StringUtil.toDisplayStyle(pageName);
String pageNameUnderline = StringUtil.toUnderlineStyle(pageName);
String appNameDisplay = StringUtil.toDisplayStyle(appName, "-");
String pageNameDisplay = StringUtil.toDisplayStyle(pageName, "-");
String pageNameUnderline = StringUtil.toUnderlineStyle(pageName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_name_d", appNameDisplay);
dataMap.put("page_name_d", pageNameDisplay);
String vmPath = "create-page/page.html.vm";
String vmPath = "create-page/page_html.vm";
String filePath = appPath + "/src/main/webapp/www/page/" + pageNameUnderline + ".html";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
......
......@@ -26,25 +26,25 @@ public class CreateServiceCommand extends Command {
public void generateFiles() {
String appPackage = getAppPackage(appPath);
String packageName = appPackage.replace('.', '/');
String serviceNameCamelhump = StringUtil.toCamelhumpStyle(serviceName);
String serviceNamePascal = StringUtil.toPascalStyle(serviceName, "-");
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_package", appPackage);
dataMap.put("service_name_c", serviceNameCamelhump);
dataMap.put("service_name_p", serviceNamePascal);
generateServiceInterface(packageName, serviceNameCamelhump, dataMap);
generateServiceImplement(packageName, serviceNameCamelhump, dataMap);
generateServiceInterface(packageName, serviceNamePascal, dataMap);
generateServiceImplement(packageName, serviceNamePascal, dataMap);
}
private void generateServiceInterface(String packageName, String serviceNameCamelhump, Map<String, Object> dataMap) {
String vmPath = "create-service/service.java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/service/" + serviceNameCamelhump + "Service.java";
private void generateServiceInterface(String packageName, String serviceNamePascal, Map<String, Object> dataMap) {
String vmPath = "create-service/service_java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/service/" + serviceNamePascal + "Service.java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
private void generateServiceImplement(String packageName, String serviceNameCamelhump, Map<String, Object> dataMap) {
String vmPath = "create-service/service.impl.java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/service/impl/" + serviceNameCamelhump + "ServiceImpl.java";
private void generateServiceImplement(String packageName, String serviceNamePascal, Map<String, Object> dataMap) {
String vmPath = "create-service/service_impl_java.vm";
String filePath = appPath + "/src/main/java/" + packageName + "/service/impl/" + serviceNamePascal + "ServiceImpl.java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
}
......@@ -56,18 +56,18 @@ public class EntityBuilder extends Builder {
for (Map.Entry<Table, List<Column>> entry : tableMap.entrySet()) {
Table table = entry.getKey();
String tableName = table.getName();
String entityName = StringUtil.toCamelhumpStyle(tableName);
String entityNamePascal = StringUtil.toPascalStyle(tableName, "-");
List<Column> columnList = entry.getValue();
List<Field> fieldList = transformFieldList(columnList);
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("app_package", packageName);
dataMap.put("entity_name_c", entityName);
dataMap.put("entity_name_p", entityNamePascal);
dataMap.put("field_list", fieldList);
dataMap.put("SU", new StringUtil());
String vmPath = "load-dict/entity.java.vm";
String filePath = outputPath + "/" + entityName + ".java";
String vmPath = "load-dict/entity_java.vm";
String filePath = outputPath + "/" + entityNamePascal + ".java";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
}
......
......@@ -22,7 +22,7 @@ public class SQLBuilder extends Builder {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("table_map", tableMap);
String vmPath = "load-dict/table.sql.vm";
String vmPath = "load-dict/table_sql.vm";
String filePath = outputPath + "/" + appName + ".sql";
VelocityUtil.mergeTemplateIntoFile(vmPath, dataMap, filePath);
}
......
package com.smart.generator.test;
import com.smart.framework.OrderedRunner;
import com.smart.framework.annotation.Order;
import com.smart.generator.command.Invoker;
import com.smart.generator.command.impl.CreateActionCommand;
import com.smart.generator.command.impl.CreateAppCommand;
......@@ -8,11 +10,15 @@ import com.smart.generator.command.impl.CreateEntityCommand;
import com.smart.generator.command.impl.CreatePageCommand;
import com.smart.generator.command.impl.CreateServiceCommand;
import com.smart.generator.command.impl.LoadDictCommand;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(OrderedRunner.class)
public class CommandTest {
@Test
@Order(1)
public void createAppCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreateAppCommand()); // smart create-app
......@@ -23,7 +29,6 @@ public class CommandTest {
"com.smart", // App Group
"com.smart.demo" // App Package = <App Group> + <App Artifact>
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
......@@ -31,86 +36,103 @@ public class CommandTest {
}
@Test
@Order(2)
public void createEntityCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreateEntityCommand()); // smart create-entity <entity-name>
String[] params = {
"C:\\Smart\\demo", // Current Path
"product", // Entity Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
String[] entityNameArray = {"product", "product-type"};
for (String entityName : entityNameArray) {
String[] params = {
"C:\\Smart\\demo", // Current Path
entityName, // Entity Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
}
}
}
@Test
@Order(3)
public void createServiceCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreateServiceCommand()); // smart create-service <service-name>
String[] params = {
"C:\\Smart\\demo", // Current Path
"product", // Service Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
String[] serviceNameArray = {"product", "product-type"};
for (String serviceName : serviceNameArray) {
String[] params = {
"C:\\Smart\\demo", // Current Path
serviceName, // Service Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
}
}
}
@Test
@Order(4)
public void createActionCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreateActionCommand()); // smart create-action <action-name>
String[] params = {
"C:\\Smart\\demo", // Current Path
"product", // Action Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
String[] actionNameArray = {"product", "product-type"};
for (String actionName : actionNameArray) {
String[] params = {
"C:\\Smart\\demo", // Current Path
actionName, // Action Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
}
}
}
@Test
@Order(5)
public void createPageCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreatePageCommand()); // smart create-page <page-name>
String[] params = {
"C:\\Smart\\demo", // Current Path
"product", // Page Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
String[] pageNameArray = {"product", "product-type"};
for (String pageName : pageNameArray) {
String[] params = {
"C:\\Smart\\demo", // Current Path
pageName, // Page Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
}
}
}
@Test
@Order(6)
public void createCRUDCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new CreateCRUDCommand()); // smart create-crud <crud-name>
String[] params = {
"C:\\Smart\\demo", // Current Path
"product", // Page Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
String[] crudNameArray = {"customer", "customer-type"};
for (String crudName : crudNameArray) {
String[] params = {
"C:\\Smart\\demo", // Current Path
crudName, // CRUD Name
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
}
}
}
@Ignore
@Test
@Order(7)
public void loadDictCommandTest() {
Invoker invoker = new Invoker();
invoker.setCommand(new LoadDictCommand()); // smart load-dict <dict-path>
......@@ -119,7 +141,6 @@ public class CommandTest {
"C:\\Smart\\demo", // Current Path
"C:\\Smart\\demo\\db.xls", // Dict Path
};
boolean result = invoker.execCommand(params);
if (!result) {
System.err.println("执行命令出错!");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册