Skip to content

Commit 18dc2ec

Browse files
author
lihao18
committed
gen html
1 parent 6372314 commit 18dc2ec

File tree

51 files changed

+1037
-503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1037
-503
lines changed

gendoc-core/pom.xml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@
99
<artifactId>gendoc-core</artifactId>
1010
<version>1.0-SNAPSHOT</version>
1111

12-
13-
1412
<dependencies>
15-
<dependency>
16-
<groupId>org.projectlombok</groupId>
17-
<artifactId>lombok</artifactId>
18-
<version>1.16.20</version>
19-
<scope>provided</scope>
20-
</dependency>
21-
2213
<dependency>
2314
<groupId>org.freemarker</groupId>
2415
<artifactId>freemarker</artifactId>
@@ -51,6 +42,19 @@
5142
</dependency>
5243
</dependencies>
5344

45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<configuration>
51+
<source>1.8</source>
52+
<target>1.8</target>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
5458

5559

5660
</project>
Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,104 @@
11
package com.easycodingnow;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
5-
import lombok.Data;
3+
import com.easycodingnow.utils.CollectionUtils;
4+
import com.easycodingnow.utils.StringUtils;
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
67

78
import java.util.List;
89

910
/**
11+
* 文档生成配置
1012
* @author lihao
1113
* @since 2018/3/21
1214
*/
13-
@AllArgsConstructor
14-
@Builder
15-
@Data
1615
public class GenConfig {
16+
private static final Log logger = LogFactory.getLog(GenConfig.class);
17+
1718

1819
private List<String> scanPackages;
1920

20-
private String[] sourcePathRoot;
21+
private List<String> sourcePathRoot;
22+
23+
private String outputPath;
24+
25+
private WebType webType = WebType.SPRING_MVC;
26+
27+
private OutPutType outPutType = OutPutType.HTML;
28+
29+
30+
//校验配置是否合法
31+
boolean validate(){
32+
if(CollectionUtils.isEmpty(sourcePathRoot)){
33+
logger.error("gendoc gen failure! GenConfig.sourcePathRoot was required!");
34+
return false;
35+
}
36+
37+
if(CollectionUtils.isEmpty(scanPackages)){
38+
logger.error("gendoc gen failure! GenConfig.scanPackages was required!");
39+
return false;
40+
}
41+
42+
if(StringUtils.isEmpty(outputPath)){
43+
logger.error("gendoc gen failure! GenConfig.outputPath was required!");
44+
return false;
45+
}
46+
47+
return true;
48+
49+
}
50+
51+
//解析的web框架类型,目前只支持spring mvc
52+
enum WebType{
53+
SPRING_MVC;
54+
}
55+
56+
//输出文档类型,目前只支持HTML
57+
enum OutPutType{
58+
59+
HTML,
60+
MARKDOWN,
61+
WORD;
62+
63+
}
64+
65+
public List<String> getScanPackages() {
66+
return scanPackages;
67+
}
68+
69+
public void setScanPackages(List<String> scanPackages) {
70+
this.scanPackages = scanPackages;
71+
}
72+
73+
public List<String> getSourcePathRoot() {
74+
return sourcePathRoot;
75+
}
76+
77+
public void setSourcePathRoot(List<String> sourcePathRoot) {
78+
this.sourcePathRoot = sourcePathRoot;
79+
}
80+
81+
public String getOutputPath() {
82+
return outputPath;
83+
}
84+
85+
public void setOutputPath(String outputPath) {
86+
this.outputPath = outputPath;
87+
}
88+
89+
public WebType getWebType() {
90+
return webType;
91+
}
92+
93+
public void setWebType(WebType webType) {
94+
this.webType = webType;
95+
}
2196

22-
private String genHtmlPath;
97+
public OutPutType getOutPutType() {
98+
return outPutType;
99+
}
23100

101+
public void setOutPutType(OutPutType outPutType) {
102+
this.outPutType = outPutType;
103+
}
24104
}

