Skip to content

Commit

Permalink
pre-compilation for unit tests, generate commands and plugins
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.codehaus.org/grails/trunk@4984 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
  • Loading branch information
graeme committed Jul 29, 2007
1 parent 03fdc75 commit 052a570
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 144 deletions.
41 changes: 28 additions & 13 deletions scripts/Compile.groovy
Expand Up @@ -52,7 +52,30 @@ Ant.taskdef ( name : 'groovyc' ,
task ('default': "Performs compilation on any source files (Java or Groovy) in the 'src' tree") {
compile()
}

compilerClasspath = { testSources ->

def excludedPaths = ["views", "i18n"]
def pluginResources = resolveResources("file:${basedir}/plugins/*/grails-app/*").toList() +
resolveResources("file:${basedir}/plugins/*/src/java").toList() +
resolveResources("file:${basedir}/plugins/*/src/groovy").toList()

for(dir in new File("${basedir}/grails-app").listFiles()) {
if(!excludedPaths.contains(dir.name) && dir.isDirectory())
src(path:"${dir}")
}
for(dir in pluginResources.file) {
if(!excludedPaths.contains(dir.name) && dir.isDirectory()) {
src(path:"${dir}")
}
}
src(path:"${basedir}/src/java")
src(path:"${basedir}/src/groovy")
if(testSources) {
src(path:"${basedir}/test/unit")
src(path:"${basedir}/test/integration")
}
}
task(compile : "Implementation of compilation phase") {
depends(dependencies, classpath)

Expand All @@ -63,22 +86,14 @@ task(compile : "Implementation of compilation phase") {
Ant.sequential {
mkdir(dir:"${basedir}/web-app/WEB-INF/classes")


def excludedPaths = ["views", "i18n"]

def destDir = "${userHome}/.grails/${grailsVersion}/tmp/${baseName}/classes"

//def destDir = "${userHome}/.grails/${grailsVersion}/tmp/${baseName}/classes"
def destDir = "${basedir}/web-app/WEB-INF/classes"

mkdir(dir:destDir)
groovyc(destdir:destDir,
classpathref:"grails.classpath",
resourcePattern:"file:${basedir}/grails-app/**/*.groovy") {
for(dir in new File("${basedir}/grails-app").listFiles()) {
if(!excludedPaths.contains(dir.name) && dir.isDirectory())
src(path:"${dir}")
}
src(path:"${basedir}/src/java")
src(path:"${basedir}/src/groovy")
}
resourcePattern:"file:${basedir}/**/grails-app/**/*.groovy",
compilerClasspath.curry(false))

def rootLoader = getClass()
.classLoader
Expand Down
60 changes: 54 additions & 6 deletions scripts/GenerateAll.groovy
Expand Up @@ -24,12 +24,18 @@

import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
import groovy.text.SimpleTemplateEngine
import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator;
import org.codehaus.groovy.grails.scaffolding.*
import org.springframework.mock.web.MockServletContext;


Ant.property(environment:"env")
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )
includeTargets << new File ( "${grailsHome}/scripts/Package.groovy" )

generateViews = true
generateController = true

task ('default': "Generates a CRUD interface (contoroller + views) for a domain class") {
depends( checkVersion, packageApp )
Expand All @@ -38,9 +44,51 @@ task ('default': "Generates a CRUD interface (contoroller + views) for a domain
generateAll()
}

task(generateAll:"The implementation task") {
Ant.java(classname:"grails.util.GenerateUtils", failonerror:true) {
arg(value:"all")
arg(value:args)
}
task(generateAll:"The implementation task") {

def beans = new grails.spring.BeanBuilder().beans {
resourceHolder(org.codehaus.groovy.grails.commons.spring.GrailsResourceHolder) {
resources = "file:${basedir}/**/grails-app/domain/*.groovy"
}
grailsResourceLoader(org.codehaus.groovy.grails.commons.GrailsResourceLoaderFactoryBean) {
grailsResourceHolder = resourceHolder
}
grailsApplication(org.codehaus.groovy.grails.commons.DefaultGrailsApplication.class, ref("grailsResourceLoader"))
}

appCtx = beans.createApplicationContext()
grailsApp = appCtx.grailsApplication
grailsApp.initialise()

def name = args.trim()
def domainClass = grailsApp.getDomainClass(name)

if(!domainClass) {
println "Domain class not found in grails-app/domain, trying hibernate mapped classes..."
try {
def config = new GrailsRuntimeConfigurator(grailsApp, appCtx)
appCtx = config.configure(new MockServletContext())
}
catch(Exception e) {
println e.message
e.printStackTrace()
}
domainClass = grailsApp.getDomainClass(name)
}

if(domainClass) {
def generator = new DefaultGrailsTemplateGenerator()
if(generateViews) {
event("StatusUpdate", ["Generating views for domain class ${domainClass.fullName}"])
generator.generateViews(domainClass,".")
}
if(generateController) {
event("StatusUpdate", ["Generating controller for domain class ${domainClass.fullName}"])
generator.generateController(domainClass,".")
}
event("StatusFinal", ["Finished generation for domain class ${domainClass.fullName}"])
}
else {
event("StatusFinal", ["No domain class found for name ${name}. Please try again and enter a valid domain class name"])
}
}
17 changes: 5 additions & 12 deletions scripts/GenerateController.groovy
Expand Up @@ -15,7 +15,7 @@
*/

/**
* Gant script that generates a CRUD controller for a given domain class
* Gant script that generates CRUD views for a given domain class
*
* @author Graeme Rocher
*
Expand All @@ -28,19 +28,12 @@ import groovy.text.SimpleTemplateEngine
Ant.property(environment:"env")
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )
includeTargets << new File ( "${grailsHome}/scripts/Package.groovy" )
includeTargets << new File ( "${grailsHome}/scripts/GenerateAll.groovy" )

task ('default': "Generates a CRUD controller for a specified domain class") {
task ('default': "Generates the CRUD views for a specified domain class") {
depends( checkVersion, packageApp )
typeName = "Domain Class"
promptForName()
promptForName()
generateViews = false
generateAll()
}

task(generateAll:"The implementation task") {
Ant.java(classname:"grails.util.GenerateUtils", failonerror:true) {
arg(value:"controller")
arg(value:args)
}
}
13 changes: 3 additions & 10 deletions scripts/GenerateViews.groovy
Expand Up @@ -28,19 +28,12 @@ import groovy.text.SimpleTemplateEngine
Ant.property(environment:"env")
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"

includeTargets << new File ( "${grailsHome}/scripts/Init.groovy" )
includeTargets << new File ( "${grailsHome}/scripts/Package.groovy" )
includeTargets << new File ( "${grailsHome}/scripts/GenerateAll.groovy" )

task ('default': "Generates the CRUD views for a specified domain class") {
depends( checkVersion, packageApp )
typeName = "Domain Class"
promptForName()
promptForName()
generateController = false
generateAll()
}

task(generateAll:"The implementation task") {
Ant.java(classname:"grails.util.GenerateUtils", failonerror:true) {
arg(value:"view")
arg(value:args)
}
}
9 changes: 6 additions & 3 deletions scripts/Init.groovy
Expand Up @@ -381,16 +381,19 @@ void setClasspath() {

Ant.path(id:"grails.classpath") {
pathelement(location:"${basedir}")
pathelement(location:"${basedir}/grails-tests")
pathelement(location:"${basedir}/test/unit")
pathelement(location:"${basedir}/test/integration")
pathelement(location:"${basedir}/web-app")
pathelement(location:"${basedir}/web-app/WEB-INF")
pathelement(location:"${basedir}/web-app/WEB-INF/classes")
if (new File("${basedir}/web-app/WEB-INF/lib").exists()) {
fileset(dir:"${basedir}/web-app/WEB-INF/lib")
}
fileset(dir:"${grailsHome}/lib")
fileset(dir:"${grailsHome}/dist")
fileset(dir:"lib")
fileset(dir:"${grailsHome}/dist")
if(new File("${basedir}/lib").exists()) {
fileset(dir:"${basedir}/lib")
}
for(d in grailsDir) {
pathelement(location:"${d.file.absolutePath}")
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/InstallPlugin.groovy
Expand Up @@ -83,7 +83,7 @@ task(cachePlugin:"Implementation task") {
task(installPlugin:"Implementation task") {
depends( configureProxy )
// fix for Windows-style path with backslashes
def pluginsBase = "${basedir}/plugins".toString().replaceAll(/\\/,'/')
def pluginsBase = "${basedir}/plugins".toString().replaceAll('\\\\','/')
if(args) {
def pluginFile = new File(args.trim())
Ant.mkdir(dir:pluginsBase)
Expand Down
64 changes: 27 additions & 37 deletions scripts/TestApp.groovy
Expand Up @@ -81,7 +81,7 @@ task(testApp:"The test app implementation task") {
Ant.mkdir(dir:"${testDir}/plain")


//runCompiledTests()
compileTests()
try {
runUnitTests()
runIntegrationTests()
Expand All @@ -91,28 +91,19 @@ task(testApp:"The test app implementation task") {
processResults()
}
}
task(compileTests:"Compiles the test cases") {
def destDir = "${basedir}/test/classes"
Ant.sequential {

task(runCompiledTests:"Runs the tests located under src/test which are compiled then executed") {
compileTests()
Ant.sequential {
junit(fork:true, forkmode:"once", failureproperty:"grails.test.failures") {
classpath(refid:"grails.classpath")
jvmarg(value:"-Xmx256M")

formatter(type:"xml")
batchtest(todir:testDir) {
fileset(dir:"${basedir}/target/test-classes", includes:"**/*Tests.class")
}
}
}
def result = Ant.antProject.properties["grails.test.failures"]
if(result == "true") {
event("StatusFinal", ["Compiled tests failed"])
exit(1)
}
event("StatusFinal", ["Compiled tests complete"])
}

