Skip to content

Commit 90c5c4d

Browse files
committed
gradle scripts: make javaCC code reusable by using a gradle plugin
1 parent 2ab0a7e commit 90c5c4d

9 files changed

Lines changed: 88 additions & 116 deletions

File tree

buildSrc/src/main/groovy/JavaCCPlugin.groovy

Lines changed: 0 additions & 61 deletions
This file was deleted.

buildSrc/src/main/groovy/JavaCCTask.groovy

Lines changed: 0 additions & 21 deletions
This file was deleted.

buildSrc/src/main/groovy/buildlogic.groovy-common-conventions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
// Apply a specific Java toolchain to ease working on different environments.
3333
java {
3434
toolchain {
35-
languageVersion = JavaLanguageVersion.of(21)
35+
languageVersion = JavaLanguageVersion.of(23)
3636
}
3737
}
3838

buildSrc/src/main/groovy/buildlogic.java-common-conventions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies {
2727
// Apply a specific Java toolchain to ease working on different environments.
2828
java {
2929
toolchain {
30-
languageVersion = JavaLanguageVersion.of(21)
30+
languageVersion = JavaLanguageVersion.of(23)
3131
}
3232
}
3333

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.xcommand
2+
3+
import org.gradle.api.*
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.tasks.Copy
6+
7+
class JavaCCPluginExtension {
8+
final Property<File> inputFile
9+
final Property<File> genDir
10+
11+
JavaCCPluginExtension(Project project) {
12+
inputFile = project.objects.property(File)
13+
genDir = project.objects.property(File)
14+
}
15+
}
16+
17+
class JavaCCPlugin implements Plugin<Project> {
18+
void apply(Project project) {
19+
def ext = project.extensions.create("javacc", JavaCCPluginExtension, project)
20+
def versionJavacc = project.findProperty('VERSION_JAVACC') ?: 'UNKNOWN'
21+
22+
project.tasks.register('copyToLib', Copy) {
23+
into("${project.buildDir}/_artifacts")
24+
from project.configurations.compileClasspath
25+
from project.configurations.default.allArtifacts*.file
26+
from project.configurations.compileClasspath.allArtifacts*.file
27+
}
28+
29+
project.tasks.register('renameJCC', Copy) {
30+
dependsOn project.tasks.named('copyToLib')
31+
from("${project.buildDir}/_artifacts/javacc-${versionJavacc}.jar")
32+
into("${project.buildDir}/_artifacts")
33+
rename("javacc-${versionJavacc}.jar", "javacc.jar")
34+
}
35+
36+
// jcc task
37+
project.tasks.register('jcc', JavaCCTask) { JavaCCTask t ->
38+
dependsOn(project.tasks.named('renameJCC'))
39+
40+
// must be provided by the caller; fail if missing
41+
t.inputFile.set(ext.inputFile)
42+
t.genDir.set(ext.genDir)
43+
44+
// Add generated directory to main source set lazily
45+
project.sourceSets.main.java.srcDirs += t.genDir
46+
}
47+
48+
}
49+
50+
}
51+
52+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.xcommand
2+
3+
import org.gradle.api.*
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.tasks.*
6+
7+
class JavaCCTask extends DefaultTask {
8+
@InputFile
9+
final Property<File> inputFile = project.objects.property(File)
10+
11+
@OutputDirectory
12+
final Property<File> genDir = project.objects.property(File)
13+
14+
@TaskAction
15+
void generate() {
16+
println "generate jcc source"
17+
File input = inputFile.get()
18+
File output = genDir.get()
19+
20+
ant.mkdir(dir: output)
21+
ant.javacc(target: input, outputdirectory: output, javacchome: 'build/_artifacts')
22+
}
23+
24+
}
25+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=org.xcommand.JavaCCPlugin

collage/build.gradle

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
plugins {
22
id 'buildlogic.groovy-library-conventions'
33
alias(libs.plugins.versions)
4+
id "org.xcommand.javacc"
45
}
56

6-
task ('copyToLib', type: Copy) {
7-
into('build/_artifacts')
8-
from configurations.compileClasspath
9-
from configurations.default.allArtifacts*.file
10-
from configurations.compileClasspath.allArtifacts*.file
7+
javacc {
8+
inputFile = file("src/main/javacc/templateparser.jj")
9+
genDir = file("build/src/java/org/collage/jcc")
1110
}
12-
13-
task renameJCC(type: Copy, dependsOn: 'copyToLib') {
14-
from("build/_artifacts/javacc-${VERSION_JAVACC}.jar")
15-
into('build/_artifacts')
16-
rename("javacc-${VERSION_JAVACC}.jar", 'javacc.jar')
17-
}
18-
19-
task jcc (dependsOn: 'renameJCC', type: JavaCCTask) {
20-
inputFile = file('src/main/javacc/templateparser.jj')
21-
genDir = file('build/src/java/org/collage/jcc')
22-
}
23-
2411
compileJava.dependsOn(jcc)
2512

2613
dependencies {

jst/build.gradle

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
plugins {
22
id 'buildlogic.java-library-conventions'
33
alias(libs.plugins.versions)
4+
id "org.xcommand.javacc"
45
}
56

6-
task ('copyToLib', type: Copy) {
7-
into('build/_artifacts')
8-
from configurations.compileClasspath
9-
from configurations.default.allArtifacts*.file
10-
from configurations.compileClasspath.allArtifacts*.file
7+
javacc {
8+
inputFile = file("src/main/javacc/jst.jj")
9+
genDir = file("build/src/java/org/xcommand/template/jst/parser")
1110
}
12-
task renameJCC(type: Copy, dependsOn: 'copyToLib') {
13-
from("build/_artifacts/javacc-${VERSION_JAVACC}.jar")
14-
into('build/_artifacts')
15-
rename("javacc-${VERSION_JAVACC}.jar", 'javacc.jar')
16-
}
17-
task jcc (dependsOn: 'renameJCC', type: JavaCCTask) {
18-
inputFile = 'src/main/javacc/jst.jj'
19-
genDir = 'build/src/java/org/xcommand/template/jst/parser'
20-
}
21-
2211
compileJava.dependsOn(jcc)
2312

2413
sourceSets {

0 commit comments

Comments
 (0)