Skip to content

Commit 13fc823

Browse files
committed
cache friendly gradle tasks
1 parent 1b6d7b6 commit 13fc823

1 file changed

Lines changed: 54 additions & 40 deletions

File tree

de.peeeq.wurstscript/build.gradle

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,68 +120,82 @@ def parseqFiles = fileTree(dir: 'parserspec', include: '*.parseq')
120120

121121
def pkgPattern = Pattern.compile(/package\s+(\S+)\s*;/)
122122

123-
tasks.register('genAst') {
124-
// make it incremental/cacheable
125-
inputs.files(parseqFiles)
126-
outputs.dir(genDir)
123+
// resolve once at configuration time
124+
def genDirFile = file(genDir)
125+
def astgenCp = configurations.astgen
126+
def pkgPatternLocal = pkgPattern
127+
128+
def perFileTasks = []
129+
130+
parseqFiles.files.each { File f ->
131+
// determine package at configuration time (same logic you had)
132+
String contents = f.getText('UTF-8')
133+
def m = pkgPatternLocal.matcher(contents)
134+
String pkg = m.find() ? m.group(1) : ""
135+
File targetDir = new File(genDirFile, pkg.replace('.', '/'))
136+
137+
def t = tasks.register("genAst_${f.name.replaceAll(/[^A-Za-z0-9_]/, '_')}", JavaExec) {
138+
// incremental + cache correctness for the generator
139+
inputs.file(f)
140+
inputs.files(astgenCp).withPropertyName("astgenClasspath")
141+
outputs.dir(targetDir)
142+
143+
classpath = astgenCp
144+
mainClass.set("asg.Main")
145+
args(f.absolutePath, targetDir.absolutePath)
146+
147+
// optional: ensure target dir exists
148+
doFirst { targetDir.mkdirs() }
149+
}
127150

128-
doLast {
129-
// fetch ExecOperations from Gradle services (no @Inject needed)
130-
ExecOperations execOps = project.services.get(ExecOperations)
151+
perFileTasks << t
152+
}
131153

132-
parseqFiles.files.each { File f ->
133-
String contents = f.getText('UTF-8')
134-
def m = pkgPattern.matcher(contents)
135-
String pkg = m.find() ? m.group(1) : ""
136-
File targetDir = file("$genDir/${pkg.replace('.', '/')}")
154+
tasks.register("genAst") {
155+
inputs.files(parseqFiles)
156+
outputs.dir(genDirFile)
157+
dependsOn(perFileTasks)
158+
}
137159

138-
targetDir.mkdirs()
139160

140-
// run: asg.Main <file> <targetDir> using isolated classpath
141-
execOps.javaexec {
142-
classpath = configurations.astgen
143-
mainClass.set('asg.Main')
144-
args(f.absolutePath, targetDir.absolutePath)
145-
}
146-
}
147-
}
148-
}
149161

150162
/** -------- Version info file generation -------- */
151163

152164
tasks.register('versionInfoFile') {
153165
description "Generates a file CompileTimeInfo.java with version number etc."
154166

155-
// resolve git info at configuration time
156-
Git git = Git.open(new File(rootProject.projectDir, '..'))
157-
ObjectId head = git.getRepository().resolve(Constants.HEAD)
158-
String gitRevision = head.abbreviate(8).name()
159-
String gitRevisionlong = head.getName()
160-
String tag = git.describe().setTarget(head).setAlways(true).setTags(true).call()
161-
String wurstVersion = "${version}-${tag}"
162-
163-
inputs.property("wurstVersion", wurstVersion)
167+
def repoDir = new File(rootProject.projectDir, '..')
164168

165169
def dir = new File("$genDir/de/peeeq/wurstscript/")
166170
def out = new File(dir, 'CompileTimeInfo.java')
167171
outputs.file(out)
168172

173+
// capture at configuration time (no Task.project usage later)
174+
def versionString = project.version.toString()
175+
169176
doLast {
177+
def rev8 = ["git", "rev-parse", "--short=8", "HEAD"].execute(null, repoDir).text.trim()
178+
def revLong = ["git", "rev-parse", "HEAD"].execute(null, repoDir).text.trim()
179+
def tag = ["git", "describe", "--tags", "--always", "--dirty"].execute(null, repoDir).text.trim()
180+
181+
def wurstVersion = "${versionString}-${tag}"
182+
def currentTime = new Date().format("yyyy/MM/dd KK:mm:ss")
183+
170184
dir.mkdirs()
171-
String currentTime = new Date().format("yyyy/MM/dd KK:mm:ss")
172185
out.text = """
173-
package de.peeeq.wurstscript;
186+
package de.peeeq.wurstscript;
174187
175-
public class CompileTimeInfo {
176-
public static final String time="${currentTime}";
177-
public static final String revision="${gitRevision}";
178-
public static final String revisionLong="${gitRevisionlong}";
179-
public static final String version="${wurstVersion}";
180-
}
181-
"""
188+
public class CompileTimeInfo {
189+
public static final String time="${currentTime}";
190+
public static final String revision="${rev8}";
191+
public static final String revisionLong="${revLong}";
192+
public static final String version="${wurstVersion}";
193+
}
194+
"""
182195
}
183196
}
184197

198+
185199
/** -------- Aggregate generation + wiring into compile -------- */
186200

187201
tasks.register('gen') {

0 commit comments

Comments
 (0)