Skip to content
211 changes: 123 additions & 88 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.github.eerohele.dita-ot-gradle' version '0.7.1'
id 'io.github.jyjeanne.dita-ot-gradle' version '2.3.0'
id 'com.github.eerohele.saxon-gradle' version '0.9.0-beta4'
}

Expand All @@ -18,171 +18,206 @@ dependencies {
saxon 'net.sf.saxon:Saxon-HE:10.6'
}

import com.github.eerohele.DitaOtTask
import com.github.jyjeanne.DitaOtTask
import com.github.eerohele.SaxonXsltTask
import org.gradle.process.ExecOperations

def getPropertyOrDefault(String name, def defaultValue) {
hasProperty(name) ? findProperty(name) : defaultValue
providers.gradleProperty(name).getOrElse(defaultValue)
}

String ditaHome = getPropertyOrDefault('ditaHome', projectDir.getParent())
// 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 = "${projectDir}/platform.ditaval"
Boolean toolkitBuild = file("${projectDir}/../lib/dost.jar").exists()
String samplesDir = toolkitBuild ? "${ditaHome}/docsrc/samples" : "${projectDir}/samples"
String outputDir = getPropertyOrDefault('outputDir', toolkitBuild ? "${ditaHome}/doc" : "${projectDir}/out")
String ditavalFile = "${projectDirPath}/platform.ditaval"
Boolean toolkitBuild = file("${projectDirPath}/../lib/dost.jar").exists()
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()
}

ditaOt.dir ditaHome

task messages(type: SaxonXsltTask) {
input "${configDir}/messages.xml"
output "${projectDir}/topics/error-messages.xml"
stylesheet "${projectDir}/resources/messages.xsl"
output "${projectDirPath}/topics/error-messages.xml"
stylesheet "${projectDirPath}/resources/messages.xsl"
}

task params(type: SaxonXsltTask) {
input "${configDir}/plugins.xml"
output "${projectDir}/parameters/all-parameters.dita"
stylesheet "${projectDir}/resources/params.xsl"
output "${projectDirPath}/parameters/all-parameters.dita"
stylesheet "${projectDirPath}/resources/params.xsl"
parameters('output-dir.url': toURI('parameters'))
outputs.dir "${projectDir}/parameters"
outputs.dir "${projectDirPath}/parameters"
}

task extensionPoints(type: SaxonXsltTask) {
input "${configDir}/plugins.xml"
output "${projectDir}/extension-points/all-extension-points.dita"
stylesheet "${projectDir}/resources/extension-points.xsl"
output "${projectDirPath}/extension-points/all-extension-points.dita"
stylesheet "${projectDirPath}/resources/extension-points.xsl"
parameters('output-dir.url': toURI('extension-points'))
outputs.dir "${projectDir}/extension-points"
outputs.dir "${projectDirPath}/extension-points"
}

