Skip to content

Commit

Permalink
fix: Update Gradle plugin to Gradle 8 API
Browse files Browse the repository at this point in the history
  • Loading branch information
matrei committed Oct 1, 2024
1 parent c532f84 commit 77953ca
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ abstract class AbstractGroovyTemplateCompileTask extends AbstractCompile {
@Nested
final ViewCompileOptions compileOptions

@Input
final Property<String> fileExtension

@Input
final Property<String> scriptBaseName

@Input
final Property<String> compilerName

@Inject
AbstractGroovyTemplateCompileTask(ObjectFactory objectFactory) {
packageName = objectFactory.property(String)
packageName = objectFactory.property(String).convention(project.name ?: project.projectDir.canonicalFile.name)
srcDir = objectFactory.directoryProperty()
compileOptions = new ViewCompileOptions(objectFactory)
fileExtension = objectFactory.property(String)
scriptBaseName = objectFactory.property(String)
compilerName = objectFactory.property(String)
}

@Override
Expand All @@ -62,16 +74,18 @@ abstract class AbstractGroovyTemplateCompileTask extends AbstractCompile {
protected void compile() {
Iterable<String> projectPackageNames = getProjectPackageNames(project.projectDir)

/*
if (packageName.isPresent()) {
packageName.set(project.name ?: project.projectDir.canonicalFile.name)
}
*/

ExecResult result = project.javaexec(
new Action<JavaExecSpec>() {
@Override @CompileDynamic
void execute(JavaExecSpec javaExecSpec) {
javaExecSpec.mainClass.set(getCompilerName())
javaExecSpec.classpath = getClasspath()
javaExecSpec.mainClass.set(compilerName)
javaExecSpec.classpath = classpath

List<String> jvmArgs = compileOptions.forkOptions.jvmArgs
if (jvmArgs) {
Expand All @@ -88,7 +102,7 @@ abstract class AbstractGroovyTemplateCompileTask extends AbstractCompile {
packageImports,
packageName.get(),
project.file('grails-app/conf/application.yml').canonicalPath,
compileOptions.encoding
compileOptions.encoding.get()
] as List<String>

prepareArguments(arguments)
Expand All @@ -103,17 +117,6 @@ abstract class AbstractGroovyTemplateCompileTask extends AbstractCompile {
// no-op
}

@Input
protected String getCompilerName() {
'grails.views.GenericGroovyTemplateCompiler'
}

@Input
abstract String getFileExtension()

@Input
abstract String getScriptBaseName()

Iterable<String> getProjectPackageNames(File baseDir) {
File rootDir = baseDir ? new File(baseDir, "grails-app${File.separator}domain") : null
Set<String> packageNames = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.SourceSetOutput
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.bundling.Jar
import org.grails.gradle.plugin.core.GrailsExtension
import org.grails.gradle.plugin.core.IntegrationTestGradlePlugin
Expand All @@ -32,7 +34,7 @@ class AbstractGroovyTemplatePlugin implements Plugin<Project> {
AbstractGroovyTemplatePlugin(Class<? extends AbstractGroovyTemplateCompileTask> taskClass, String fileExtension) {
this.taskClass = taskClass
this.fileExtension = fileExtension
this.pathToSource = "grails-app/views"
this.pathToSource = 'grails-app/views'
}

AbstractGroovyTemplatePlugin(Class<? extends AbstractGroovyTemplateCompileTask> taskClass, String fileExtension, String pathToSource) {
Expand All @@ -43,43 +45,58 @@ class AbstractGroovyTemplatePlugin implements Plugin<Project> {

@Override
void apply(Project project) {
def allTasks = project.tasks
def upperCaseName = GrailsNameUtils.getClassName(fileExtension)
AbstractGroovyTemplateCompileTask templateCompileTask = (AbstractGroovyTemplateCompileTask) allTasks.register("compile${upperCaseName}Views".toString(), (Class<? extends Task>) taskClass).get()
TaskContainer tasks = project.tasks
String upperCaseName = GrailsNameUtils.getClassName(fileExtension)
AbstractGroovyTemplateCompileTask templateCompileTask = (AbstractGroovyTemplateCompileTask) tasks.register(
"compile${upperCaseName}Views".toString(),
(Class<? extends Task>) taskClass
).get()
SourceSetOutput output = SourceSets.findMainSourceSet(project)?.output
FileCollection classesDir = resolveClassesDirs(output, project)
File destDir = new File(project.buildDir, "${templateCompileTask.fileExtension}-classes/main")
File destDir = new File(project.layout.buildDirectory.get().asFile, "${templateCompileTask.fileExtension}-classes/main")
output?.dir(destDir)
project.afterEvaluate {
GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension)
if (grailsExt.pathingJar && Os.isFamily(Os.FAMILY_WINDOWS)) {
Jar pathingJar = (Jar) allTasks.named('pathingJar').get()
def allClasspath = project.files("${project.buildDir}/classes/groovy/main", "${project.buildDir}/resources/main", "${project.projectDir}/gsp-classes", pathingJar.archiveFile.get().asFile)
Jar pathingJar = (Jar) tasks.named('pathingJar').get()
ConfigurableFileCollection allClasspath = project.files(
"${project.layout.buildDirectory.get().asFile}/classes/groovy/main",
"${project.layout.buildDirectory.get().asFile}/resources/main",
"${project.layout.projectDirectory.getAsFile()}/gsp-classes",
pathingJar.archiveFile.get().asFile
)
templateCompileTask.dependsOn(pathingJar)
templateCompileTask.classpath = allClasspath
}
}
def allClasspath = classesDir + project.configurations.named('compileClasspath').get()
templateCompileTask.getDestinationDirectory().set( destDir )
templateCompileTask.destinationDirectory.set(destDir)
templateCompileTask.classpath = allClasspath
templateCompileTask.packageName.set(project.name)
templateCompileTask.setSource(project.file("${project.projectDir}/$pathToSource"))
templateCompileTask.dependsOn( allTasks.named('classes').get() )
templateCompileTask.dependsOn(tasks.named('classes').get())
project.plugins.withType(SpringBootPlugin).configureEach {plugin ->
allTasks.withType(Jar).configureEach { Task task ->
if (task.name in ['jar', 'bootJar', 'war', 'bootWar']) { task.dependsOn templateCompileTask }
tasks.withType(Jar).configureEach { Task task ->
if (task.name in ['jar', 'bootJar', 'war', 'bootWar']) {
task.dependsOn(templateCompileTask)
}
}
tasks.withType(ResolveMainClassName).configureEach {
it.dependsOn(templateCompileTask)
}
allTasks.withType(ResolveMainClassName).configureEach { t -> t.dependsOn(templateCompileTask)}
}
project.plugins.withType(IntegrationTestGradlePlugin).configureEach { plugin ->
allTasks.named("compileIntegrationTestGroovy") { t-> t.dependsOn(templateCompileTask)}
allTasks.named("integrationTest") {t -> t.dependsOn(templateCompileTask)}
project.plugins.withType(IntegrationTestGradlePlugin).configureEach {
tasks.named('compileIntegrationTestGroovy') { Task task ->
task.dependsOn(templateCompileTask)
}
tasks.named('integrationTest') { Task task ->
task.dependsOn(templateCompileTask)
}
}
}

@CompileDynamic
protected FileCollection resolveClassesDirs(SourceSetOutput output, Project project) {
return output.classesDirs ?: project.files(new File(project.buildDir, "classes/groovy/main"))
return output.classesDirs ?: project.files(new File(project.layout.buildDirectory.get().asFile, 'classes/groovy/main'))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.compile.GroovyForkOptions
import javax.inject.Inject;
import javax.inject.Inject

/**
* @author Graeme Rocher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import groovy.transform.CompileStatic
class GrailsJsonViewsPlugin extends AbstractGroovyTemplatePlugin {

GrailsJsonViewsPlugin() {
super(JsonViewCompilerTask, "gson")
super(JsonViewCompilerTask, 'gson')
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package grails.views.gradle.json

import grails.views.gradle.AbstractGroovyTemplateCompileTask
import groovy.transform.CompileStatic
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input

import javax.inject.Inject

/**
* Concrete implementation that compiles JSON templates
*
Expand All @@ -12,22 +16,11 @@ import org.gradle.api.tasks.Input
@CompileStatic
class JsonViewCompilerTask extends AbstractGroovyTemplateCompileTask {

@Input
@Override
String getFileExtension() {
"gson"
}

@Input
@Override
String getScriptBaseName() {
"grails.plugin.json.view.JsonViewTemplate"
@Inject
JsonViewCompilerTask(ObjectFactory objectFactory) {
super(objectFactory)
fileExtension.convention('gson')
scriptBaseName.convention('grails.plugin.json.view.JsonViewTemplate')
compilerName.convention('grails.plugin.json.view.JsonViewCompiler')
}

@Input
@Override
protected String getCompilerName() {
"grails.plugin.json.view.JsonViewCompiler"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import groovy.transform.CompileStatic
class GrailsMarkupViewsPlugin extends AbstractGroovyTemplatePlugin {

GrailsMarkupViewsPlugin() {
super(MarkupViewCompilerTask, "gml")
super(MarkupViewCompilerTask, 'gml')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package grails.views.gradle.markup

import grails.views.gradle.AbstractGroovyTemplateCompileTask
import groovy.transform.CompileStatic
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input

/**
Expand All @@ -14,21 +16,18 @@ import org.gradle.api.tasks.Input
class MarkupViewCompilerTask extends AbstractGroovyTemplateCompileTask {

@Input
@Override
String getFileExtension() {
"gml"
}
final Property<String> fileExtension

@Input
@Override
String getScriptBaseName() {
"grails.plugin.markup.view.MarkupViewTemplate"
}
final Property<String> scriptBaseName

@Input
@Override
protected String getCompilerName() {
"grails.plugin.markup.view.MarkupViewCompiler"
}
final Property<String> compilerName

MarkupViewCompilerTask(ObjectFactory objectFactory) {
super(objectFactory)
fileExtension.convention('gml')
scriptBaseName.convention('grails.plugin.markup.view.MarkupViewTemplate')
compilerName.convention('grails.plugin.markup.view.MarkupViewCompiler')
}
}

0 comments on commit 77953ca

Please sign in to comment.