gendoc-core/src/main/java/com/easycodingnow/GenDoc.java

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.easycodingnow.parse.Parse;
55
import com.easycodingnow.reflect.Class;
66
import com.easycodingnow.template.TemplateParse;
7+
import com.easycodingnow.template.vo.DocApiApiClass;
78
import com.easycodingnow.template.vo.spring.SpringConvertHelper;
8-
import com.easycodingnow.template.vo.spring.SpringMvcClass;
99
import com.easycodingnow.utils.IOUtils;
10+
import org.apache.commons.logging.Log;
11+
import org.apache.commons.logging.LogFactory;
1012

1113
import java.io.File;
1214
import java.io.FileInputStream;
@@ -22,72 +24,93 @@
2224
* @since 2018/3/21
2325
*/
2426
public class GenDoc {
25-
27+
private static final Log logger = LogFactory.getLog(GenDoc.class);
2628

2729
public static void gen(GenConfig genConfig){
28-
String[] sourcePathRoot = genConfig.getSourcePathRoot();
30+
31+
if(!genConfig.validate()){
32+
//配置校验
33+
return;
34+
}
35+
36+
List<String> sourcePathRoot = genConfig.getSourcePathRoot();
37+
38+
String outPutPath = genConfig.getOutputPath();
2939

3040
List<String> packageList = genConfig.getScanPackages();
3141

32-
List<SpringMvcClass> springMvcClass = new ArrayList<SpringMvcClass>();
42+
List<DocApiApiClass> docClassList = new ArrayList<DocApiApiClass>();
3343

44+
//扫描解析
3445
for(String path:sourcePathRoot){
35-
3646
for(String pk:packageList){
3747
String absPath = path+"/"+pk.replaceAll("\\.", "/");
38-
List<String> fileList = getJavaFileList(absPath);
39-
48+
List<String> fileList = IOUtils.getJavaFileList(absPath);
4049
try {
4150
for(String filePath:fileList){
4251
Class cls = Parse.parse(new FileInputStream(filePath), sourcePathRoot);
43-
if(cls.getAnnotationByName("Controller") != null || cls.getAnnotationByName("RestController") != null){
44-
if(!cls.ignore()){
45-
springMvcClass.add(SpringConvertHelper.convertToSpringMvcClass(cls));
52+
53+
if(GenConfig.WebType.SPRING_MVC.equals(genConfig.getWebType())){
54+
DocApiApiClass docClass = parseSpringMvcClass(cls);
55+
if(docClass != null){
56+
docClassList.add(docClass);
4657
}
4758
}
59+
60+
4861
}
4962
} catch (FileNotFoundException e) {
50-
e.printStackTrace();
63+
logger.error("file not found!", e);
5164
}
5265
}
5366
}
5467

5568

69+
if(GenConfig.OutPutType.HTML.equals(genConfig.getOutPutType())){
70+
//生成html
71+
genHtml(docClassList, outPutPath);
72+
}
73+
}
5674

57-
Map<String, Object> classList = new HashMap<String, Object>();
58-
classList.put("jsonData", JSON.toJSONString(springMvcClass));
59-
classList.put("classList", springMvcClass);
60-
61-
try {
62-
String html = TemplateParse.parseTemplate("html/index.ftl", classList);
63-
IOUtils.copy(html, new FileOutputStream("/Users/leeco/Desktop/text.html"));
64-
} catch (Exception e) {
65-
e.printStackTrace();
75+
private static DocApiApiClass parseSpringMvcClass(Class cls){
76+
if(cls.getAnnotationByName("Controller") != null || cls.getAnnotationByName("RestController") != null){
77+
if(!cls.ignore()){
78+
return SpringConvertHelper.convertToSpringMvcClass(cls);
79+
}
6680
}
81+
return null;
6782
}
6883

84+
private static void genHtml(List<DocApiApiClass> docClassList, String outPutPath){
85+
try {
6986

70-
private static List<String> getJavaFileList(String filePath) {
71-
List<String> fileList = new ArrayList<String>();
72-
File file = new File(filePath);
73-
if(file.exists()){
74-
File[] childFiles = file.listFiles();
75-
if(childFiles != null && childFiles.length > 0) {
76-
for (File childFile : childFiles) {
77-
if (childFile.isDirectory()) {
78-
fileList.addAll(getJavaFileList(childFile.getPath()));
79-
} else {
80-
String childFilePath = childFile.getPath();
81-
82-
if(childFilePath.endsWith(".java")){
83-
fileList.add(childFilePath);
84-
}
85-
}
86-
}
87+
Map<String, Object> mapData = new HashMap<String, Object>();
88+
mapData.put("jsonData", JSON.toJSONString(docClassList));
89+
mapData.put("classList", docClassList);
90+
91+
String html = TemplateParse.parseTemplate("html/index.ftl", mapData);
92+
File fileDir = new File(outPutPath);
93+
File outFile = new File(outPutPath+"/doc.html");
94+
95+
boolean resultFlag = true;
96+
if(!fileDir.exists()){
97+
resultFlag = fileDir.mkdirs();
8798
}
88-
}
8999

90-
return fileList;
100+
if(!outFile.exists()){
101+
resultFlag = outFile.createNewFile();
102+
}
103+
104+
if(!resultFlag){
105+
throw new Exception("create outFile `"+outFile+"` failure! Please ensure that the files exist and have read and write permissions.");
106+
}
107+
108+
IOUtils.copy(html, new FileOutputStream(outFile));
109+
} catch (Exception e) {
110+
logger.error("parsing failure!", e);
111+
}
91112
}
92113

114+
115+
93116
}

gendoc-core/src/main/java/com/easycodingnow/Main.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

gendoc-core/src/main/java/com/easycodingnow/parse/Parse.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import java.util.List;
2222

2323
/**
24-
* Created by lihao on 2018/3/8.
24+
* @author lihao
25+
* @since 2018/3/8
2526
*/
2627
public class Parse {
2728

2829
private static final Log logger = LogFactory.getLog(Parse.class);
2930

30-
public static Class parse(String fullyName, String[] sourceRoot){
31+
public static Class parse(String fullyName, List<String> sourceRoot){
3132

3233
for(String sr:sourceRoot){
3334
String absPath = sr+"/"+fullyName.replaceAll("\\.", "/")+".java";
@@ -36,7 +37,7 @@ public static Class parse(String fullyName, String[] sourceRoot){
3637
try {
3738
return parse(new FileInputStream(file), sourceRoot);
3839
} catch (FileNotFoundException e) {
39-
//
40+
logger.error("file not found!", e);
4041
}
4142
}
4243
}
@@ -46,12 +47,12 @@ public static Class parse(String fullyName, String[] sourceRoot){
4647
return null;
4748
}
4849

49-
public static Class parse(InputStream inputStream, String[] sourceRoot){
50+
public static Class parse(InputStream inputStream, List<String> sourceRoot){
5051
CompilationUnit cu = JavaParser.parse(inputStream);
5152
return innerParse(cu, sourceRoot);
5253
}
5354

54-
private static Class innerParse(CompilationUnit cu, final String[] sourceRoot){
55+
private static Class innerParse(CompilationUnit cu, final List<String> sourceRoot){
5556
final Class cls = new Class();
5657

5758
cu.accept(new VoidVisitorAdapter<Void>(){
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package com.easycodingnow.reflect;
22

3-
import lombok.Data;
43

54
/**
65
* @author lihao
76
* @since 2018/3/8
87
*/
9-
@Data
108
public abstract class Annotation {
119

1210
private Member member;
1311

1412
private String name;
1513

1614

15+
public Member getMember() {
16+
return member;
17+
}
18+
19+
public void setMember(Member member) {
20+
this.member = member;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
1730
}

0 commit comments

Comments
 (0)