-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathbuild.gradle
More file actions
303 lines (255 loc) · 10.4 KB
/
build.gradle
File metadata and controls
303 lines (255 loc) · 10.4 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.sf.saxon:Saxon-HE:12.9'
}
}
plugins {
id 'io.github.jyjeanne.dita-ot-gradle' version '2.8.5'
}
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.tasks.*
import net.sf.saxon.s9api.Processor
import net.sf.saxon.s9api.QName
import net.sf.saxon.s9api.XdmAtomicValue
import javax.xml.transform.stream.StreamSource
/**
* Gradle 9 compatible XSLT transformation task using Saxon-HE.
* Replaces eerohele/saxon-gradle, which is not configuration cache compatible.
*/
@CacheableTask
abstract class XsltTransformTask extends DefaultTask {
@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
abstract RegularFileProperty getInputFile()
@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
abstract RegularFileProperty getStylesheetFile()
@OutputFile
abstract RegularFileProperty getOutputFile()
@Input
@Optional
abstract MapProperty<String, String> getParameters()
@TaskAction
void transform() {
def inputPath = inputFile.get().asFile
def stylesheetPath = stylesheetFile.get().asFile
def outputPath = outputFile.get().asFile
try {
outputPath.parentFile?.mkdirs()
def processor = new Processor(false)
def compiler = processor.newXsltCompiler()
def executable = compiler.compile(new StreamSource(stylesheetPath))
def transformer = executable.load30()
def params = parameters.getOrElse([:])
if (!params.isEmpty()) {
params.each { name, value ->
transformer.setStylesheetParameters(
Collections.singletonMap(new QName(name), new XdmAtomicValue(value))
)
}
}
transformer.transform(new StreamSource(inputPath), processor.newSerializer(outputPath))
logger.lifecycle("Generated: ${outputPath.name}")
} catch (Exception e) {
throw new GradleException("XSLT transformation failed: ${inputPath.name} -> ${outputPath.name}\n" +
"Stylesheet: ${stylesheetPath.name}\n" +
"Cause: ${e.message}", e)
}
}
// DSL methods for backward compatibility with saxon-gradle syntax
// Using layout API for configuration cache compatibility
void input(Object path) {
inputFile.set(project.layout.projectDirectory.file(path.toString()))
}
void output(Object path) {
outputFile.set(project.layout.projectDirectory.file(path.toString()))
}
void stylesheet(Object path) {
stylesheetFile.set(project.layout.projectDirectory.file(path.toString()))
}
void parameters(Map<String, String> params) {
parameters.putAll(params)
}
}
import com.github.jyjeanne.DitaOtTask
def getPropertyOrDefault(String name, def defaultValue) {
providers.gradleProperty(name).getOrElse(defaultValue)
}
// Use layout.projectDirectory for configuration cache compatibility
def projectDirPath = layout.projectDirectory.asFile.path
String ditaHome = getPropertyOrDefault('ditaHome', layout.projectDirectory.asFile.getParent())
String ditaHomeSrc = getPropertyOrDefault('ditaHomeSrc', ditaHome)
String configDir = "${ditaHomeSrc}/config"
String ditavalFile = "${projectDirPath}/platform.ditaval"
// Defer file existence check for configuration cache compatibility
Boolean toolkitBuild = providers.provider {
file("${projectDirPath}/../lib/dost.jar").exists()
}.get()
String samplesDir = toolkitBuild ? "${ditaHome}/docsrc/samples" : "${projectDirPath}/samples"
String outputDir = getPropertyOrDefault('outputDir', toolkitBuild ? "${ditaHome}/doc" : "${projectDirPath}/out")
String toURI(String path) {
file(path).toURI().toString()
}
task messages(type: XsltTransformTask) {
group = 'generation'
description = 'Generate error messages documentation from DITA-OT config.'
input "${configDir}/messages.xml"
output "${projectDirPath}/topics/error-messages.xml"
stylesheet "${projectDirPath}/resources/messages.xsl"
}
task params(type: XsltTransformTask) {
group = 'generation'
description = 'Generate parameters documentation from DITA-OT plugins config.'
input "${configDir}/plugins.xml"
output "${projectDirPath}/parameters/all-parameters.dita"
stylesheet "${projectDirPath}/resources/params.xsl"
parameters('output-dir.url': toURI('parameters'))
outputs.dir "${projectDirPath}/parameters"
}
task extensionPoints(type: XsltTransformTask) {
group = 'generation'
description = 'Generate extension points documentation from DITA-OT plugins config.'
input "${configDir}/plugins.xml"
output "${projectDirPath}/extension-points/all-extension-points.dita"
stylesheet "${projectDirPath}/resources/extension-points.xsl"
parameters('output-dir.url': toURI('extension-points'))
outputs.dir "${projectDirPath}/extension-points"
}
task generatePlatformFilter {
group = 'generation'
description = 'Generate platform-specific DITAVAL filter file.'
def outputFile = layout.projectDirectory.file(ditavalFile)
outputs.file(outputFile)
doLast {
// Use System properties for OS detection (public API)
def osName = System.getProperty('os.name').toLowerCase()
def platformName = osName.contains('win') ? 'windows' :
osName.contains('mac') ? 'mac' : 'unix'
// Generate the ditaval file using modern Gradle file operations
outputFile.asFile.text = """<?xml version="1.0" encoding="UTF-8"?>
<val>
<prop action="include" att="platform" val="${platformName}"/>
<prop action="exclude" att="platform"/>
</val>
"""
}
}
task generatePropertiesTemplate(type: XsltTransformTask) {
group = 'generation'
description = 'Generate properties template file from DITA-OT plugins config.'
input "${configDir}/plugins.xml"
output "${samplesDir}/properties/template.properties"
stylesheet "${projectDirPath}/resources/properties-file.xsl"
}
task autoGenerate(dependsOn: [messages, params, extensionPoints, generatePlatformFilter, generatePropertiesTemplate]) {
group = 'generation'
description = 'Run tasks that generate content from resource files and the build environment.'
}
task pdf(type: DitaOtTask, dependsOn: autoGenerate) {
group = 'documentation'
description = 'Build PDF documentation.'
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide-book.ditamap"
output outputDir
transtype 'pdf'
filter "${projectDirPath}/resources/pdf.ditaval"
ditaProperties.put('args.chapter.layout', 'BASIC')
ditaProperties.put('args.gen.task.lbl', 'YES')
ditaProperties.put('include.rellinks', '#default external')
ditaProperties.put('outputFile.base', 'userguide')
ditaProperties.put('theme', "${projectDirPath}/samples/themes/dita-ot-docs-theme.yaml")
}
task html(type: DitaOtTask, dependsOn: autoGenerate) {
group = 'documentation'
description = 'Build HTML5 documentation.'
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide.ditamap"
output outputDir
transtype 'html5'
filter "${projectDirPath}/resources/html.ditaval"
ditaProperties.put('args.copycss', 'yes')
ditaProperties.put('args.css', 'dita-ot-doc.css')
ditaProperties.put('args.csspath', 'css')
ditaProperties.put('args.cssroot', "${projectDirPath}/resources/")
ditaProperties.put('args.gen.task.lbl', 'YES')
ditaProperties.put('args.hdr', "${projectDirPath}/resources/header.xml")
ditaProperties.put('args.rellinks', 'noparent')
ditaProperties.put('html5.toc.generate', 'no')
ditaProperties.put('nav-toc', 'partial')
}
task htmlhelp(type: DitaOtTask, dependsOn: autoGenerate) {
group = 'documentation'
description = 'Build HTML Help (.chm) documentation.'
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide.ditamap"
output outputDir
transtype 'htmlhelp'
filter ditavalFile
ditaProperties.put('args.copycss', 'yes')
ditaProperties.put('args.css', 'dita-ot-doc.css')
ditaProperties.put('args.csspath', 'css')
ditaProperties.put('args.cssroot', "${projectDirPath}/resources/")
ditaProperties.put('args.gen.task.lbl', 'YES')
doLast {
// Move .chm files using modern Gradle file operations
def htmlhelpDir = file("${outputDir}/htmlhelp")
if (htmlhelpDir.exists()) {
copy {
from htmlhelpDir
into outputDir
include '*.chm'
}
// Clean up the htmlhelp directory
delete htmlhelpDir
}
}
}
task cleanOutput(type: Delete) {
group = 'build'
description = 'Delete the output directory.'
delete file(outputDir)
}
// Get Git commit hash using Gradle 9 compatible Provider API
def gitCommitHash = providers.exec {
commandLine 'git', 'rev-parse', 'HEAD'
ignoreExitValue = true
}.standardOutput.asText.map { it.trim() }.getOrElse('unknown')
task gitMetadata {
group = 'build'
description = 'Log git commit metadata.'
doLast {
logger.lifecycle("Git commit: ${gitCommitHash}")
}
outputs.upToDateWhen { false }
}
task site(type: DitaOtTask) {
group = 'documentation'
description = 'Build website documentation.'
dependsOn 'messages', 'params', 'extensionPoints', 'gitMetadata'
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input file("${projectDirPath}/site.ditamap")
output getPropertyOrDefault('outputDir', layout.buildDirectory.dir("site").get().asFile.path)
filter "${projectDirPath}/resources/site.ditaval"
transtype 'org.dita-ot.html'
// Evaluate the noCommitMeta flag at configuration time
def includeCommitMeta = !providers.gradleProperty('noCommitMeta').map { Boolean.parseBoolean(it) }.getOrElse(false)
ditaProperties.put('args.gen.task.lbl', 'YES')
ditaProperties.put('args.rellinks', 'noparent')
if (includeCommitMeta) {
ditaProperties.put('commit', gitCommitHash)
}
}
task all(dependsOn: [pdf, html, htmlhelp]) {
group = 'documentation'
description = 'Build all documentation formats (PDF, HTML, HTMLHelp).'
}
task dist(dependsOn: [pdf, html]) {
group = 'documentation'
description = 'Build distribution documentation (PDF and HTML).'
}
defaultTasks 'dist'