Skip to content

Commit

Permalink
add: 代码生成器增加vue版本支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Dec 8, 2018
1 parent 22d50c3 commit a333023
Show file tree
Hide file tree
Showing 13 changed files with 1,195 additions and 153 deletions.
2 changes: 1 addition & 1 deletion wk-app/wk-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>cn.wizzer.app</groupId>
<artifactId>wk-code-generator-nb</artifactId>
<version>5.0.7</version>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
2 changes: 1 addition & 1 deletion wk-code/wk-code-generator-nb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>cn.wizzer.app</groupId>
<artifactId>wk-code-generator-nb</artifactId>
<version>5.0.7</version>
<version>5.1.1</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<name>wk-code-generator-nb</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static void main(String[] args) throws Exception {
Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
add.setAccessible(true);
add.invoke(classLoader, programRootDir.toURI().toURL());
boolean vue = false;
String basePath = "";
String baseNbPath = "";
String baseComPath = "";
Expand All @@ -94,7 +95,7 @@ public static void main(String[] args) throws Exception {
String types[] = {"all"};
String pages[] = {"index", "add", "edit", "detail"};
Options options = new Options();
options.addOption("c", "config", true, "spring datasource config file(classpath)");
options.addOption("c", "config", true, "datasource config file(classpath)");
options.addOption("i", "include", true, "include table pattern");
options.addOption("x", "exclude", true, "exclude table pattern");
options.addOption("p", "package", true, "base package name,default:cn.wizzer.app");
Expand All @@ -114,6 +115,10 @@ public static void main(String[] args) throws Exception {
"views",
true,
"for generator pages,default:all pages,eg: -v index_detail will generate index.html and detail.html");
options.addOption("vue",
"vue",
true,
"for generator vue");
options.addOption("j", "project-path", true, "project directory, default is empty");
options.addOption("b", "base-path", true, "base directory, default is empty");
options.addOption("y", "common-path", true, "base common directory, default is empty");
Expand All @@ -126,6 +131,12 @@ public static void main(String[] args) throws Exception {
CommandLineParser parser = new GnuParser();
try {
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption("vue")) {
String _vue = commandLine.getOptionValue("vue", "false");
if ("true".equals(_vue)) {
vue = true;
}
}
if (commandLine.hasOption("c")) {
configPath = commandLine.getOptionValue("c");
}
Expand Down Expand Up @@ -228,7 +239,14 @@ public static void main(String[] args) throws Exception {
if (!hasLocale(types)) {
viewpath = "view_notlocale";
}
generateViews(projectPath + "/" + outputDir + "/src/main", force, viewpath, table, generator, pages);
if (vue) {
viewpath = "vue";
if (!hasLocale(types)) {
viewpath = "vue_notlocale";
}
generateViewsVue(projectPath + "/" + outputDir + "/src/main", force, viewpath, table, generator, pages);
} else
generateViews(projectPath + "/" + outputDir + "/src/main", force, viewpath, table, generator, pages);
} else if (type.equals("locale")) {
generateLocales(projectPath + "/" + outputDir + "/src/main", force, table, generator);
} else {
Expand All @@ -246,6 +264,9 @@ public static void main(String[] args) throws Exception {
}
String templatePath = "templet/" + type.toLowerCase() + ".vm";

if ("controller".equals(type) && vue) {
templatePath = "templet/controller_vue.vm";
}
String packagePath = packageName.replace('.', '/');
String className = table.getEntityClassName();

Expand Down Expand Up @@ -320,6 +341,21 @@ private static void generateViews(String outputDir, boolean force, String viewpa
}
}

private static void generateViewsVue(String outputDir, boolean force, String viewpath,
TableDescriptor table,
Generator generator,
String[] pages)
throws IOException {

String templatePath = "templet/" + viewpath + "/index.html.vm";
File file = new File(outputDir + "/resources/views/"
+ table.getViewBasePath()
+ "/index.html");
log.debug("generate html:" + file.getName());
generator.generate(null, templatePath, file, force);

}

private static void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Main [options] [all|entity|service|controller|view]", options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ${packageName};

import cn.wizzer.app.web.commons.slog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.PageUtil;
import cn.wizzer.app.web.commons.utils.StringUtil;
import ${table.EntityFullClassName};
import ${table.ServiceFullClassName};
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

#set($parameterId = '${args[0].id}')
#set($parameterId2 = "${req.getAttribute('id')}")
@IocBean
@At("${table.UriPrefix}")
public class ${table.ControllerClassName}{
private static final Log log = Logs.get();
@Inject
@Reference
private ${table.ServiceClassName} ${table.ServiceInstanceName};

@At("")
@Ok("beetl:/${table.ViewBasePath}/index.html")
@RequiresPermissions("${table.Permissions}")
public void index() {
}

@At("/data")
@Ok("json:full")
@RequiresPermissions("${table.Permissions}")
public Object data(@Param("searchName") String searchName, @Param("searchKeyword") String searchKeyword, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
Cnd cnd = Cnd.NEW();
if (!Strings.isBlank(searchName) && !Strings.isBlank(searchKeyword)) {
cnd.and(searchName, "like", "%" + searchKeyword + "%");
}
if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
}
return Result.success().addData(${table.ServiceInstanceName}.listPage(pageNumber, pageSize, cnd));
}


@At("/addDo")
@Ok("json")
@RequiresPermissions("${table.Permissions}.add")
@SLog(tag = "${table.Label}", msg = "$parameterId")
public Object addDo(@Param("..")${table.EntityClassName} ${table.EntityInstanceName}, HttpServletRequest req) {
try {
${table.EntityInstanceName}.setOpBy(StringUtil.getPlatformUid());
${table.ServiceInstanceName}.insert(${table.EntityInstanceName});
return Result.success();
} catch (Exception e) {
return Result.error();
}
}

@At("/edit/?")
@Ok("json")
@RequiresPermissions("${table.Permissions}")
public Object edit(${table.pkType} id,HttpServletRequest req) {
try {
return Result.success().addData(${table.ServiceInstanceName}.fetch(id));
} catch (Exception e) {
return Result.error();
}
}

@At("/editDo")
@Ok("json")
@RequiresPermissions("${table.Permissions}.edit")
@SLog(tag = "${table.Label}", msg = "$parameterId")
public Object editDo(@Param("..")${table.EntityClassName} ${table.EntityInstanceName}, HttpServletRequest req) {
try {
${table.EntityInstanceName}.setOpBy(StringUtil.getPlatformUid());
${table.EntityInstanceName}.setOpAt(Times.getTS());
${table.ServiceInstanceName}.updateIgnoreNull(${table.EntityInstanceName});
return Result.success();
} catch (Exception e) {
return Result.error();
}
}

@At({"/delete/?", "/delete"})
@Ok("json")
@RequiresPermissions("${table.Permissions}.delete")
@SLog(tag = "${table.Label}", msg = "$parameterId2")
public Object delete(${table.pkType} id, @Param("ids") ${table.pkType}[] ids, HttpServletRequest req) {
try {
if(ids!=null&&ids.length>0){
${table.ServiceInstanceName}.delete(ids);
req.setAttribute("id", org.apache.shiro.util.StringUtils.toString(ids));
}else{
${table.ServiceInstanceName}.delete(id);
req.setAttribute("id", id);
}
return Result.success();
} catch (Exception e) {
return Result.error();
}
}
}
Loading

0 comments on commit a333023

Please sign in to comment.