Repository navigation
Expand file tree
/
Copy pathdocs.gradle
More file actions
141 lines (121 loc) · 5.74 KB
/
Copy pathdocs.gradle
File metadata and controls
141 lines (121 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.javaparser:javaparser-core:3.26.2'
}
}
import com.github.javaparser.JavaParser
import com.github.javaparser.ParserConfiguration
import com.github.javaparser.ast.body.EnumDeclaration
import com.github.javaparser.ast.body.FieldDeclaration
import com.github.javaparser.ast.type.ClassOrInterfaceType
tasks.register('generateConfigDocs') {
group = 'documentation'
description = 'Generates CONFIG.md from Settings.java and SettingTypes.java'
def settingsFile = file("src/main/java/configured/Settings.java")
def typesFile = file("src/main/java/configured/SettingTypes.java")
def outputFile = file("CONFIG.md")
inputs.files settingsFile, typesFile
outputs.file outputFile
doLast {
def parserConfig = new ParserConfiguration()
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17)
def parser = new JavaParser(parserConfig)
def cleanComment = { commentOptional ->
if (!commentOptional.isPresent()) return ""
return commentOptional.get().asString()
.lines()
.collect { line -> line.trim().replaceAll(/^(?:\/{3}|\/\*\*|\*\/|\*)\s?/, '').trim() }
.findAll { !it.isEmpty() }
.join("\n")
}
def enumRegistry = [:] // Map<String, List<[name: String, doc: String]>>
if (typesFile.exists()) {
def parseResult = parser.parse(typesFile)
if (!parseResult.isSuccessful()) {
throw new GradleException("Failed to parse SettingTypes.java: " + parseResult.problems)
}
parseResult.result.get().findAll(EnumDeclaration.class).each { enumDecl ->
def entries = []
enumDecl.entries.each { entry ->
entries << [name: entry.nameAsString, doc: cleanComment(entry.comment)]
}
enumRegistry[enumDecl.nameAsString] = entries
}
}
def getSimpleName = { String typeName ->
return typeName.contains(".") ? typeName.substring(typeName.lastIndexOf('.') + 1) : typeName
}
def settingsParseResult = parser.parse(settingsFile)
if (!settingsParseResult.isSuccessful()) {
throw new GradleException("Failed to parse Settings.java: " + settingsParseResult.problems)
}
def settingsCu = settingsParseResult.result.get()
def md = new StringBuilder()
md.append("# Settings\n*All default values should show vanilla behavior.\nIf that is not the case, please submit a bug report.*\n\n")
settingsCu.findAll(FieldDeclaration.class).each { field ->
boolean hasConfig = field.annotations.any { it.nameAsString == 'Config' }
if (!hasConfig) return
def variable = field.variables[0]
def fieldName = variable.nameAsString
def fieldType = field.commonType
def rawType = fieldType.asString()
def fieldComment = cleanComment(field.comment)
String displayType = "unknown"
String matchedEnumName = null
String mapKeyEnumName = null
if (rawType.equalsIgnoreCase("boolean")) {
displayType = "bool"
} else if (rawType.equalsIgnoreCase("int") || rawType.equalsIgnoreCase("integer")) {
displayType = "int"
} else if (rawType.equalsIgnoreCase("String")) {
displayType = "string"
} else if (rawType.startsWith("Map") || rawType.startsWith("HashMap")) {
displayType = "map"
// Extract generic Key type from Map<KeyType, ValueType>
if (fieldType instanceof ClassOrInterfaceType && fieldType.typeArguments.isPresent()) {
def typeArgs = fieldType.typeArguments.get()
if (!typeArgs.isEmpty()) {
def keyTypeSimple = getSimpleName(typeArgs[0].asString())
if (enumRegistry.containsKey(keyTypeSimple)) {
mapKeyEnumName = keyTypeSimple
}
}
}
} else if (rawType.startsWith("List") || rawType.startsWith("ArrayList")) {
displayType = "list"
} else {
def simpleName = getSimpleName(rawType)
if (enumRegistry.containsKey(simpleName)) {
displayType = "enum"
matchedEnumName = simpleName
} else {
displayType = rawType.toLowerCase()
}
}
md.append("### ${fieldName} `${displayType}`\n")
if (!fieldComment.isEmpty()) {
md.append(fieldComment).append("\n")
}
if (matchedEnumName != null && enumRegistry.containsKey(matchedEnumName)) {
md.append("> Variants:\n")
enumRegistry[matchedEnumName].each { variant ->
def docStr = variant.doc ? " " + variant.doc.replaceAll(/\n+/, ' ') : ""
md.append("> - `${variant.name}`${docStr}\n")
}
}
if (mapKeyEnumName != null && enumRegistry.containsKey(mapKeyEnumName)) {
md.append("> Keys:\n")
enumRegistry[mapKeyEnumName].each { variant ->
def docStr = variant.doc ? " " + variant.doc.replaceAll(/\n+/, ' ') : ""
md.append("> - `${variant.name}`${docStr}\n")
}
}
md.append("\n")
}
outputFile.text = md.toString().trim() + "\n"
logger.lifecycle("Successfully generated ${outputFile.path}")
}
}