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
2222import groovy.transform.CompileDynamic
2323import groovy.transform.CompileStatic
2424
25+ import grails.build.logging.GrailsConsole
2526import grails.codegen.model.Model
2627import grails.dev.commands.io.FileSystemInteraction
2728import 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/$t emplateRoot /$location " ))
237252 if (! f?. exists()) {
238- return resource(" classpath*:META-INF/templates /$location " )
253+ return resource(" classpath*:META-INF/$t emplateRoot /$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