task generatePlatformFilter {
ant.condition(property: 'platform', value: 'windows') {
os(family: 'windows')
}

ant.condition(property: 'platform', value: 'mac' ) {
os(family: 'mac')
}

ant.condition(property: 'platform', value: 'unix' ) {
os(family: 'unix')
}
def outputFile = layout.projectDirectory.file(ditavalFile)
outputs.file(outputFile)

ant.echoxml(file: ditavalFile) {
val {
prop(action: 'include', att: 'platform', val: platform)
prop(action: 'exclude', att: 'platform')
doLast {
// Use Gradle's built-in OS detection instead of Ant
def platformName = 'unix' // default
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
platformName = 'windows'
} else if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
platformName = 'mac'
}

// 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: SaxonXsltTask) {
input "${configDir}/plugins.xml"
output "${samplesDir}/properties/template.properties"
stylesheet "${projectDir}/resources/properties-file.xsl"
stylesheet "${projectDirPath}/resources/properties-file.xsl"
}

task autoGenerate(dependsOn: [messages, params, extensionPoints, generatePlatformFilter, generatePropertiesTemplate]) {
description 'Run tasks that generate content from resource files and the build environment.'
description = 'Run tasks that generate content from resource files and the build environment.'
}

task pdf(type: DitaOtTask, dependsOn: autoGenerate) {
input "${projectDir}/userguide-book.ditamap"
// Set DITA-OT directory: pass as parameter -PditaHome or fall back to parent when run in core repo.
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide-book.ditamap"
output outputDir
transtype 'pdf'
filter "${projectDir}/resources/pdf.ditaval"

properties {
property(name: 'args.chapter.layout', value: 'BASIC')
property(name: 'args.gen.task.lbl', value: 'YES')
property(name: 'include.rellinks', value: '#default external')
property(name: 'outputFile.base', value: 'userguide')
property(name: 'theme', value: "${projectDir}/samples/themes/dita-ot-docs-theme.yaml")
}
filter "${projectDirPath}/resources/pdf.ditaval"

// Use ditaProperties MapProperty directly for v2.3.0 compatibility
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) {
input "${projectDir}/userguide.ditamap"
// Set DITA-OT directory: pass as parameter -PditaHome or fall back to parent when run in core repo.
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide.ditamap"
output outputDir
transtype 'html5'
filter "${projectDir}/resources/html.ditaval"

properties {
property(name: 'args.copycss', value: 'yes')
property(name: 'args.css', value: 'dita-ot-doc.css')
property(name: 'args.csspath', value: 'css')
property(name: 'args.cssroot', value: "${projectDir}/resources/")
property(name: 'args.gen.task.lbl', value: 'YES')
property(name: 'args.hdr', value: "${projectDir}/resources/header.xml")
property(name: 'args.rellinks', value: 'noparent')
property(name: 'html5.toc.generate', value: 'no')
property(name: 'nav-toc', value: 'partial')
}
filter "${projectDirPath}/resources/html.ditaval"

// Use ditaProperties MapProperty directly for v2.3.0 compatibility
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) {
input "${projectDir}/userguide.ditamap"
// Set DITA-OT directory: pass as parameter -PditaHome or fall back to parent when run in core repo.
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input "${projectDirPath}/userguide.ditamap"
output outputDir
transtype 'htmlhelp'
filter ditavalFile

properties {
property(name: 'args.copycss', value: 'yes')
property(name: 'args.css', value: 'dita-ot-doc.css')
property(name: 'args.csspath', value: 'css')
property(name: 'args.cssroot', value: "${projectDir}/resources/")
property(name: 'args.gen.task.lbl', value: 'YES')
}
// Use ditaProperties MapProperty directly for v2.3.0 compatibility
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 {
ant.move(todir: outputDir, failonerror: 'no') {
fileset(dir: "${outputDir}/htmlhelp", includes: '*.chm')
// 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
}

ant.delete(dir: "${outputDir}/htmlhelp")
}
}

task cleanUp {
doLast {
ant.delete(dir: outputDir)
delete outputDir
}
}

def commit = new ByteArrayOutputStream()
// Get git commit hash at configuration time for tasks that need it
def getGitCommitHash() {
try {
def result = new ByteArrayOutputStream()
exec {
workingDir = layout.projectDirectory.asFile
commandLine 'git', 'rev-parse', 'HEAD'
standardOutput = result
ignoreExitValue = true
}
return result.toString().trim()
} catch (Exception e) {
logger.warn("Could not get git commit hash: ${e.message}")
return 'unknown'
}
}

// Store git commit for use by tasks
def gitCommitHash = getGitCommitHash()

task gitMetadata {
doLast {
exec {
workingDir = projectDir
commandLine 'git'
args = ['rev-parse', 'HEAD']
standardOutput = commit
// This task just logs the git commit for reference
doLast {
logger.info("Git commit: ${gitCommitHash}")
}
}

// Mark outputs to help with up-to-date checking
outputs.upToDateWhen { false } // Always run since git commit changes frequently
}

task site(type: DitaOtTask) {
dependsOn 'messages', 'params', 'extensionPoints', 'gitMetadata'

input file("${projectDir}/site.ditamap")
// Set DITA-OT directory: pass as parameter -PditaHome or fall back to parent when run in core repo.
ditaOt file(findProperty('ditaHome') ?: ditaHome)
input file("${projectDirPath}/site.ditamap")
output getPropertyOrDefault('outputDir', "${buildDir}/site")
filter "${projectDir}/resources/site.ditaval"
filter "${projectDirPath}/resources/site.ditaval"

transtype 'org.dita-ot.html'

properties {
property(name: 'args.gen.task.lbl', value: 'YES')
property(name: 'args.rellinks', value: 'noparent')
if (!(project.hasProperty('noCommitMeta') && Boolean.parseBoolean(project.property('noCommitMeta')))) {
property(name: 'commit', value: commit)
}
// Evaluate the noCommitMeta flag at configuration time
def includeCommitMeta = !providers.gradleProperty('noCommitMeta').map { Boolean.parseBoolean(it) }.getOrElse(false)

// Use ditaProperties MapProperty directly for v2.3.0 compatibility
ditaProperties.put('args.gen.task.lbl', 'YES')
ditaProperties.put('args.rellinks', 'noparent')
if (includeCommitMeta) {
// Use the git commit hash obtained at configuration time
ditaProperties.put('commit', gitCommitHash)
}
}

Expand Down
8 changes: 8 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# Fix Java memory errors with Gradle 5.2
org.gradle.jvmargs = -Xmx1024m

# Gradle 8 features for better performance and caching
# ↓ Not supported by eerohele/dita-ot-gradle ↓
# org.gradle.configuration-cache=true
# org.gradle.configuration-cache.problems=warn
# ↑ Not supported by eerohele/dita-ot-gradle ↑
org.gradle.parallel=true
org.gradle.caching=true