forked from grails/grails-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
239 lines (195 loc) · 8.78 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
apply plugin: "base"
version = System.getProperty("grails.version") ?: "1.4.0.BUILD-SNAPSHOT"
archivesBaseName = "grails-docs"
checkOutDir = "${buildDir.path}/checkout"
outputDir = "${buildDir.path}/docs"
explicitGrailsHome = System.getProperty("grails.home")
grailsHome = explicitGrailsHome ? file(explicitGrailsHome).absolutePath : "$checkOutDir/grails-src"
configurations {
publish
}
repositories {
mavenRepo urls: "http://repo.grails.org/grails/core"
mavenCentral()
mavenRepo urls: "http://snapshots.repository.codehaus.org/"
}
dependencies {
// Required while picking up grails-docs from dist dir.
publish "org.grails:grails-gdoc-engine:1.0.1", "org.codehaus.groovy:groovy:1.8.0"
publish "org.slf4j:slf4j-log4j12:1.5.8",
"org.xhtmlrenderer:core-renderer:R8",
"org.yaml:snakeyaml:1.8",
"commons-lang:commons-lang:2.4"
}
task fetchGrailsSource << {
ant.mkdir dir: checkOutDir
println "Downloading Grails source code. If you already have a copy " +
"of the Grails source code checked out you can avoid this download " +
"by setting the grails.home system property to point to your local " +
"copy of the source. See README.txt for more information."
def zipFile = "${checkOutDir}/grails-src.zip"
ant.get src: "http://github.com/grails/grails-core/zipball/master", dest: zipFile, verbose: true
ant.unzip src: zipFile, dest: checkOutDir, {
mapper type: "regexp", from: "(grails-grails-core-\\S*?/)(.*)", to: "grails-src/\\2"
}
println "Grails source code has been downloaded to ${relativePath(grailsHome)}"
}
fetchGrailsSource.onlyIf { !explicitGrailsHome }
task apiDocs(type: GradleBuild, dependsOn: 'fetchGrailsSource') {
def deps = [":grails-docs:install"]
if (!System.getProperty("disable.groovydocs")) {
deps << 'groovydoc'
}
tasks = deps
buildFile = "${project.grailsHome}/build.gradle"
}
task copyApiDocs(type: Copy) {
from "${project.grailsHome}/doc/api"
into "${outputDir}/api"
}
task createPublishClassLoader << {
// Create a class loader for the publication stuff.
def classpath = project.configurations.publish
classpath = classpath.files.collect { it.toURI().toURL() }
// Add the grails-docs JAR.
def distDir = project.file("${project.grailsHome}/dist")
def grailsDocJars = distDir.listFiles({ dir, name -> name ==~ /grails-docs-.*\.jar/ } as FilenameFilter)
if (grailsDocJars.size() > 1) {
throw new RuntimeException("Found multiple grails-doc JARs in ${distDir}")
}
else if (grailsDocJars.size() == 0) {
throw new RuntimeException("No grails-doc JAR found in ${distDir}")
}
classpath << grailsDocJars[0].toURI().toURL()
project.publishClassLoader = new URLClassLoader(
classpath as URL[],
buildscript.classLoader)
}
task migrate(dependsOn: ['createPublishClassLoader']) << {
def props = new Properties()
new File("${projectDir}/resources/doc.properties").withInputStream {input ->
props.load(input)
}
props = props.findAll { key, value -> key.startsWith("alias.") }.collectEntries { key, value -> [key[6..-1], value] }
def migrator = publishClassLoader.loadClass("grails.doc.LegacyDocMigrator").newInstance(
file(projectDir.path + "/src/guide"),
props)
migrator.migrate()
}
task publishGuide(type: PublishGuide, dependsOn: ['apiDocs', 'copyApiDocs', 'createPublishClassLoader']) {
// No language setting because we want the English guide to be
// generated with a 'en' in the path, but the source is in 'en'
// so that it's easy to track with git.
sourceDir = new File(projectDir, "src/en")
}
task publishPdf(type: PublishPdf, dependsOn: ['publishGuide'])
tasks.addRule("Pattern: publishGuide_<lang>") { String taskName ->
def m = taskName =~ /publishGuide_(\w+)/
if (m) {
def lang = m[0][1]
task(taskName, type: PublishGuide, dependsOn: ['apiDocs', 'copyApiDocs', 'createPublishClassLoader']) {
language = lang
}
}
}
tasks.addRule("Pattern: publishPdf_<lang>") { String taskName ->
def m = taskName =~ /publishPdf_(\w+)/
if (m) {
def lang = m[0][1]
task(taskName, type: PublishPdf, dependsOn: "publishGuide_${lang}") {
language = lang
}
}
}
task docs(dependsOn: ['publishPdf'])
for (f in new File(projectDir, "src").listFiles()) {
if (f.directory && !(f.name in ["en", "guide", "ref"])) {
docs.dependsOn << "publishPdf_${f.name}"
}
}
task dist(type: Zip, dependsOn: 'docs') {
from outputDir
}
artifacts {
archives dist
}
task wrapper(type: Wrapper) {
gradleVersion = '1.0-milestone-3'
}
class PublishGuide extends DefaultTask {
File sourceDir = new File(project.projectDir, "src")
File targetDir = project.outputDir as File
File workDir = project.buildDir as File
File resourcesDir = new File(project.projectDir, "resources")
String language
@TaskAction
def publishGuide() {
def props = new Properties()
new File("${resourcesDir}/doc.properties").withInputStream {input ->
props.load(input)
}
new File("${project.grailsHome}/build.properties").withInputStream {input ->
props.load(input)
}
def publisher = project.publishClassLoader.loadClass("grails.doc.DocPublisher").newInstance(project.file(sourceDir), project.file(targetDir))
publisher.ant = ant
publisher.workDir = workDir
publisher.apiDir = "${project.outputDir}" as File
publisher.language = language ?: ''
publisher.images = project.file("${resourcesDir}/img")
publisher.css = project.file("${resourcesDir}/css")
publisher.js = project.file("${resourcesDir}/js")
publisher.style = project.file("${resourcesDir}/style")
publisher.version = props."grails.version"
publisher.logo = '<a href="http://grails.org" target="_blank"><img alt="Grails Logo" title="The Grails Framework" src="${path}/img/grails.png" border="0"/></a>'
publisher.sponsorLogo = '<a href="http://springsource.com" target="_blank"><img alt="SpringSource Logo" title="SpringSource - Weapons for the War on Java Complexity" src="${path}/img/springsource-logo.png" border="0"/></a>'
// Override doc.properties properties with their language-specific counterparts (if
// those are defined). You just need to add entries like es.title or pt_PT.subtitle.
if (language) {
def pos = language.size() + 1
def languageProps = props.findAll { k, v -> k.startsWith("${language}.") }
languageProps.each { k, v -> props[k[pos..-1]] = v }
}
// Aliases and other doc.properties entries are passed in as engine properties. This
// is how the doc title, subtitle, etc. are set.
publisher.engineProperties = props
// Add support for displaying the source code for GSP tags.
def sourceMacro = project.publishClassLoader.loadClass("grails.doc.macros.GspTagSourceMacro")
def searchDirs = project.file(project.grailsHome).listFiles().findAll {
new File(it, "src/main/groovy/org/codehaus/groovy/grails").exists()
}.collect {
new File(it, "src/main/groovy/org/codehaus/groovy/grails")
}
// {hidden} macro for enabling translations.
def hiddenMacro = project.publishClassLoader.loadClass("grails.doc.macros.HiddenMacro")
publisher.registerMacro(sourceMacro.newInstance(searchDirs))
publisher.registerMacro(hiddenMacro.newInstance())
// Radeox loads its bundles off the context class loader, which
// unfortunately doesn't contain the grails-docs JAR. So, we
// temporarily switch the DocPublisher class loader into the
// thread so that the Radeox bundles can be found.
def oldClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = publisher.getClass().classLoader
publisher.publish()
// Restore the old context class loader.
Thread.currentThread().contextClassLoader = oldClassLoader
}
}
class PublishPdf extends DefaultTask {
String language
@TaskAction
def publish() {
try {
def baseDir = new File(project.outputDir, language ?: '')
def currFile = new File(baseDir, "guide/single.html")
def pdfBuilder = project.publishClassLoader.loadClass("grails.doc.PdfBuilder")
def xml = pdfBuilder.createXml(currFile, baseDir.absolutePath)
pdfBuilder.createPdf xml,
new File(currFile.parentFile, currFile.name[0..-5] + "pdf"),
new File(baseDir, "guide/single.html")
}
catch (Exception ex) {
ex.printStackTrace()
}
}
}