Permalink
Browse files

fix for GRAILS-8818 "Dynamic scaffolding broken when templates loaded…

… from the class path instead of Grails (as happens with Maven plugin)"
  • Loading branch information...
1 parent 2640b0d commit 42bc55739de03b43b21c7166058c3cf4acae1c00 @graemerocher graemerocher committed Feb 23, 2012
@@ -32,6 +32,7 @@ import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.ResourceLoader
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.util.Assert
+import org.springframework.core.io.AbstractResource
/**
* Default implementation of the generator that generates grails artifacts (controllers, views etc.)
@@ -271,13 +272,20 @@ class DefaultGrailsTemplateGenerator implements GrailsTemplateGenerator, Resourc
}
- private getTemplateText(String template) {
+ public getTemplateText(String template) {
def application = grailsApplication
// first check for presence of template in application
if (resourceLoader && application?.warDeployed) {
return resourceLoader.getResource("/WEB-INF/templates/scaffolding/${template}").inputStream.text
}
+ AbstractResource templateFile = getTemplateResource(template)
+ if(templateFile.exists()) {
+ return templateFile.inputStream.getText()
+ }
+ }
+
+ AbstractResource getTemplateResource(String template) {
def templateFile = new FileSystemResource(new File("${basedir}/src/templates/scaffolding/${template}").absoluteFile)
if (!templateFile.exists()) {
@@ -294,12 +302,13 @@ class DefaultGrailsTemplateGenerator implements GrailsTemplateGenerator, Resourc
}
}
else {
+ if(template.startsWith('/')) {
+ template = template.substring(1)
+ }
templateFile = new ClassPathResource("src/grails/templates/scaffolding/${template}")
}
}
- if(templateFile.exists()) {
- return templateFile.inputStream.getText()
- }
+ return templateFile
}
def getTemplateNames() {
@@ -31,6 +31,8 @@
import org.codehaus.groovy.grails.commons.GrailsDomainClass;
import org.codehaus.groovy.grails.plugins.MockGrailsPluginManager;
import org.codehaus.groovy.grails.plugins.PluginManagerHolder;
+import org.springframework.core.io.AbstractResource;
+import org.springframework.core.io.ClassPathResource;
/**
* @author Graeme Rocher
@@ -53,6 +55,27 @@ protected void tearDown() {
PluginManagerHolder.setPluginManager(null);
}
+ public void testGetTemplateTextFromClasspath() {
+ BuildSettings bs = BuildSettingsHolder.getSettings();
+ BuildSettingsHolder.setSettings(null);
+ try {
+ DefaultGrailsTemplateGenerator generator = new DefaultGrailsTemplateGenerator();
+ generator.setBasedir("/not/there");
+ AbstractResource templateResource = generator.getTemplateResource("list.gsp");
+ assertTrue(templateResource instanceof ClassPathResource);
+ ClassPathResource cpr = (ClassPathResource) templateResource;
+ assertEquals("src/grails/templates/scaffolding/list.gsp", cpr.getPath());
+
+ templateResource = generator.getTemplateResource("/list.gsp");
+ assertTrue(templateResource instanceof ClassPathResource);
+ cpr = (ClassPathResource) templateResource;
+ assertEquals("src/grails/templates/scaffolding/list.gsp", cpr.getPath());
+ } finally {
+ BuildSettingsHolder.setSettings(bs);
+ }
+ }
+
+
public void testGenerateController() throws Exception {
DefaultGrailsTemplateGenerator generator;

0 comments on commit 42bc557

Please sign in to comment.