Automatically generate basic CraftTweaker documentation on build

This commit is contained in:
modmuss50 2018-01-29 21:14:08 +00:00
parent a6df9441ec
commit e711bbbedd
4 changed files with 105 additions and 3 deletions

2
Jenkinsfile vendored
View file

@ -8,7 +8,7 @@ node {
sh "rm -rf build/libs/"
sh "rm -rf .gradle/asmInjector/"
sh "chmod +x gradlew"
sh "./gradlew build uploadArchive curseTools --refresh-dependencies --stacktrace"
sh "./gradlew build mtDocGen uploadArchive curseTools --refresh-dependencies --stacktrace"
stage "Archive artifacts"

View file

@ -348,3 +348,106 @@ task curseTools {
}
}
}
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import org.apache.commons.io.IOUtils
import java.util.zip.ZipFile
//Reads the new fml_cache_annotation file built into the jar file, to find all crafttweaker methods, and saves the data to a file
task mtDocGen {
doLast{
def inputFile = new File(jar.archivePath.getAbsolutePath())
println("Reading " + inputFile.getName() + " for crafttweaker documentation")
def jarFile = new ZipFile(inputFile)
def annotation_cache = jarFile.getEntry("META-INF/fml_cache_annotation.json")
def cache_json = IOUtils.toString(jarFile.getInputStream(annotation_cache), "UTF-8")
def jsonObject = new GsonBuilder().create().fromJson(cache_json, JsonObject.class)
def builder = new StringBuilder()
for(entry in jsonObject.entrySet()){
def clzz = entry.value.asJsonObject
if(!clzz.has("annotations")){
continue
}
def annoations = clzz.get("annotations").asJsonArray
for(annotation in annoations.toList()){
if(annotation.asJsonObject.get("type").asString.equals("METHOD") && annotation.asJsonObject.get("name").asString.equals("Lstanhebben/zenscript/annotations/ZenMethod;")){
def method = sanitsiseMethodName(annotation.asJsonObject.get("target").asString)
def zen_name = getZenMetName(clzz)
builder.append(zen_name + "." + method)
builder.append("\n")
}
}
}
def outputFile = new File(jar.archivePath.getAbsolutePath().replace(".jar", "-crafttweaker.txt"))
outputFile.write builder.toString()
println("Crafttweaker documentation saved to " + outputFile.name)
}
}
//Gets the zenMethod class name from the class json object
String getZenMetName(JsonObject jsonObject){
def annoations = jsonObject.get("annotations").asJsonArray
for(annotation in annoations.toList()){
if(annotation.asJsonObject.get("type").asString.equals("CLASS") && annotation.asJsonObject.get("name").asString.equals("Lstanhebben/zenscript/annotations/ZenClass;")){
return annotation.asJsonObject.get("value").asJsonObject.get("value").asString
}
}
}
String sanitsiseMethodName(String methodSig){
def builder = new StringBuilder()
def name = methodSig.split("\\(")[0]
builder.append(name)
builder.append("(")
def methodArgs = methodSig.split("\\(")[1].split("\\)")[0].split(";")
for(arg in methodArgs){
def argSlit = arg.split("/")
def argStr = argSlit[argSlit.length -1]
//If a class is not in a package I assume its a primitive //TODO any suggestions for a better way to do this?
if(!arg.contains("/") && !arg.isEmpty()){
argStr = humanizeArg(argStr)
}
builder.append(argStr)
//Dont add the comma to the last arg
if(arg != methodArgs[methodArgs.length - 1]){
builder.append(",")
}
}
builder.append(")")
return builder.toString()
}
//Argumets that are java primitives do not use a freindly name, this method replaces them with something most people will understand
String humanizeArg(String arg){
def primitiveMap = [
Z: "Boolean",
B: "Byte",
C: "Char",
D: "Double",
F: "Float",
I: "Integer",
J: "Long",
L: "Object",
S: "Short"
]
def builder = new StringBuilder()
for(cha in arg.toCharArray()){
builder.append(primitiveMap.get(cha.toString().toUpperCase()))
builder.append(",")
}
//Removes the last ,
return builder.toString().substring(0, builder.toString().length() - 1)
}

Binary file not shown.

View file

@ -1,6 +1,5 @@
#Mon Jun 19 11:12:24 BST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-bin.zip