Skip to content

Commit e11be3b

Browse files
infotextureclaude
andcommitted
Refactor for Gradle 9 compatibility
Use Cursor’s `claude-4-sonnet` model to replace deprecated constructs from Gradle 7 and earlier versions with their Gradle 8 and 9 equivalents to quiet deprecation warnings. Co-Authored-By: Claude <[email protected]> Signed-off-by: Roger Sheen <[email protected]>
1 parent 914ca53 commit e11be3b

File tree

2 files changed

+98
-57
lines changed

2 files changed

+98
-57
lines changed

build.gradle

Lines changed: 90 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@ dependencies {
2020

2121
import com.github.eerohele.DitaOtTask
2222
import com.github.eerohele.SaxonXsltTask
23+
import org.gradle.process.ExecOperations
2324

2425
def getPropertyOrDefault(String name, def defaultValue) {
25-
hasProperty(name) ? findProperty(name) : defaultValue
26+
providers.gradleProperty(name).getOrElse(defaultValue)
2627
}
2728

28-
String ditaHome = getPropertyOrDefault('ditaHome', projectDir.getParent())
29+
// Use layout.projectDirectory for configuration cache compatibility
30+
def projectDirPath = layout.projectDirectory.asFile.path
31+
32+
String ditaHome = getPropertyOrDefault('ditaHome', layout.projectDirectory.asFile.getParent())
2933
String ditaHomeSrc = getPropertyOrDefault('ditaHomeSrc', ditaHome)
3034
String configDir = "${ditaHomeSrc}/config"
31-
String ditavalFile = "${projectDir}/platform.ditaval"
32-
Boolean toolkitBuild = file("${projectDir}/../lib/dost.jar").exists()
33-
String samplesDir = toolkitBuild ? "${ditaHome}/docsrc/samples" : "${projectDir}/samples"
34-
String outputDir = getPropertyOrDefault('outputDir', toolkitBuild ? "${ditaHome}/doc" : "${projectDir}/out")
35+
String ditavalFile = "${projectDirPath}/platform.ditaval"
36+
Boolean toolkitBuild = file("${projectDirPath}/../lib/dost.jar").exists()
37+
String samplesDir = toolkitBuild ? "${ditaHome}/docsrc/samples" : "${projectDirPath}/samples"
38+
String outputDir = getPropertyOrDefault('outputDir', toolkitBuild ? "${ditaHome}/doc" : "${projectDirPath}/out")
3539

3640
String toURI(String path) {
3741
file(path).toURI().toString()
@@ -41,93 +45,95 @@ ditaOt.dir ditaHome
4145

4246
task messages(type: SaxonXsltTask) {
4347
input "${configDir}/messages.xml"
44-
output "${projectDir}/topics/error-messages.xml"
45-
stylesheet "${projectDir}/resources/messages.xsl"
48+
output "${projectDirPath}/topics/error-messages.xml"
49+
stylesheet "${projectDirPath}/resources/messages.xsl"
4650
}
4751

4852
task params(type: SaxonXsltTask) {
4953
input "${configDir}/plugins.xml"
50-
output "${projectDir}/parameters/all-parameters.dita"
51-
stylesheet "${projectDir}/resources/params.xsl"
54+
output "${projectDirPath}/parameters/all-parameters.dita"
55+
stylesheet "${projectDirPath}/resources/params.xsl"
5256
parameters('output-dir.url': toURI('parameters'))
53-
outputs.dir "${projectDir}/parameters"
57+
outputs.dir "${projectDirPath}/parameters"
5458
}
5559

5660
task extensionPoints(type: SaxonXsltTask) {
5761
input "${configDir}/plugins.xml"
58-
output "${projectDir}/extension-points/all-extension-points.dita"
59-
stylesheet "${projectDir}/resources/extension-points.xsl"
62+
output "${projectDirPath}/extension-points/all-extension-points.dita"
63+
stylesheet "${projectDirPath}/resources/extension-points.xsl"
6064
parameters('output-dir.url': toURI('extension-points'))
61-
outputs.dir "${projectDir}/extension-points"
65+
outputs.dir "${projectDirPath}/extension-points"
6266
}
6367

6468
task generatePlatformFilter {
65-
ant.condition(property: 'platform', value: 'windows') {
66-
os(family: 'windows')
67-
}
68-
69-
ant.condition(property: 'platform', value: 'mac' ) {
70-
os(family: 'mac')
71-
}
69+
def outputFile = layout.projectDirectory.file(ditavalFile)
70+
outputs.file(outputFile)
7271

73-
ant.condition(property: 'platform', value: 'unix' ) {
74-
os(family: 'unix')
75-
}
76-
77-
ant.echoxml(file: ditavalFile) {
78-
val {
79-
prop(action: 'include', att: 'platform', val: platform)
80-
prop(action: 'exclude', att: 'platform')
72+
doLast {
73+
// Use Gradle's built-in OS detection instead of Ant
74+
def platformName = 'unix' // default
75+
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
76+
platformName = 'windows'
77+
} else if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
78+
platformName = 'mac'
8179
}
80+
81+
// Generate the ditaval file using modern Gradle file operations
82+
outputFile.asFile.text = """<?xml version="1.0" encoding="UTF-8"?>
83+
<val>
84+
<prop action="include" att="platform" val="${platformName}"/>
85+
<prop action="exclude" att="platform"/>
86+
</val>
87+
"""
8288
}
8389
}
8490

8591
task generatePropertiesTemplate(type: SaxonXsltTask) {
8692
input "${configDir}/plugins.xml"
8793
output "${samplesDir}/properties/template.properties"
88-
stylesheet "${projectDir}/resources/properties-file.xsl"
94+
stylesheet "${projectDirPath}/resources/properties-file.xsl"
8995
}
9096

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

95101
task pdf(type: DitaOtTask, dependsOn: autoGenerate) {
96-
input "${projectDir}/userguide-book.ditamap"
102+
input "${projectDirPath}/userguide-book.ditamap"
97103
output outputDir
98104
transtype 'pdf'
99-
filter "${projectDir}/resources/pdf.ditaval"
105+
filter "${projectDirPath}/resources/pdf.ditaval"
100106

101107
properties {
102108
property(name: 'args.chapter.layout', value: 'BASIC')
103109
property(name: 'args.gen.task.lbl', value: 'YES')
104110
property(name: 'include.rellinks', value: '#default external')
105111
property(name: 'outputFile.base', value: 'userguide')
106-
property(name: 'theme', value: "${projectDir}/samples/themes/dita-ot-docs-theme.yaml")
112+
property(name: 'theme', value: "${projectDirPath}/samples/themes/dita-ot-docs-theme.yaml")
107113
}
108114
}
109115

110116
task html(type: DitaOtTask, dependsOn: autoGenerate) {
111-
input "${projectDir}/userguide.ditamap"
117+
input "${projectDirPath}/userguide.ditamap"
112118
output outputDir
113119
transtype 'html5'
114-
filter "${projectDir}/resources/html.ditaval"
120+
filter "${projectDirPath}/resources/html.ditaval"
115121

116122
properties {
117123
property(name: 'args.copycss', value: 'yes')
118124
property(name: 'args.css', value: 'dita-ot-doc.css')
119125
property(name: 'args.csspath', value: 'css')
120-
property(name: 'args.cssroot', value: "${projectDir}/resources/")
126+
property(name: 'args.cssroot', value: "${projectDirPath}/resources/")
121127
property(name: 'args.gen.task.lbl', value: 'YES')
122-
property(name: 'args.hdr', value: "${projectDir}/resources/header.xml")
128+
property(name: 'args.hdr', value: "${projectDirPath}/resources/header.xml")
123129
property(name: 'args.rellinks', value: 'noparent')
124130
property(name: 'html5.toc.generate', value: 'no')
125131
property(name: 'nav-toc', value: 'partial')
126132
}
127133
}
128134

129135
task htmlhelp(type: DitaOtTask, dependsOn: autoGenerate) {
130-
input "${projectDir}/userguide.ditamap"
136+
input "${projectDirPath}/userguide.ditamap"
131137
output outputDir
132138
transtype 'htmlhelp'
133139
filter ditavalFile
@@ -136,52 +142,79 @@ task htmlhelp(type: DitaOtTask, dependsOn: autoGenerate) {
136142
property(name: 'args.copycss', value: 'yes')
137143
property(name: 'args.css', value: 'dita-ot-doc.css')
138144
property(name: 'args.csspath', value: 'css')
139-
property(name: 'args.cssroot', value: "${projectDir}/resources/")
145+
property(name: 'args.cssroot', value: "${projectDirPath}/resources/")
140146
property(name: 'args.gen.task.lbl', value: 'YES')
141147
}
142148

143149
doLast {
144-
ant.move(todir: outputDir, failonerror: 'no') {
145-
fileset(dir: "${outputDir}/htmlhelp", includes: '*.chm')
150+
// Move .chm files using modern Gradle file operations
151+
def htmlhelpDir = file("${outputDir}/htmlhelp")
152+
if (htmlhelpDir.exists()) {
153+
copy {
154+
from htmlhelpDir
155+
into outputDir
156+
include '*.chm'
157+
}
158+
// Clean up the htmlhelp directory
159+
delete htmlhelpDir
146160
}
147-
148-
ant.delete(dir: "${outputDir}/htmlhelp")
149161
}
150162
}
151163

152164
task cleanUp {
153165
doLast {
154-
ant.delete(dir: outputDir)
166+
delete outputDir
167+
}
168+
}
169+
170+
// Get git commit hash at configuration time for tasks that need it
171+
def getGitCommitHash() {
172+
try {
173+
def result = new ByteArrayOutputStream()
174+
exec {
175+
workingDir = layout.projectDirectory.asFile
176+
commandLine 'git', 'rev-parse', 'HEAD'
177+
standardOutput = result
178+
ignoreExitValue = true
179+
}
180+
return result.toString().trim()
181+
} catch (Exception e) {
182+
logger.warn("Could not get git commit hash: ${e.message}")
183+
return 'unknown'
155184
}
156185
}
157186

158-
def commit = new ByteArrayOutputStream()
187+
// Store git commit for use by tasks
188+
def gitCommitHash = getGitCommitHash()
159189

160190
task gitMetadata {
161-
doLast {
162-
exec {
163-
workingDir = projectDir
164-
commandLine 'git'
165-
args = ['rev-parse', 'HEAD']
166-
standardOutput = commit
191+
// This task just logs the git commit for reference
192+
doLast {
193+
logger.info("Git commit: ${gitCommitHash}")
167194
}
168-
}
195+
196+
// Mark outputs to help with up-to-date checking
197+
outputs.upToDateWhen { false } // Always run since git commit changes frequently
169198
}
170199

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

174-
input file("${projectDir}/site.ditamap")
203+
input file("${projectDirPath}/site.ditamap")
175204
output getPropertyOrDefault('outputDir', "${buildDir}/site")
176-
filter "${projectDir}/resources/site.ditaval"
205+
filter "${projectDirPath}/resources/site.ditaval"
177206

178207
transtype 'org.dita-ot.html'
179208

209+
// Evaluate the noCommitMeta flag at configuration time
210+
def includeCommitMeta = !providers.gradleProperty('noCommitMeta').map { Boolean.parseBoolean(it) }.getOrElse(false)
211+
180212
properties {
181213
property(name: 'args.gen.task.lbl', value: 'YES')
182214
property(name: 'args.rellinks', value: 'noparent')
183-
if (!(project.hasProperty('noCommitMeta') && Boolean.parseBoolean(project.property('noCommitMeta')))) {
184-
property(name: 'commit', value: commit)
215+
if (includeCommitMeta) {
216+
// Use the git commit hash obtained at configuration time
217+
property(name: 'commit', value: gitCommitHash)
185218
}
186219
}
187220
}

gradle.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# Fix Java memory errors with Gradle 5.2
22
org.gradle.jvmargs = -Xmx1024m
3+
4+
# Gradle 8 features for better performance and caching
5+
# ↓ Not supported by eerohele/dita-ot-gradle ↓
6+
# org.gradle.configuration-cache=true
7+
# org.gradle.configuration-cache.problems=warn
8+
# ↑ Not supported by eerohele/dita-ot-gradle ↑
9+
org.gradle.parallel=true
10+
org.gradle.caching=true

0 commit comments

Comments
 (0)