新增代码生成打包下载功能

pull/214/head
dqjdda 2019-11-26 17:42:56 +08:00
parent 2ad07c8f02
commit 2ecb82a55a
6 changed files with 118 additions and 13 deletions

View File

@ -9,7 +9,7 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<hutool.version>[4.1.12,)</hutool.version> <hutool.version>5.0.6</hutool.version>
</properties> </properties>
<artifactId>eladmin-common</artifactId> <artifactId>eladmin-common</artifactId>

View File

@ -6,9 +6,11 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter; import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelUtil;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import org.apache.poi.util.IOUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -273,6 +275,37 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return null; return null;
} }
/**
*
* @param request /
* @param response /
* @param file /
*/
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file, boolean deleteOnExit){
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
IOUtils.copy(fis,response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
if(deleteOnExit){
file.deleteOnExit();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String getMd5(File file) { public static String getMd5(File file) {
return getMd5(getByte(file)); return getMd5(getByte(file));
} }

View File

@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
/** /**
@ -67,9 +69,9 @@ public class GeneratorController {
@ApiOperation("生成代码") @ApiOperation("生成代码")
@PostMapping(value = "/{tableName}/{type}") @PostMapping(value = "/{tableName}/{type}")
public ResponseEntity generator(@PathVariable String tableName, @PathVariable Integer type){ public ResponseEntity generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
if(!generatorEnabled){ if(!generatorEnabled && type == 0){
throw new BadRequestException("此环境不允许生成代码"); throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看");
} }
switch (type){ switch (type){
// 生成代码 // 生成代码
@ -77,7 +79,10 @@ public class GeneratorController {
break; break;
// 预览 // 预览
case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName)); case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
default: break; // 打包
case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
break;
default: throw new BadRequestException("没有这个选项");
} }
return new ResponseEntity(HttpStatus.OK); return new ResponseEntity(HttpStatus.OK);
} }

View File

@ -4,7 +4,8 @@ import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
/** /**
@ -61,4 +62,13 @@ public interface GeneratorService {
* @return / * @return /
*/ */
ResponseEntity preview(GenConfig genConfig, List<ColumnInfo> columns); ResponseEntity preview(GenConfig genConfig, List<ColumnInfo> columns);
/**
*
* @param genConfig
* @param columns
* @param request
* @param response
*/
void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);
} }

View File

@ -2,12 +2,14 @@ package me.zhengjie.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
import me.zhengjie.domain.GenConfig; import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.domain.vo.TableInfo; import me.zhengjie.domain.vo.TableInfo;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.repository.ColumnInfoRepository; import me.zhengjie.repository.ColumnInfoRepository;
import me.zhengjie.service.GeneratorService; import me.zhengjie.service.GeneratorService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.GenUtil; import me.zhengjie.utils.GenUtil;
import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.StringUtils; import me.zhengjie.utils.StringUtils;
@ -17,6 +19,9 @@ import org.springframework.stereotype.Service;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -136,4 +141,19 @@ public class GeneratorServiceImpl implements GeneratorService {
List<Map<String,Object>> genList = GenUtil.preview(columns, genConfig); List<Map<String,Object>> genList = GenUtil.preview(columns, genConfig);
return new ResponseEntity<>(genList, HttpStatus.OK); return new ResponseEntity<>(genList, HttpStatus.OK);
} }
@Override
public void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response) {
if(genConfig.getId() == null){
throw new BadRequestException("请先配置生成器");
}
try {
File file = new File(GenUtil.download(columns, genConfig));
String zipPath = file.getPath() + ".zip";
ZipUtil.zip(file.getPath(), zipPath);
FileUtil.downloadFile(request, response, new File(zipPath), true);
} catch (IOException e) {
throw new BadRequestException("打包失败");
}
}
} }

View File

