Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ h2.version=1.4.199
gradleNexusPluginVersion=2.3.1
gradleNexusStagingPluginVersion=0.12.0
servletApiVersion=4.0.1
# These need to be released once 4.0.0 is available
asyncVersion=4.0.0
gspVersion=4.0.0
legacyConvertersVersion=4.0.1
testingSupportVersion=2.1.1
scaffoldingCoreVersion=2.1.0
gspVersion=4.0.1
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import javax.servlet.ServletResponse
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.Part

import grails.web.mime.MimeType
import org.grails.web.util.GrailsApplicationAttributes
import org.grails.web.servlet.mvc.GrailsWebRequest
Expand Down