Skip to content

Commit

Permalink
Implemented Forked GSP Compiler on Gradle to work similar to grails-v…
Browse files Browse the repository at this point in the history
…iews (grails#11582)

* Implemented Forked GSP Compiler on Gradle to work similar to grails-views

* fixed versions and updated javadoc

* whoops, reverting change to request mock
  • Loading branch information
davydotcom committed Jul 9, 2020
1 parent c039fbc commit c191ac6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.grails.gradle.plugin.web.gsp

import grails.io.ResourceUtils
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.codehaus.groovy.tools.shell.util.PackageHelper
import org.gradle.api.Action
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.process.ExecResult
import org.gradle.process.JavaExecSpec

/**
* Abstract Gradle task for compiling templates, using GroovyPageCompilerForkTask
* This Task is a Forked Java Task that is configurable with fork options provided
* by {@link GspCompileOptions}
*
* @author David Estes
* @since 4.0
*/
@CompileStatic
class GroovyPageForkCompileTask extends AbstractCompile {

@Input
@Optional
String packageName

@InputDirectory
File srcDir

@Input
File tmpDir

@Input
@Optional
String serverpath

@Nested
GspCompileOptions compileOptions = new GspCompileOptions()

@Override
void setSource(Object source) {
try {
srcDir = project.file(source)
if(srcDir.exists() && !srcDir.isDirectory()) {
throw new IllegalArgumentException("The source for GSP compilation must be a single directory, but was $source")
}
super.setSource(source)
} catch (e) {
throw new IllegalArgumentException("The source for GSP compilation must be a single directory, but was $source")
}
}

@TaskAction
void execute(IncrementalTaskInputs inputs) {
compile()
}

protected void compile() {

if(packageName == null) {
packageName = project.name
if(!packageName) {
packageName = project.projectDir.canonicalFile.name
}
}

ExecResult result = project.javaexec(
new Action<JavaExecSpec>() {
@Override
@CompileDynamic
void execute(JavaExecSpec javaExecSpec) {
javaExecSpec.setMain(getCompilerName())
javaExecSpec.setClasspath(getClasspath())

def jvmArgs = compileOptions.forkOptions.jvmArgs
if(jvmArgs) {
javaExecSpec.jvmArgs(jvmArgs)
}
javaExecSpec.setMaxHeapSize( compileOptions.forkOptions.memoryMaximumSize )
javaExecSpec.setMinHeapSize( compileOptions.forkOptions.memoryInitialSize )


def arguments = [
srcDir.canonicalPath,
destinationDir.canonicalPath,
tmpDir.canonicalPath,
targetCompatibility,
packageName,
serverpath,
project.file("grails-app/conf/application.yml").canonicalPath,
compileOptions.encoding
]

prepareArguments(arguments)
javaExecSpec.args(arguments)
}

}
)
result.assertNormalExitValue()

}

void prepareArguments(List<String> arguments) {
// no-op
}

protected String getCompilerName() {
"org.grails.web.pages.GroovyPageCompilerForkTask"
}


String getFileExtension() {
"gsp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ class GroovyPagePlugin implements Plugin<Project> {
}

def allTasks = project.tasks
def compileGroovyPages = allTasks.create("compileGroovyPages", GroovyPageCompileTask) {
def compileGroovyPages = allTasks.create("compileGroovyPages", GroovyPageForkCompileTask) {
destinationDir = destDir
tmpDir = getTmpDir(project)
source = project.file("${project.projectDir}/grails-app/views")
serverpath = "/WEB-INF/grails-app/views/"
classpath = allClasspath
}

def compileWebappGroovyPages = allTasks.create("compileWebappGroovyPages", GroovyPageCompileTask) {
compileGroovyPages.setClasspath( allClasspath )

def compileWebappGroovyPages = allTasks.create("compileWebappGroovyPages", GroovyPageForkCompileTask) {
destinationDir = destDir
source = project.file("${project.projectDir}/src/main/webapp")
tmpDir = getTmpDir(project)
serverpath = "/"
classpath = allClasspath
}

compileWebappGroovyPages.setClasspath( allClasspath )


compileGroovyPages.dependsOn( allTasks.findByName("classes") )
compileGroovyPages.dependsOn( compileWebappGroovyPages )
Expand Down Expand Up @@ -90,6 +94,12 @@ class GroovyPagePlugin implements Plugin<Project> {
output?.classesDirs ?: project.files(new File(project.buildDir, "classes/main"))
}

protected File getTmpDir(Project project) {
def tmpdir = new File(project.buildDir as String, "gsptmp")
tmpdir.mkdirs()
return tmpdir
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.grails.gradle.plugin.web.gsp

import org.gradle.api.tasks.compile.GroovyForkOptions

/**
* Presents the Compile Options used by the {@llink GroovyPageForkCompileTask}
*
* @author David Estes
* @since 4.0
*/
class GspCompileOptions {
String encoding = "UTF-8"
GroovyForkOptions forkOptions = new GroovyForkOptions()
}

0 comments on commit c191ac6

Please sign in to comment.