2019-09-27 00:09:37 +08:00
|
|
|
package com.power.doc.builder;
|
|
|
|
|
|
|
|
import com.power.common.util.CollectionUtil;
|
2019-09-29 00:40:37 +08:00
|
|
|
import com.power.common.util.DateTimeUtil;
|
2019-09-27 00:09:37 +08:00
|
|
|
import com.power.common.util.FileUtil;
|
|
|
|
import com.power.common.util.StringUtil;
|
|
|
|
import com.power.doc.constants.DocGlobalConstants;
|
|
|
|
import com.power.doc.constants.DocLanguage;
|
|
|
|
import com.power.doc.constants.TemplateVariable;
|
2019-11-05 23:14:00 +08:00
|
|
|
import com.power.doc.model.*;
|
2019-09-27 00:09:37 +08:00
|
|
|
import com.power.doc.utils.BeetlTemplateUtil;
|
|
|
|
import org.beetl.core.Template;
|
|
|
|
|
2019-11-05 23:14:00 +08:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
2019-09-27 00:09:37 +08:00
|
|
|
import java.util.List;
|
2019-11-05 23:14:00 +08:00
|
|
|
import java.util.Objects;
|
2019-09-27 00:09:37 +08:00
|
|
|
|
|
|
|
import static com.power.doc.constants.DocGlobalConstants.FILE_SEPARATOR;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author yu 2019/9/26.
|
|
|
|
*/
|
2019-09-27 00:38:01 +08:00
|
|
|
public class DocBuilderTemplate {
|
2019-09-27 00:09:37 +08:00
|
|
|
|
2019-09-29 00:40:37 +08:00
|
|
|
private static long now = System.currentTimeMillis();
|
2019-11-05 23:14:00 +08:00
|
|
|
|
2019-09-27 00:09:37 +08:00
|
|
|
/**
|
|
|
|
* check condition and init
|
|
|
|
*
|
2019-09-27 00:38:01 +08:00
|
|
|
* @param config Api config
|
2019-09-27 00:09:37 +08:00
|
|
|
*/
|
|
|
|
public void checkAndInit(ApiConfig config) {
|
|
|
|
if (null == config) {
|
|
|
|
throw new NullPointerException("ApiConfig can't be null");
|
|
|
|
}
|
|
|
|
if (StringUtil.isEmpty(config.getOutPath())) {
|
|
|
|
throw new RuntimeException("doc output path can't be null or empty");
|
|
|
|
}
|
|
|
|
if (null != config.getLanguage()) {
|
|
|
|
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, config.getLanguage().getCode());
|
|
|
|
} else {
|
|
|
|
//default is chinese
|
|
|
|
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, DocLanguage.CHINESE.getCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 23:42:56 +08:00
|
|
|
/**
|
|
|
|
* check condition and init for get Data
|
|
|
|
*
|
|
|
|
* @param config Api config
|
|
|
|
*/
|
|
|
|
public void checkAndInitForGetApiData(ApiConfig config) {
|
|
|
|
if (null == config) {
|
|
|
|
throw new NullPointerException("ApiConfig can't be null");
|
|
|
|
}
|
|
|
|
if (null != config.getLanguage()) {
|
|
|
|
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, config.getLanguage().getCode());
|
|
|
|
} else {
|
|
|
|
//default is chinese
|
|
|
|
System.setProperty(DocGlobalConstants.DOC_LANGUAGE, DocLanguage.CHINESE.getCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 00:09:37 +08:00
|
|
|
/**
|
|
|
|
* Generate api documentation for all controllers.
|
|
|
|
*
|
|
|
|
* @param apiDocList list of api doc
|
|
|
|
* @param config api config
|
|
|
|
* @param template template
|
|
|
|
* @param fileExtension file extension
|
|
|
|
*/
|
|
|
|
public void buildApiDoc(List<ApiDoc> apiDocList, ApiConfig config, String template, String fileExtension) {
|
|
|
|
FileUtil.mkdirs(config.getOutPath());
|
|
|
|
for (ApiDoc doc : apiDocList) {
|
|
|
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
|
|
|
mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
|
|
|
|
mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName());
|
|
|
|
mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList());
|
|
|
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge all api doc into one document
|
|
|
|
*
|
2019-09-27 00:38:01 +08:00
|
|
|
* @param apiDocList list data of Api doc
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge all api doc into one document
|
2019-09-27 23:42:56 +08:00
|
|
|
*
|
|
|
|
* @param apiDocList list data of Api doc
|
|
|
|
* @param config api config
|
|
|
|
* @param template template
|
2019-09-27 00:38:01 +08:00
|
|
|
* @param outPutFileName output file
|
2019-09-27 00:09:37 +08:00
|
|
|
*/
|
|
|
|
public void buildAllInOne(List<ApiDoc> apiDocList, ApiConfig config, String template, String outPutFileName) {
|
|
|
|
String outPath = config.getOutPath();
|
2019-09-29 00:40:37 +08:00
|
|
|
String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
|
2019-09-27 00:09:37 +08:00
|
|
|
FileUtil.mkdirs(outPath);
|
|
|
|
Template tpl = BeetlTemplateUtil.getByName(template);
|
|
|
|
tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
|
|
|
tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), config.getErrorCodes());
|
|
|
|
tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.VERSION.getVariable(), now);
|
2019-09-29 00:40:37 +08:00
|
|
|
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
|
|
|
|
if (CollectionUtil.isEmpty(config.getErrorCodes())) {
|
|
|
|
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 1);
|
|
|
|
} else {
|
|
|
|
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
|
|
|
}
|
2019-10-29 11:09:00 +08:00
|
|
|
if (null != config.getLanguage()) {
|
|
|
|
if (DocLanguage.CHINESE.code.equals(config.getLanguage().getCode())) {
|
|
|
|
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_CN_TITLE);
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_CN_TITLE);
|
2019-10-29 11:09:00 +08:00
|
|
|
} else {
|
|
|
|
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_EN_TITLE);
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_EN_TITLE);
|
2019-10-29 11:09:00 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_CN_TITLE);
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_CN_TITLE);
|
2019-10-29 11:09:00 +08:00
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
List<ApiDocDict> apiDocDictList = buildDictionary(config);
|
|
|
|
tpl.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);
|
2019-09-27 00:09:37 +08:00
|
|
|
FileUtil.nioWriteFile(tpl.render(), outPath + FILE_SEPARATOR + outPutFileName);
|
|
|
|
}
|
|
|
|
|
2019-09-27 00:38:01 +08:00
|
|
|
|
2019-09-27 00:09:37 +08:00
|
|
|
/**
|
2019-09-27 00:38:01 +08:00
|
|
|
* build error_code adoc
|
2019-09-27 23:42:56 +08:00
|
|
|
*
|
|
|
|
* @param errorCodeList list data of Api doc
|
|
|
|
* @param config api config
|
|
|
|
* @param template template
|
2019-09-27 00:38:01 +08:00
|
|
|
* @param outPutFileName output file
|
2019-09-27 00:09:37 +08:00
|
|
|
*/
|
|
|
|
public void buildErrorCodeDoc(List<ApiErrorCode> errorCodeList, ApiConfig config, String template, String outPutFileName) {
|
|
|
|
if (CollectionUtil.isNotEmpty(errorCodeList)) {
|
|
|
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
|
|
|
mapper.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
|
|
|
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 23:42:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a single controller api document
|
|
|
|
*
|
|
|
|
* @param outPath output path
|
|
|
|
* @param controllerName controller name
|
|
|
|
* @param template template
|
|
|
|
* @param fileExtension file extension
|
|
|
|
*/
|
|
|
|
public void buildSingleControllerApi(String outPath, String controllerName, String template, String fileExtension) {
|
|
|
|
FileUtil.mkdirs(outPath);
|
|
|
|
SourceBuilder sourceBuilder = new SourceBuilder(true);
|
|
|
|
ApiDoc doc = sourceBuilder.getSingleControllerApiData(controllerName);
|
|
|
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
|
|
|
mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
|
|
|
|
mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName());
|
|
|
|
mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList());
|
|
|
|
FileUtil.writeFileNotAppend(mapper.render(), outPath + FILE_SEPARATOR + doc.getName() + fileExtension);
|
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build dictionary
|
|
|
|
*
|
|
|
|
* @param config api config
|
|
|
|
* @return list of ApiDocDict
|
|
|
|
*/
|
|
|
|
public List<ApiDocDict> buildDictionary(ApiConfig config) {
|
2019-11-06 15:53:13 +08:00
|
|
|
List<ApiDataDictionary> apiDataDictionaryList = config.getDataDictionaries();
|
2019-11-07 16:58:09 +08:00
|
|
|
if (CollectionUtil.isEmpty(apiDataDictionaryList)) {
|
|
|
|
return new ArrayList<>(0);
|
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
List<ApiDocDict> apiDocDictList = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int order = 0;
|
2019-11-06 15:53:13 +08:00
|
|
|
for (ApiDataDictionary apiDataDictionary : apiDataDictionaryList) {
|
2019-11-05 23:14:00 +08:00
|
|
|
order++;
|
|
|
|
ApiDocDict apiDocDict = new ApiDocDict();
|
|
|
|
apiDocDict.setOrder(order);
|
|
|
|
apiDocDict.setTitle(apiDataDictionary.getTitle());
|
|
|
|
Class<?> clzz = apiDataDictionary.getEnumClass();
|
|
|
|
if (Objects.isNull(clzz)) {
|
|
|
|
if (StringUtil.isEmpty(apiDataDictionary.getEnumClassName())) {
|
|
|
|
throw new RuntimeException(" enum class name can't be null.");
|
|
|
|
}
|
|
|
|
clzz = Class.forName(apiDataDictionary.getEnumClassName());
|
|
|
|
}
|
|
|
|
if (!clzz.isEnum()) {
|
|
|
|
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
|
|
|
}
|
|
|
|
List<DataDict> dataDictList = new ArrayList<>();
|
|
|
|
Object[] objects = clzz.getEnumConstants();
|
2019-11-06 16:23:47 +08:00
|
|
|
String valueMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getCodeField());
|
2019-11-05 23:14:00 +08:00
|
|
|
String descMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getDescField());
|
|
|
|
Method valueMethod = clzz.getMethod(valueMethodName);
|
|
|
|
Method descMethod = clzz.getMethod(descMethodName);
|
|
|
|
for (Object object : objects) {
|
|
|
|
Object val = valueMethod.invoke(object);
|
|
|
|
Object desc = descMethod.invoke(object);
|
|
|
|
DataDict dataDict = new DataDict();
|
2019-11-06 10:00:26 +08:00
|
|
|
if (val instanceof String) {
|
|
|
|
dataDict.setType("string");
|
|
|
|
} else {
|
|
|
|
dataDict.setType("int32");
|
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
dataDict.setDesc(desc.toString());
|
|
|
|
dataDict.setValue(val.toString());
|
|
|
|
dataDictList.add(dataDict);
|
|
|
|
}
|
|
|
|
apiDocDict.setDataDictList(dataDictList);
|
|
|
|
apiDocDictList.add(apiDocDict);
|
|
|
|
}
|
|
|
|
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
|
|
|
|
e.fillInStackTrace();
|
|
|
|
}
|
|
|
|
return apiDocDictList;
|
|
|
|
}
|
2019-09-27 00:09:37 +08:00
|
|
|
}
|