Skip to content

Commit

Permalink
Support TemplateRenderer.template() to find specific the root direc…
Browse files Browse the repository at this point in the history
…tory of the templates

Closes gh-766
  • Loading branch information
rainboyan committed Nov 28, 2024
1 parent a112414 commit ebbfba7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions grace-cli/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dependencies {
api project(":grace-api")
api project(":grace-bootstrap")
compileOnly libs.jansi
compileOnly libs.jline

api libs.groovy.templates
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@ import org.grails.io.support.Resource
* API for locating and rendering templates in the code generation layer
*
* @author Graeme Rocher
* @author Michael Yan
* @since 3.0
*/
interface TemplateRenderer {
Expand Down Expand Up @@ -162,6 +163,15 @@ interface TemplateRenderer {
*/
Iterable<Resource> templates(String pattern)

/**
* Find a template at the given location
*
* @param templateRoot The template root
* @param location The location
* @return The resource or null if it doesn't exist
*/
Resource template(String templateRoot, Object location)

/**
* Find a template at the given location
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 the original author or authors.
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@ import groovy.text.Template
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic

import grails.build.logging.GrailsConsole
import grails.codegen.model.Model
import grails.dev.commands.io.FileSystemInteraction
import grails.dev.commands.io.FileSystemInteractionImpl
Expand All @@ -34,6 +35,7 @@ import org.grails.io.support.ResourceLoader
* API for locating and rendering templates in the code generation layer
*
* @author Graeme Rocher
* @author Michael Yan
* @since 3.2
*/
@CompileStatic
Expand All @@ -44,6 +46,8 @@ class TemplateRendererImpl implements TemplateRenderer {

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

GrailsConsole console = GrailsConsole.getInstance()

TemplateRendererImpl(File baseDir, ResourceLoader resourceLoader = new DefaultResourceLoader()) {
this.fileSystemInteraction = new FileSystemInteractionImpl(baseDir, resourceLoader)
}
Expand Down Expand Up @@ -123,7 +127,7 @@ class TemplateRendererImpl implements TemplateRenderer {
void render(File template, File destination, Map model = Collections.emptyMap(), boolean overwrite = false) {
if (template && destination) {
if (destination.exists() && !overwrite) {
println("Warning | Destination file ${projectPath(destination)} already exists, skipping...")
this.console.addStatus('skip '.padLeft(13), projectPath(destination), "YELLOW")
}
else {
Template t = templateCache[template.absolutePath]
Expand All @@ -138,8 +142,13 @@ class TemplateRendererImpl implements TemplateRenderer {
}
}
try {
if (destination.exists() && overwrite) {
this.console.addStatus('force '.padLeft(13), projectPath(destination), "YELLOW")
}
else {
this.console.addStatus('create '.padLeft(13), projectPath(destination), "GREEN")
}
writeTemplateToDestination(t, model, destination)
println("Rendered template ${template.name} to destination ${projectPath(destination)}")
}
catch (Throwable e) {
destination.delete()
Expand Down Expand Up @@ -171,7 +180,7 @@ class TemplateRendererImpl implements TemplateRenderer {
void render(Resource template, File destination, Map model = Collections.emptyMap(), boolean overwrite = false) {
if (template && destination) {
if (destination.exists() && !overwrite) {
println("Warning | Destination file ${projectPath(destination)} already exists, skipping...")
this.console.addStatus('skip '.padLeft(13), projectPath(destination), "YELLOW")
}
else if (!template?.exists()) {
throw new TemplateException("Template [$template.filename] not found.")
Expand Down Expand Up @@ -200,8 +209,13 @@ class TemplateRendererImpl implements TemplateRenderer {
}
if (t != null) {
try {
if (destination.exists() && overwrite) {
this.console.addStatus('force '.padLeft(13), projectPath(destination), "YELLOW")
}
else {
this.console.addStatus('create '.padLeft(13), projectPath(destination), "GREEN")
}
writeTemplateToDestination(t, model, destination)
println("Rendered template ${template.filename} to destination ${projectPath(destination)}")
}
catch (Throwable e) {
destination.delete()
Expand Down Expand Up @@ -229,17 +243,28 @@ class TemplateRendererImpl implements TemplateRenderer {
/**
* Find a template at the given location
*
* @param templateRoot The template root
* @param location The location
* @return The resource or null if it doesn't exist
*/
Resource template(Object location) {
Resource f = resource(file("src/main/templates/$location"))
Resource template(String templateRoot, Object location) {
Resource f = resource(file("src/main/$templateRoot/$location"))
if (!f?.exists()) {
return resource("classpath*:META-INF/templates/$location")
return resource("classpath*:META-INF/$templateRoot/$location")
}
resource(f)
}

/**
* Find a template at the given location
*
* @param location The location
* @return The resource or null if it doesn't exist
*/
Resource template(Object location) {
template('templates', location)
}

protected static void writeTemplateToDestination(Template template, Map model, File destination) {
destination.parentFile.mkdirs()
destination.withWriter { BufferedWriter w ->
Expand Down

0 comments on commit ebbfba7

Please sign in to comment.