16
16
17
17
package au.com.ish.docs
18
18
19
- import au.com.ish.docs.generator.DSLGenerator
20
- import au.com.ish.docs.generator.chapter.ChapterDSLGenerator
21
- import au.com.ish.docs.generator.root.SectionDSLGenerator
19
+ import au.com.ish.docs.handlebars.EmptyTemplate
20
+ import au.com.ish.docs.handlebars.HandlebarsContext
21
+ import au.com.ish.docs.generator.chapter.ChapterContext
22
+ import au.com.ish.docs.generator.root.SectionContext
23
+ import au.com.ish.docs.helpers.FileHelper
22
24
import au.com.ish.docs.utils.GroovyDocUtils
25
+ import com.github.jknack.handlebars.Options
23
26
import groovyjarjarantlr.RecognitionException
24
27
import groovyjarjarantlr.TokenStreamException
25
28
import org.codehaus.groovy.groovydoc.GroovyClassDoc
@@ -30,35 +33,31 @@ import org.codehaus.groovy.tools.groovydoc.OutputTool
30
33
import org.gradle.api.Project
31
34
import org.gradle.api.file.FileCollection
32
35
33
- import java.nio.charset.Charset
34
36
/**
35
37
* This is external interface to the entire tool. First we instantiate the class with the templates
36
38
* and some properties. Then we add all the files we want to document (which causes them to be
37
39
* immediately parsed). And then finally we generate the documentation by rendering the groovy templates.
38
40
*/
39
41
class DslGroovyDocTool {
40
42
41
- def log
43
+ def log
42
44
43
- private final DslGroovyRootDocBuilder rootDocBuilder
44
- private final OutputTool output = new FileOutputTool ()
45
- Project project
45
+ private Project project
46
+ private final DslGroovyRootDocBuilder rootDocBuilder
47
+ private final OutputTool output = new FileOutputTool ()
46
48
47
- private Configuration configuration
48
-
49
- /**
49
+ /**
50
50
* Let's set everything up.
51
51
*
52
52
* @param docTemplate Top level index template for the entire document.
53
53
* @param classTemplate A template per class.
54
54
* @param links Some external links to other javadocs we might want to link to
55
55
*/
56
- DslGroovyDocTool (Configuration configuration , List<LinkArgument > links , Project project ) {
56
+ DslGroovyDocTool (List<LinkArgument > links , Project project ) {
57
57
this . project = project
58
- this . log = project. logger
58
+ this . log = project. logger
59
59
this . rootDocBuilder = new DslGroovyRootDocBuilder (this , links)
60
- this . configuration = configuration;
61
- }
60
+ }
62
61
63
62
/**
64
63
* Adding the files to the tool causes them to be parsed by the antlr based groovy or java parser
@@ -87,49 +86,70 @@ class DslGroovyDocTool {
87
86
*/
88
87
void renderToOutput (String destdir ) throws Exception {
89
88
GroovyRootDoc rootDoc = rootDocBuilder. resolve()
90
- rootDocBuilder. mergeMixins()
89
+ rootDocBuilder. mergeMixins()
91
90
92
- // only output classes with @API annotation
93
- def classes = rootDoc. classes(). findAll { GroovyDocUtils . isVisible(it) }. toList()
94
- // rootDocBuilder.mergeTraits(classes)
91
+ // only output classes with @API annotation
92
+ def classes = rootDoc. classes(). findAll { GroovyDocUtils . isVisible(it) }. toList()
93
+ rootDocBuilder. mergeTraits(classes)
95
94
96
- writeClasses(classes, destdir)
97
- writeRoot(classes, project, destdir)
95
+ writeClasses(classes, destdir)
96
+ writeRoot(classes, project, destdir)
98
97
99
- // clean up by deleting all the empty folders
100
- def emptyDirs = []
98
+ // clean up by deleting all the empty folders
99
+ def emptyDirs = []
100
+ project. fileTree(dir : destdir). visit { v ->
101
+ File f = v. file
101
102
102
- project. fileTree(dir : destdir). visit { v ->
103
- File f = v. file
103
+ if (f. isDirectory() ) {
104
+ def children = project. fileTree(f). filter { f. isFile() }. files
105
+ if (children. size() == 0 ) {
106
+ emptyDirs << f
107
+ }
108
+ }
109
+ }
104
110
105
- if (f. isDirectory() ) {
106
- def children = project. fileTree(f). filter { f. isFile() }. files
107
- if (children. size() == 0 ) {
108
- emptyDirs << f
109
- }
110
- }
111
- }
111
+ // reverse so that we do the deepest folders first
112
+ emptyDirs. reverseEach { it. delete() }
112
113
113
- // reverse so that we do the deepest folders first
114
- emptyDirs. reverseEach { it. delete() }
114
+ }
115
115
116
+ void cleanUpDirectory (String destDir ) {
117
+ File dest = new File (destDir)
118
+ if (dest. exists()) {
119
+ dest. deleteDir()
120
+ }
116
121
}
117
122
118
123
private void writeClasses (Collection<GroovyClassDoc > classes , String destdir ) throws Exception {
119
- DSLGenerator generator = new ChapterDSLGenerator ()
120
- generator. generate(classes, destdir)
124
+ classes. each { classDoc ->
125
+ def context = new ChapterContext.Builder ()
126
+ .setClassDoc(classDoc)
127
+ .setClasses(classes)
128
+ .setDistDir(destdir)
129
+ .setProject(project)
130
+ .setOutput(output)
131
+ .setProject(project)
132
+ .build()
133
+
134
+ def _context = new HandlebarsContext ([" root" : context])
135
+ def options = new Options (null , null , null , _context, new EmptyTemplate (" index.md" ), null , null , null , [])
136
+ FileHelper . renderChapter(classDoc, options)
137
+ }
121
138
}
122
139
123
- private void writeRoot (Collection<GroovyClassDoc > classes , Project project , String destdir ) throws Exception {
124
- // todo
125
- String path = destdir + " /index.md"
126
- output. makeOutputArea(destdir)
127
- new File (path). createNewFile()
128
-
129
- SectionDSLGenerator sectionDSLGenerator = new SectionDSLGenerator (output, project, destdir)
130
- def renderedSrc = sectionDSLGenerator. generate(classes, configuration. roomTemplate)
131
-
132
- output. writeToOutput(path, renderedSrc, Charset . defaultCharset(). name())
133
- }
140
+ private void writeRoot (Collection<GroovyClassDoc > classes , Project project , String destdir ) throws Exception {
141
+ String path = destdir + " /index.md"
142
+ def context = new SectionContext.Builder ()
143
+ .setClasses(classes)
144
+ .setDistDir(destdir)
145
+ .setProject(project)
146
+ .setOutput(output)
147
+ .setProject(project)
148
+ .build()
149
+
150
+ def _context = new HandlebarsContext ([" root" : context])
151
+ def options = new Options (null , null , null , _context, new EmptyTemplate (" index.md" ), null , null , null , [])
152
+ FileHelper . render(path, options)
153
+ }
134
154
135
155
}
0 commit comments