Skip to content

Commit ebbfba7

Browse files
committed
Support TemplateRenderer.template() to find specific the root directory of the templates
Closes gh-766
1 parent a112414 commit ebbfba7

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

grace-cli/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
dependencies {
22
api project(":grace-api")
33
api project(":grace-bootstrap")
4+
compileOnly libs.jansi
5+
compileOnly libs.jline
46

57
api libs.groovy.templates
68
}

grace-cli/src/main/groovy/grails/dev/commands/template/TemplateRenderer.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import org.grails.io.support.Resource
2525
* API for locating and rendering templates in the code generation layer
2626
*
2727
* @author Graeme Rocher
28+
* @author Michael Yan
2829
* @since 3.0
2930
*/
3031
interface TemplateRenderer {
@@ -162,6 +163,15 @@ interface TemplateRenderer {
162163
*/
163164
Iterable<Resource> templates(String pattern)
164165

166+
/**
167+
* Find a template at the given location
168+
*
169+
* @param templateRoot The template root
170+
* @param location The location
171+
* @return The resource or null if it doesn't exist
172+
*/
173+
Resource template(String templateRoot, Object location)
174+
165175
/**
166176
* Find a template at the given location
167177
*

grace-cli/src/main/groovy/grails/dev/commands/template/TemplateRendererImpl.groovy

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import groovy.text.Template
2222
import groovy.transform.CompileDynamic
2323
import groovy.transform.CompileStatic
2424

25+
import grails.build.logging.GrailsConsole
2526
import grails.codegen.model.Model
2627
import grails.dev.commands.io.FileSystemInteraction
2728
import grails.dev.commands.io.FileSystemInteractionImpl
@@ -34,6 +35,7 @@ import org.grails.io.support.ResourceLoader
3435
* API for locating and rendering templates in the code generation layer
3536
*
3637
* @author Graeme Rocher
38+
* @author Michael Yan
3739
* @since 3.2
3840
*/
3941
@CompileStatic
@@ -44,6 +46,8 @@ class TemplateRendererImpl implements TemplateRenderer {
4446

4547
protected Map<String, Template> templateCache = [:]
4648

49+
GrailsConsole console = GrailsConsole.getInstance()
50+
4751
TemplateRendererImpl(File baseDir, ResourceLoader resourceLoader = new DefaultResourceLoader()) {
4852
this.fileSystemInteraction = new FileSystemInteractionImpl(baseDir, resourceLoader)
4953
}
@@ -123,7 +127,7 @@ class TemplateRendererImpl implements TemplateRenderer {
123127
void render(File template, File destination, Map model = Collections.emptyMap(), boolean overwrite = false) {
124128
if (template && destination) {
125129
if (destination.exists() && !overwrite) {
126-
println("Warning | Destination file ${projectPath(destination)} already exists, skipping...")
130+
this.console.addStatus('skip '.padLeft(13), projectPath(destination), "YELLOW")
127131
}
128132
else {
129133
Template t = templateCache[template.absolutePath]
@@ -138,8 +142,13 @@ class TemplateRendererImpl implements TemplateRenderer {
138142
}
139143
}
140144
try {
145+
if (destination.exists() && overwrite) {
146+
this.console.addStatus('force '.padLeft(13), projectPath(destination), "YELLOW")
147+
}
148+
else {
149+
this.console.addStatus('create '.padLeft(13), projectPath(destination), "GREEN")
150+
}
141151
writeTemplateToDestination(t, model, destination)
142-
println("Rendered template ${template.name} to destination ${projectPath(destination)}")
143152
}
144153
catch (Throwable e) {
145154
destination.delete()
@@ -171,7 +180,7 @@ class TemplateRendererImpl implements TemplateRenderer {
171180
void render(Resource template, File destination, Map model = Collections.emptyMap(), boolean overwrite = false) {
172181
if (template && destination) {
173182
if (destination.exists() && !overwrite) {
174-
println("Warning | Destination file ${projectPath(destination)} already exists, skipping...")
183+
this.console.addStatus('skip '.padLeft(13), projectPath(destination), "YELLOW")
175184
}
176185
else if (!template?.exists()) {
177186
throw new TemplateException("Template [$template.filename] not found.")
@@ -200,8 +209,13 @@ class TemplateRendererImpl implements TemplateRenderer {
200209
}
201210
if (t != null) {
202211
try {
212+
if (destination.exists() && overwrite) {
213+
this.console.addStatus('force '.padLeft(13), projectPath(destination), "YELLOW")
214+
}
215+
else {
216+
this.console.addStatus('create '.padLeft(13), projectPath(destination), "GREEN")
217+
}
203218
writeTemplateToDestination(t, model, destination)
204-
println("Rendered template ${template.filename} to destination ${projectPath(destination)}")
205219
}
206220
catch (Throwable e) {
207221
destination.delete()
@@ -229,17 +243,28 @@ class TemplateRendererImpl implements TemplateRenderer {
229243
/**
230244
* Find a template at the given location
231245
*
246+
* @param templateRoot The template root
232247
* @param location The location
233248
* @return The resource or null if it doesn't exist
234249
*/
235-
Resource template(Object location) {
236-
Resource f = resource(file("src/main/templates/$location"))
250+
Resource template(String templateRoot, Object location) {
251+
Resource f = resource(file("src/main/$templateRoot/$location"))
237252
if (!f?.exists()) {
238-
return resource("classpath*:META-INF/templates/$location")
253+
return resource("classpath*:META-INF/$templateRoot/$location")
239254
}
240255
resource(f)
241256
}
242257

258+
/**
259+
* Find a template at the given location
260+
*
261+
* @param location The location
262+
* @return The resource or null if it doesn't exist
263+
*/
264+
Resource template(Object location) {
265+
template('templates', location)
266+
}
267+
243268
protected static void writeTemplateToDestination(Template template, Map model, File destination) {
244269
destination.parentFile.mkdirs()
245270
destination.withWriter { BufferedWriter w ->

0 commit comments

Comments
 (0)