mkdir(dir:destDir)
groovyc(destdir:destDir,
classpathref:"grails.classpath",
resourcePattern:"file:${basedir}/**/grails-app/**/*.groovy",
compilerClasspath.curry(true))
}
def rootLoader = getClass().classLoader.rootLoader
rootLoader?.addURL(new File(destDir).toURL())
}

task(produceReports:"Outputs aggregated xml and html reports") {
Ant.junitreport {
Expand All @@ -125,9 +116,9 @@ task(produceReports:"Outputs aggregated xml and html reports") {


def populateTestSuite = { suite, testFiles, classLoader, ctx ->
testFiles.each { r ->
for(r in testFiles) {
try {
def c = classLoader.parseClass(r.file)
def c = classLoader.loadClass(r.filename - ".groovy")
if(TestCase.isAssignableFrom(c) && !Modifier.isAbstract(c.modifiers)) {
suite.addTest(new GrailsTestSuite(ctx.beanFactory, c))
}
Expand All @@ -138,7 +129,7 @@ def populateTestSuite = { suite, testFiles, classLoader, ctx ->
}
}
def runTests = { suite, TestResult result, Closure callback ->
suite.tests().each { test ->
for(test in suite.tests()) {
new File("${testDir}/TEST-${test.name}.xml").withOutputStream { xmlOut ->
new File("${testDir}/plain/TEST-${test.name}.txt").withOutputStream { plainOut ->
def xmlOutput = new XMLJUnitResultFormatter(output:xmlOut)
Expand Down Expand Up @@ -174,11 +165,14 @@ def runTests = { suite, TestResult result, Closure callback ->
}
task(runUnitTests:"Run Grails' unit tests under the test/unit directory") {
try {
def beans = new grails.spring.BeanBuilder().beans {
domainInjector(org.codehaus.groovy.grails.injection.DefaultGrailsDomainClassInjector.class)
injectionOperation(org.codehaus.groovy.grails.injection.GrailsInjectionOperation.class)
def appResources = resolveResources("grails-app/**/*.groovy")
grailsApplication(org.codehaus.groovy.grails.commons.DefaultGrailsApplication.class, appResources, ref("injectionOperation") )
def beans = new grails.spring.BeanBuilder().beans {
resourceHolder(org.codehaus.groovy.grails.commons.spring.GrailsResourceHolder) {
resources = "file:${basedir}/**/grails-app/**/*.groovy"
}
grailsResourceLoader(org.codehaus.groovy.grails.commons.GrailsResourceLoaderFactoryBean) {
grailsResourceHolder = resourceHolder
}
grailsApplication(org.codehaus.groovy.grails.commons.DefaultGrailsApplication.class, ref("grailsResourceLoader"))
}

appCtx = beans.createApplicationContext()
Expand Down Expand Up @@ -226,18 +220,14 @@ task(runIntegrationTests:"Runs Grails' tests under the test/integration director
event("StatusUpdate", [ "No tests found in test/integration to execute"])
return
}

def ctx = GU.bootstrapGrailsFromClassPath()

def config = new org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator(grailsApp,appCtx)
def ctx = config.configure(new MockServletContext())
def app = ctx.getBean(GrailsApplication.APPLICATION_ID)
if(app.parentContext == null) {
app.applicationContext = ctx
}
def classLoader = app.classLoader

def resources = app.resourceLoader.resources as ArrayList
testFiles.each() { resources << it }
app.resourceLoader.resources = resources

def suite = new TestSuite()

populateTestSuite(suite, testFiles, classLoader, ctx)
Expand Down
24 changes: 1 addition & 23 deletions scripts/Upgrade.groovy
Expand Up @@ -91,29 +91,7 @@ move it to the new location of '${basedir}/test/integration'. Please move the di
copy(file:"${grailsHome}/src/war/WEB-INF/applicationContext.xml",
tofile:"${basedir}/web-app/WEB-INF/applicationContext.xml", overwrite:true)

// These will still overwrite as the src name differs from target and the src name is not in target
if(!new File("${basedir}/grails-app/conf/log4j.development.properties").exists()) {
copy(tofile:"${basedir}/grails-app/conf/log4j.development.properties") {
fileset(file:"${grailsHome}/src/war/WEB-INF/log4j.properties") {
present(present:"srconly", targetdir:"${basedir}/grails-app/conf")
}
}
}
if(!new File("${basedir}/grails-app/conf/log4j.test.properties").exists()) {
copy(tofile:"${basedir}/grails-app/conf/log4j.test.properties") {
fileset(file:"${grailsHome}/src/war/WEB-INF/log4j.properties") {
present(present:"srconly", targetdir:"${basedir}/grails-app/conf")
}
}
}
if(!new File("${basedir}/grails-app/conf/log4j.production.properties").exists()) {
copy(tofile:"${basedir}/grails-app/conf/log4j.production.properties") {
fileset(file:"${grailsHome}/src/war/WEB-INF/log4j.properties") {
present(present:"srconly", targetdir:"${basedir}/grails-app/conf")
}
}
}

// These will still overwrite as the src name differs from target and the src name is not in target
if(!new File("${basedir}/grails-app/conf/${appClassName}UrlMappings.groovy").exists()) {
copy(tofile:"${basedir}/grails-app/conf/${appClassName}UrlMappings.groovy") {
fileset(file:"${grailsHome}/src/grails/templates/artifacts/UrlMappings.groovy") {
Expand Down
Expand Up @@ -245,7 +245,7 @@ private GroovyClassLoader configureClassLoader(
ClassLoader rootLoader = DefaultGroovyMethods.getRootLoader(contextLoader);
GroovyClassLoader cl;
if(rootLoader != null) {
cl = new GrailsClassLoader(rootLoader, resourceLoader);
cl = new GrailsClassLoader(contextLoader, resourceLoader);
}
else {
GrailsAwareClassLoader gcl = new GrailsAwareClassLoader(contextLoader);
Expand Down

0 comments on commit 052a570

Please sign in to comment.