-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.gradle
108 lines (97 loc) · 4.79 KB
/
build.gradle
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
import freemarker.template.TemplateExceptionHandler
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import static groovy.io.FileType.FILES
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.3/samples
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.freemarker:freemarker:2.3.32")
classpath("org.yaml:snakeyaml:2.2")
}
}
tasks.register("generate") {
doLast {
def sortByName = { a, b ->
a.name <=> b.name
}
LoaderOptions loaderOptions = new LoaderOptions()
Yaml yaml = new Yaml(loaderOptions)
var workingDir = "${rootProject.projectDir.absolutePath}"
freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_32)
cfg.setDirectoryForTemplateLoading(new File("$workingDir/tech/templates"))
cfg.setDefaultEncoding("UTF-8")
var baseTechTemplate = cfg.getTemplate("template-index.ftl")
//update mkdocs file by getting the 'nav:' section and writing the updated site structure
var baseNavSection = "nav:\n - Home: 'index.md'\n"
new File("$workingDir/tech").eachFileRecurse(groovy.io.FileType.DIRECTORIES) { dir ->
if (dir.name != "templates") {
var tech = dir.name
var cleanTechName = tech.replace("_", " ").capitalize()
var baseDir = new File("tech/$tech")
var root = new HashMap()
var innerTechTypes = new ArrayList()
baseNavSection += " - $cleanTechName:\n - '$tech/index.md'\n"
baseDir.traverse(type: FILES, sort: sortByName) { file ->
var parsedYaml = yaml.load(file.getText())
println("Processing: ${parsedYaml["name"]}")
root.put(parsedYaml["name"], parsedYaml)
innerTechTypes.add(parsedYaml["name"])
}
root.put("high_level_tech_name", tech)
root.put("tech_types", innerTechTypes)
// main file with all tech comparisons
var baseTechOutFile = new File("$workingDir/docs/$tech/index.md")
if (!baseTechOutFile.parentFile.exists()) {
baseTechOutFile.parentFile.mkdirs()
}
if (!baseTechOutFile.exists()) {
baseTechOutFile.createNewFile()
}
var out = new FileWriter(baseTechOutFile)
baseTechTemplate.process(root, out)
out.close()
// sub files with comparison with each pair
innerTechTypes.forEach(techType -> {
baseNavSection += " - $techType:\n"
innerTechTypes.forEach(techType2 -> {
var techTypeName = techType.toLowerCase().replaceAll(" ", "_")
var techType2Name = techType2.toLowerCase().replaceAll(" ", "_")
if (techTypeName != techType2Name) {
baseNavSection += " - $techType2: '$tech/$techTypeName/${techType2Name}.md'\n"
var compareRoot = new HashMap()
compareRoot.put("compare_two_tech", true)
compareRoot.put("high_level_tech_name", tech)
compareRoot.put("tech_types", [techType, techType2])
compareRoot.put(techType, root[techType])
compareRoot.put(techType2, root[techType2])
var compareTwoOutFile = new File("$workingDir/docs/$tech/$techTypeName/${techType2Name}.md")
if (!compareTwoOutFile.parentFile.exists()) {
compareTwoOutFile.parentFile.mkdirs()
}
if (!compareTwoOutFile.exists()) {
compareTwoOutFile.createNewFile()
}
var compareOut = new FileWriter(compareTwoOutFile)
baseTechTemplate.process(compareRoot, compareOut)
compareOut.close()
}
})
})
}
}
//update mkdocs file nav section with baseNavSection that will have updated site structure
var mkdocsFile = new File("$workingDir/mkdocs.yml")
var updatedMkdocs = mkdocsFile.text.replaceAll(/(?ms)nav:.*/, baseNavSection)
var mkdocsOut = new FileWriter(mkdocsFile)
mkdocsOut.write(updatedMkdocs)
mkdocsOut.close()
}
}