mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: 重构 BaseMapper 文档生成器。
This commit is contained in:
parent
8f1a0f6601
commit
e60b17ef71
@ -124,7 +124,14 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.javaparser</groupId>
|
||||
<artifactId>javaparser-symbol-solver-core</artifactId>
|
||||
<version>3.25.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@ -1,164 +0,0 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BaseMapperDocsGen {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
List<MethodInfo> methodInfos = readBaseMapperMethodsInfo();
|
||||
|
||||
StringBuilder insertMethods = new StringBuilder();
|
||||
StringBuilder deleteMethods = new StringBuilder();
|
||||
StringBuilder updateMethods = new StringBuilder();
|
||||
StringBuilder selectMethods = new StringBuilder();
|
||||
StringBuilder relationMethods = new StringBuilder();
|
||||
StringBuilder paginateMethods = new StringBuilder();
|
||||
|
||||
for (MethodInfo methodInfo : methodInfos) {
|
||||
if (methodInfo.name.startsWith("insert")) {
|
||||
insertMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
} else if (methodInfo.name.startsWith("delete")) {
|
||||
deleteMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
} else if (methodInfo.name.startsWith("update")) {
|
||||
updateMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
} else if (methodInfo.name.startsWith("select")) {
|
||||
selectMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
if (methodInfo.name.contains("WithRelation")){
|
||||
relationMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
}
|
||||
}else if (methodInfo.name.startsWith("paginate")) {
|
||||
paginateMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
if (methodInfo.name.contains("WithRelation")){
|
||||
relationMethods.append("- **`").append(methodInfo.getName()).append("`**: ").append(methodInfo.desc).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String mdDir = System.getProperty("user.dir") + "/docs/zh/base/parts/";
|
||||
writeString(new File(mdDir, "base-mapper-insert-methods.md"), insertMethods.toString());
|
||||
writeString(new File(mdDir, "base-mapper-delete-methods.md"), deleteMethods.toString());
|
||||
writeString(new File(mdDir, "base-mapper-update-methods.md"), updateMethods.toString());
|
||||
writeString(new File(mdDir, "base-mapper-query-methods.md"), selectMethods.toString());
|
||||
writeString(new File(mdDir, "base-mapper-relation-methods.md"), relationMethods.toString());
|
||||
writeString(new File(mdDir, "base-mapper-paginate-methods.md"), paginateMethods.toString());
|
||||
|
||||
|
||||
///Users/michael/work/git/mybatis-flex/docs/zh/base/parts/base-mapper-insert-methods.md
|
||||
|
||||
System.out.println(JSON.toJSON(methodInfos));
|
||||
}
|
||||
|
||||
private static List<MethodInfo> readBaseMapperMethodsInfo() throws IOException {
|
||||
List<MethodInfo> methodInfos = new ArrayList<>();
|
||||
String path = System.getProperty("user.dir") + "/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java";
|
||||
BufferedReader br = new BufferedReader(new FileReader(path));
|
||||
String line;
|
||||
MethodInfo methodInfo = null;
|
||||
while ((line = br.readLine()) != null) {
|
||||
line = line.trim();
|
||||
if (line.equals("/**")) {
|
||||
methodInfo = new MethodInfo();
|
||||
} else {
|
||||
if (methodInfo != null && line.length() > 3 && line.startsWith("*") && line.charAt(2) != '@') {
|
||||
methodInfo.addDesc(line.substring(1));
|
||||
} else if (methodInfo != null && !line.contains("=") && (line.startsWith("default")
|
||||
|| line.startsWith("int")
|
||||
|| line.startsWith("T ")
|
||||
|| line.startsWith("List<T> ")
|
||||
)) {
|
||||
String[] tokens = line.split(" ");
|
||||
int methodTokenIndex = 1;
|
||||
if (line.contains("default")) {
|
||||
methodTokenIndex++;
|
||||
}
|
||||
if (line.contains("<R>")) {
|
||||
methodTokenIndex++;
|
||||
}
|
||||
String methodToken = tokens[methodTokenIndex];
|
||||
methodInfo.setName(line.substring(line.indexOf(methodToken)));
|
||||
methodInfos.add(methodInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
br.close();
|
||||
return methodInfos;
|
||||
}
|
||||
|
||||
|
||||
public static void writeString(File file, String content) throws IOException {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file, false);
|
||||
fos.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MethodInfo {
|
||||
private String name;
|
||||
private String desc;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
if (name.endsWith("{") || name.endsWith(";")) {
|
||||
name = name.substring(0, name.length() - 1);
|
||||
}
|
||||
|
||||
name = name.trim();
|
||||
String paramsString = name.substring(name.indexOf("(") + 1, name.lastIndexOf(")"));
|
||||
if (paramsString.length() > 0) {
|
||||
String[] params = paramsString.split(",");
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
String[] paramInfos = params[i].split(" ");
|
||||
params[i] = paramInfos[paramInfos.length - 1];
|
||||
}
|
||||
paramsString = StringUtil.join(", ", params);
|
||||
name = name.substring(0, name.indexOf("(") + 1) + paramsString + ")";
|
||||
this.name = name;
|
||||
} else {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public void addDesc(String desc) {
|
||||
if (this.desc == null) {
|
||||
this.desc = desc.trim();
|
||||
} else {
|
||||
this.desc += desc.trim();
|
||||
}
|
||||
if (this.desc.contains("{@code")){
|
||||
this.desc = this.desc.replace("{@code ","`").replace("}","`");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MethodInfo{" +
|
||||
"name='" + name + '\'' +
|
||||
", desc='" + desc + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.github.javaparser.JavaParser;
|
||||
import com.github.javaparser.ParseResult;
|
||||
import com.github.javaparser.ParserConfiguration;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.comments.JavadocComment;
|
||||
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
|
||||
import com.github.javaparser.javadoc.Javadoc;
|
||||
import com.github.javaparser.javadoc.description.JavadocDescription;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BaseMapperDocsGen {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String mapperPath = System.getProperty("user.dir") + "/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java";
|
||||
|
||||
List<String> methodDescriptions = getMethodDescriptions(mapperPath);
|
||||
|
||||
List<String> insertMethods = new LinkedList<>();
|
||||
List<String> deleteMethods = new LinkedList<>();
|
||||
List<String> updateMethods = new LinkedList<>();
|
||||
List<String> selectMethods = new LinkedList<>();
|
||||
List<String> relationMethods = new LinkedList<>();
|
||||
List<String> paginateMethods = new LinkedList<>();
|
||||
|
||||
methodDescriptions.stream()
|
||||
.map(e -> e.replace("<br>", ""))
|
||||
.map(e -> e.replace("\r\n", ""))
|
||||
.map(e -> e.replaceFirst("\\{@code ", "`"))
|
||||
.map(e -> e.replaceFirst("}", "`"))
|
||||
.map(e -> e.replace("`}", "}`"))
|
||||
.forEach(methodDescription -> {
|
||||
if (methodDescription.contains("insert")) {
|
||||
insertMethods.add(methodDescription);
|
||||
}
|
||||
if (methodDescription.contains("delete")) {
|
||||
deleteMethods.add(methodDescription);
|
||||
}
|
||||
if (methodDescription.contains("update")) {
|
||||
updateMethods.add(methodDescription);
|
||||
}
|
||||
if (methodDescription.contains("select")) {
|
||||
selectMethods.add(methodDescription);
|
||||
}
|
||||
if (methodDescription.contains("Relation")) {
|
||||
relationMethods.add(methodDescription);
|
||||
}
|
||||
if (methodDescription.contains("paginate")) {
|
||||
paginateMethods.add(methodDescription);
|
||||
}
|
||||
});
|
||||
|
||||
String mdDir = System.getProperty("user.dir") + "/docs/zh/base/parts/";
|
||||
|
||||
writeString(new File(mdDir, "base-mapper-insert-methods.md"), insertMethods);
|
||||
writeString(new File(mdDir, "base-mapper-delete-methods.md"), deleteMethods);
|
||||
writeString(new File(mdDir, "base-mapper-update-methods.md"), updateMethods);
|
||||
writeString(new File(mdDir, "base-mapper-query-methods.md"), selectMethods);
|
||||
writeString(new File(mdDir, "base-mapper-relation-methods.md"), relationMethods);
|
||||
writeString(new File(mdDir, "base-mapper-paginate-methods.md"), paginateMethods);
|
||||
}
|
||||
|
||||
public static void writeString(File file, List<String> contents) throws IOException {
|
||||
try (FileWriter fw = new FileWriter(file, false);
|
||||
BufferedWriter bw = new BufferedWriter(fw)) {
|
||||
for (String content : contents) {
|
||||
bw.write(content);
|
||||
bw.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getMethodDescriptions(String mapperPath) throws IOException {
|
||||
ParserConfiguration parserConfiguration = new ParserConfiguration();
|
||||
parserConfiguration.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8);
|
||||
JavaParser javaParser = new JavaParser();
|
||||
ParseResult<CompilationUnit> parseResult = javaParser.parse(Paths.get(mapperPath));
|
||||
Optional<CompilationUnit> compilationUnitOpt = parseResult.getResult();
|
||||
List<String> methodDescriptions = new LinkedList<>();
|
||||
if (compilationUnitOpt.isPresent()) {
|
||||
CompilationUnit compilationUnit = compilationUnitOpt.get();
|
||||
List<MethodDeclaration> methodDeclarations = compilationUnit.stream()
|
||||
.filter(e -> e instanceof MethodDeclaration)
|
||||
.map(e -> (MethodDeclaration) e)
|
||||
.collect(Collectors.toList());
|
||||
for (MethodDeclaration methodDeclaration : methodDeclarations) {
|
||||
String methodName = methodDeclaration.getNameAsString();
|
||||
String params = methodDeclaration.getParameters()
|
||||
.stream()
|
||||
.map(NodeWithSimpleName::getNameAsString)
|
||||
.collect(Collectors.joining(", ", "(", ")"));
|
||||
String comment = methodDeclaration.getJavadocComment()
|
||||
.map(JavadocComment::parse)
|
||||
.map(Javadoc::getDescription)
|
||||
.map(JavadocDescription::toText)
|
||||
.orElse("");
|
||||
methodDescriptions.add("- **`" + methodName + params + "`**:" + comment);
|
||||
}
|
||||
}
|
||||
return methodDescriptions;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user