2020-01-14 00:38:02 +08:00
|
|
|
/*
|
|
|
|
* smart-doc https://github.com/shalousun/smart-doc
|
|
|
|
*
|
2020-05-16 19:24:54 +08:00
|
|
|
* Copyright (C) 2018-2020 smart-doc
|
2020-01-14 00:38:02 +08:00
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2019-09-27 00:09:37 +08:00
|
|
|
package com.power.doc.builder;
|
|
|
|
|
2019-12-08 20:46:32 +08:00
|
|
|
import com.power.common.util.*;
|
2020-11-21 02:15:13 +08:00
|
|
|
import com.power.doc.constants.HighlightStyle;
|
2019-09-27 00:09:37 +08:00
|
|
|
import com.power.doc.constants.TemplateVariable;
|
2019-11-05 23:14:00 +08:00
|
|
|
import com.power.doc.model.*;
|
2019-12-25 01:20:26 +08:00
|
|
|
import com.power.doc.template.IDocBuildTemplate;
|
|
|
|
import com.power.doc.template.SpringBootDocBuildTemplate;
|
2019-09-27 00:09:37 +08:00
|
|
|
import com.power.doc.utils.BeetlTemplateUtil;
|
2019-12-07 18:16:25 +08:00
|
|
|
import com.power.doc.utils.DocUtil;
|
2019-12-14 16:20:43 +08:00
|
|
|
import com.thoughtworks.qdox.JavaProjectBuilder;
|
|
|
|
import com.thoughtworks.qdox.model.JavaClass;
|
2019-09-27 00:09:37 +08:00
|
|
|
import org.beetl.core.Template;
|
|
|
|
|
2019-11-05 23:14:00 +08:00
|
|
|
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.
|
|
|
|
*/
|
2020-05-24 11:32:29 +08:00
|
|
|
public class DocBuilderTemplate extends BaseDocBuilderTemplate {
|
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-12-07 18:16:25 +08:00
|
|
|
/**
|
|
|
|
* get all api data
|
|
|
|
*
|
2020-01-11 19:41:31 +08:00
|
|
|
* @param config ApiConfig
|
2019-12-14 16:20:43 +08:00
|
|
|
* @param javaProjectBuilder JavaProjectBuilder
|
2019-12-25 22:57:12 +08:00
|
|
|
* @return ApiAllData
|
2019-12-07 18:16:25 +08:00
|
|
|
*/
|
2019-12-25 01:20:26 +08:00
|
|
|
public ApiAllData getApiData(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
|
2019-12-07 18:16:25 +08:00
|
|
|
ApiAllData apiAllData = new ApiAllData();
|
|
|
|
apiAllData.setProjectName(config.getProjectName());
|
2020-01-04 00:24:05 +08:00
|
|
|
apiAllData.setProjectId(DocUtil.generateId(config.getProjectName()));
|
2019-12-07 18:16:25 +08:00
|
|
|
apiAllData.setLanguage(config.getLanguage().getCode());
|
2019-12-25 01:20:26 +08:00
|
|
|
apiAllData.setApiDocList(listOfApiData(config, javaProjectBuilder));
|
2019-12-07 18:16:25 +08:00
|
|
|
apiAllData.setErrorCodeList(errorCodeDictToList(config));
|
|
|
|
apiAllData.setRevisionLogs(config.getRevisionLogs());
|
2019-12-25 01:20:26 +08:00
|
|
|
apiAllData.setApiDocDictList(buildDictionary(config, javaProjectBuilder));
|
2019-12-07 18:16:25 +08:00
|
|
|
return apiAllData;
|
|
|
|
}
|
|
|
|
|
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());
|
2020-08-26 22:09:12 +08:00
|
|
|
mapper.binding(TemplateVariable.REQUEST_EXAMPLE.getVariable(), config.isRequestExample());
|
|
|
|
mapper.binding(TemplateVariable.RESPONSE_EXAMPLE.getVariable(), config.isResponseExample());
|
2019-09-27 00:09:37 +08:00
|
|
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge all api doc into one document
|
2019-09-27 23:42:56 +08:00
|
|
|
*
|
2019-12-25 01:20:26 +08:00
|
|
|
* @param apiDocList list data of Api doc
|
|
|
|
* @param config api config
|
2019-12-14 16:20:43 +08:00
|
|
|
* @param javaProjectBuilder JavaProjectBuilder
|
2019-12-25 01:20:26 +08:00
|
|
|
* @param template template
|
|
|
|
* @param outPutFileName output file
|
2019-09-27 00:09:37 +08:00
|
|
|
*/
|
2019-12-25 01:20:26 +08:00
|
|
|
public void buildAllInOne(List<ApiDoc> apiDocList, ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
|
2019-09-27 00:09:37 +08:00
|
|
|
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);
|
2019-12-07 18:16:25 +08:00
|
|
|
List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);
|
|
|
|
|
2019-09-27 00:09:37 +08:00
|
|
|
Template tpl = BeetlTemplateUtil.getByName(template);
|
2020-11-21 02:15:13 +08:00
|
|
|
String style = config.getStyle();
|
|
|
|
tpl.binding(TemplateVariable.STYLE.getVariable(),style);
|
|
|
|
tpl.binding(TemplateVariable.BACKGROUND.getVariable(), HighlightStyle.getBackgroundColor(style));
|
2019-09-27 00:09:37 +08:00
|
|
|
tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
2019-12-07 16:07:43 +08:00
|
|
|
tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
|
2019-09-27 00:09:37 +08:00
|
|
|
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());
|
2020-07-13 11:05:04 +08:00
|
|
|
tpl.binding(TemplateVariable.REQUEST_EXAMPLE.getVariable(), config.isRequestExample());
|
|
|
|
tpl.binding(TemplateVariable.RESPONSE_EXAMPLE.getVariable(), config.isResponseExample());
|
2019-12-07 16:07:43 +08:00
|
|
|
if (CollectionUtil.isEmpty(errorCodeList)) {
|
2019-11-05 23:14:00 +08:00
|
|
|
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 1);
|
|
|
|
} else {
|
|
|
|
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
|
|
|
}
|
2020-05-15 13:56:59 +08:00
|
|
|
setDirectoryLanguageVariable(config, tpl);
|
2019-12-25 01:20:26 +08:00
|
|
|
List<ApiDocDict> apiDocDictList = buildDictionary(config, javaProjectBuilder);
|
2019-11-05 23:14:00 +08:00
|
|
|
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 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
|
|
|
*/
|
2019-12-07 15:37:55 +08:00
|
|
|
public void buildErrorCodeDoc(ApiConfig config, String template, String outPutFileName) {
|
2019-12-07 18:16:25 +08:00
|
|
|
List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);
|
2019-12-07 15:37:55 +08:00
|
|
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
|
|
|
mapper.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
|
|
|
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
2019-09-27 00:09:37 +08:00
|
|
|
}
|
2019-09-27 23:42:56 +08:00
|
|
|
|
2020-05-15 13:56:59 +08:00
|
|
|
/**
|
|
|
|
* build common_data doc
|
|
|
|
*
|
2020-05-31 02:03:11 +08:00
|
|
|
* @param config api config
|
|
|
|
* @param javaProjectBuilder JavaProjectBuilder
|
|
|
|
* @param template template
|
|
|
|
* @param outPutFileName output file
|
2020-05-15 13:56:59 +08:00
|
|
|
*/
|
|
|
|
public void buildDirectoryDataDoc(ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
|
|
|
|
List<ApiDocDict> directoryList = buildDictionary(config, javaProjectBuilder);
|
|
|
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
|
|
|
setDirectoryLanguageVariable(config, mapper);
|
|
|
|
mapper.binding(TemplateVariable.DICT_LIST.getVariable(), directoryList);
|
|
|
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-27 23:42:56 +08:00
|
|
|
/**
|
|
|
|
* Generate a single controller api document
|
|
|
|
*
|
2020-01-09 20:44:09 +08:00
|
|
|
* @param projectBuilder projectBuilder
|
2019-09-27 23:42:56 +08:00
|
|
|
* @param controllerName controller name
|
|
|
|
* @param template template
|
|
|
|
* @param fileExtension file extension
|
|
|
|
*/
|
2020-01-09 10:58:34 +08:00
|
|
|
public void buildSingleApi(ProjectDocConfigBuilder projectBuilder, String controllerName, String template, String fileExtension) {
|
|
|
|
ApiConfig config = projectBuilder.getApiConfig();
|
|
|
|
FileUtil.mkdirs(config.getOutPath());
|
2020-05-24 11:32:29 +08:00
|
|
|
IDocBuildTemplate<ApiDoc> docBuildTemplate = new SpringBootDocBuildTemplate();
|
2020-01-11 19:41:31 +08:00
|
|
|
ApiDoc doc = docBuildTemplate.getSingleApiData(projectBuilder, controllerName);
|
2019-09-27 23:42:56 +08:00
|
|
|
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());
|
2020-01-09 10:58:34 +08:00
|
|
|
FileUtil.writeFileNotAppend(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension);
|
2019-09-27 23:42:56 +08:00
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build dictionary
|
|
|
|
*
|
2019-12-25 01:20:26 +08:00
|
|
|
* @param config api config
|
2019-12-14 16:20:43 +08:00
|
|
|
* @param javaProjectBuilder JavaProjectBuilder
|
2019-11-05 23:14:00 +08:00
|
|
|
* @return list of ApiDocDict
|
|
|
|
*/
|
2019-12-14 16:20:43 +08:00
|
|
|
public List<ApiDocDict> buildDictionary(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
|
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++;
|
2019-12-14 16:20:43 +08:00
|
|
|
Class<?> clazz = apiDataDictionary.getEnumClass();
|
|
|
|
if (Objects.isNull(clazz)) {
|
|
|
|
if (StringUtil.isEmpty(apiDataDictionary.getEnumClassName())) {
|
|
|
|
throw new RuntimeException("Enum class name can't be null.");
|
|
|
|
}
|
|
|
|
clazz = Class.forName(apiDataDictionary.getEnumClassName());
|
|
|
|
}
|
2019-11-05 23:14:00 +08:00
|
|
|
ApiDocDict apiDocDict = new ApiDocDict();
|
|
|
|
apiDocDict.setOrder(order);
|
|
|
|
apiDocDict.setTitle(apiDataDictionary.getTitle());
|
2019-12-14 16:20:43 +08:00
|
|
|
JavaClass javaClass = javaProjectBuilder.getClassByName(clazz.getCanonicalName());
|
|
|
|
if (apiDataDictionary.getTitle() == null) {
|
|
|
|
apiDocDict.setTitle(javaClass.getComment());
|
2019-11-05 23:14:00 +08:00
|
|
|
}
|
2019-12-14 16:20:43 +08:00
|
|
|
List<DataDict> enumDictionaryList = EnumUtil.getEnumInformation(clazz, apiDataDictionary.getCodeField(),
|
2019-12-07 19:42:37 +08:00
|
|
|
apiDataDictionary.getDescField());
|
2019-12-14 16:20:43 +08:00
|
|
|
if (!clazz.isEnum()) {
|
|
|
|
throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class.");
|
2019-11-05 23:14:00 +08:00
|
|
|
}
|
2019-12-07 19:42:37 +08:00
|
|
|
apiDocDict.setDataDictList(enumDictionaryList);
|
2019-11-05 23:14:00 +08:00
|
|
|
apiDocDictList.add(apiDocDict);
|
|
|
|
}
|
2019-12-07 19:42:37 +08:00
|
|
|
} catch (ClassNotFoundException e) {
|
2019-11-10 16:12:26 +08:00
|
|
|
e.printStackTrace();
|
2019-11-05 23:14:00 +08:00
|
|
|
}
|
|
|
|
return apiDocDictList;
|
|
|
|
}
|
2019-12-07 15:37:55 +08:00
|
|
|
|
2019-12-25 01:20:26 +08:00
|
|
|
private List<ApiDoc> listOfApiData(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
|
2019-12-07 18:16:25 +08:00
|
|
|
this.checkAndInitForGetApiData(config);
|
|
|
|
config.setMd5EncryptedHtmlName(true);
|
2019-12-25 01:20:26 +08:00
|
|
|
ProjectDocConfigBuilder configBuilder = new ProjectDocConfigBuilder(config, javaProjectBuilder);
|
|
|
|
IDocBuildTemplate docBuildTemplate = new SpringBootDocBuildTemplate();
|
|
|
|
return docBuildTemplate.getApiData(configBuilder);
|
2019-12-07 18:16:25 +08:00
|
|
|
}
|
2019-09-27 00:09:37 +08:00
|
|
|
}
|