@ -84,6 +84,44 @@ public class GenUtil {
return genList; return genList;
} }
public static String download(List<ColumnInfo> columns, GenConfig genConfig) throws IOException {
String tempPath =System.getProperty("java.io.tmpdir") + "eladmin-gen-temp" + File.separator + genConfig.getTableName() + File.separator;
Map<String,Object> genMap = getGenMap(columns, genConfig);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
// 生成后端代码
List<String> templates = getAdminTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/admin/"+templateName+".ftl");
String filePath = getAdminFilePath(templateName,genConfig,genMap.get("className").toString(),tempPath + "eladmin" + File.separator);
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
// 生成前端代码
templates = getFrontTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/front/"+templateName+".ftl");
String path = tempPath + "eladmin-web" + File.separator;
String apiPath = path + "src" + File.separator + "api" + File.separator;
String srcPath = path + "src" + File.separator + "views" + File.separator + genMap.get("changeClassName").toString() + File.separator;
String filePath = getFrontFilePath(templateName, apiPath, srcPath, genMap.get("changeClassName").toString());
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
return tempPath;
}
public static void generatorCode(List<ColumnInfo> columnInfos, GenConfig genConfig) throws IOException { public static void generatorCode(List<ColumnInfo> columnInfos, GenConfig genConfig) throws IOException {
Map<String,Object> genMap = getGenMap(columnInfos, genConfig); Map<String,Object> genMap = getGenMap(columnInfos, genConfig);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH)); TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
@ -91,7 +129,7 @@ public class GenUtil {
List<String> templates = getAdminTemplateNames(); List<String> templates = getAdminTemplateNames();
for (String templateName : templates) { for (String templateName : templates) {
Template template = engine.getTemplate("generator/admin/"+templateName+".ftl"); Template template = engine.getTemplate("generator/admin/"+templateName+".ftl");
String filePath = getAdminFilePath(templateName,genConfig,genMap.get("className").toString()); String filePath = getAdminFilePath(templateName,genConfig,genMap.get("className").toString(),System.getProperty("user.dir"));
assert filePath != null; assert filePath != null;
File file = new File(filePath); File file = new File(filePath);
@ -108,7 +146,7 @@ public class GenUtil {
templates = getFrontTemplateNames(); templates = getFrontTemplateNames();
for (String templateName : templates) { for (String templateName : templates) {
Template template = engine.getTemplate("generator/front/"+templateName+".ftl"); Template template = engine.getTemplate("generator/front/"+templateName+".ftl");
String filePath = getFrontFilePath(templateName,genConfig,genMap.get("changeClassName").toString()); String filePath = getFrontFilePath(templateName,genConfig.getApiPath(),genConfig.getPath(),genMap.get("changeClassName").toString());
assert filePath != null; assert filePath != null;
File file = new File(filePath); File file = new File(filePath);
@ -283,8 +321,8 @@ public class GenUtil {
/** /**
* *
*/ */
private static String getAdminFilePath(String templateName, GenConfig genConfig, String className) { private static String getAdminFilePath(String templateName, GenConfig genConfig, String className, String rootPath) {
String projectPath = System.getProperty("user.dir") + File.separator + genConfig.getModuleName(); String projectPath = rootPath + File.separator + genConfig.getModuleName();
String packagePath = projectPath + File.separator + "src" +File.separator+ "main" + File.separator + "java" + File.separator; String packagePath = projectPath + File.separator + "src" +File.separator+ "main" + File.separator + "java" + File.separator;
if (!ObjectUtils.isEmpty(genConfig.getPack())) { if (!ObjectUtils.isEmpty(genConfig.getPack())) {
packagePath += genConfig.getPack().replace(".", File.separator) + File.separator; packagePath += genConfig.getPack().replace(".", File.separator) + File.separator;
@ -328,11 +366,10 @@ public class GenUtil {
/** /**
* *
*/ */
private static String getFrontFilePath(String templateName, GenConfig genConfig, String apiName) { private static String getFrontFilePath(String templateName, String apiPath, String path, String apiName) {
String path = genConfig.getPath();
if ("api".equals(templateName)) { if ("api".equals(templateName)) {
return genConfig.getApiPath() + File.separator + apiName + ".js"; return apiPath + File.separator + apiName + ".js";
} }
if ("index".equals(templateName)) { if ("index".equals(templateName)) {