Skip to content

Commit 3af1321

Browse files
committed
Update to support a plugin.groovy in grails plugins
1 parent 581f3de commit 3af1321

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

grails-core/src/main/groovy/org/grails/core/cfg/GroovyConfigPropertySourceLoader.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ class GroovyConfigPropertySourceLoader implements PropertySourceLoader {
4141
final String[] fileExtensions = ['groovy'] as String[]
4242

4343
@Override
44-
PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
44+
PropertySource<?> load(String name, Resource resource, String profile) {
45+
load(name, resource, profile, [])
46+
}
47+
48+
PropertySource<?> load(String name, Resource resource, String profile, List<String> filteredKeys) throws IOException {
4549
def env = Environment.current.name
4650
if(profile == null || env == profile) {
4751

@@ -55,6 +59,11 @@ class GroovyConfigPropertySourceLoader implements PropertySourceLoader {
5559
appVersion: Metadata.getCurrent().getApplicationVersion() )
5660
try {
5761
def configObject = configSlurper.parse(resource.URL)
62+
63+
filteredKeys?.each { key ->
64+
configObject.remove(key)
65+
}
66+
5867
def propertySource = new NavigableMap()
5968
propertySource.merge(configObject, false)
6069
return new NavigableMapPropertySource(name, propertySource)

grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import grails.io.IOUtils;
2121
import grails.plugins.GrailsPlugin;
2222
import grails.plugins.GrailsPluginManager;
23+
import grails.util.Environment;
2324
import grails.util.GrailsNameUtils;
2425
import groovy.lang.GroovyObjectSupport;
26+
import groovy.util.ConfigSlurper;
2527
import org.grails.config.yaml.YamlPropertySourceLoader;
2628
import org.grails.core.AbstractGrailsClass;
29+
import org.grails.core.cfg.GroovyConfigPropertySourceLoader;
2730
import org.grails.core.legacy.LegacyGrailsApplication;
2831
import org.grails.plugins.support.WatchPattern;
2932
import org.slf4j.Logger;
@@ -49,6 +52,8 @@ public abstract class AbstractGrailsPlugin extends GroovyObjectSupport implement
4952

5053
public static final String PLUGIN_YML = "plugin.yml";
5154
public static final String PLUGIN_YML_PATH = "/" + PLUGIN_YML;
55+
public static final String PLUGIN_GROOVY = "plugin.groovy";
56+
public static final String PLUGIN_GROOVY_PATH = "/" + PLUGIN_GROOVY;
5257
private static final List<String> DEFAULT_CONFIG_IGNORE_LIST = Arrays.asList("dataSource", "hibernate");
5358
private static Resource basePluginResource = null;
5459
protected PropertySource<?> propertySource;
@@ -84,12 +89,19 @@ public AbstractGrailsPlugin(Class<?> pluginClass, GrailsApplication application)
8489
this.grailsApplication = application;
8590
this.pluginClass = pluginClass;
8691
Resource resource = readPluginConfiguration(pluginClass);
92+
8793
if(resource != null && resource.exists()) {
88-
YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
94+
final String filename = resource.getFilename();
8995
try {
90-
this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-plugin.yml", resource, null, false, DEFAULT_CONFIG_IGNORE_LIST);
96+
if (filename.equals(PLUGIN_YML)) {
97+
YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
98+
this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-" + PLUGIN_YML, resource, null, true, DEFAULT_CONFIG_IGNORE_LIST);
99+
} else if (filename.equals(PLUGIN_GROOVY)) {
100+
GroovyConfigPropertySourceLoader propertySourceLoader = new GroovyConfigPropertySourceLoader();
101+
this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-" + PLUGIN_GROOVY, resource, null, DEFAULT_CONFIG_IGNORE_LIST);
102+
}
91103
} catch (IOException e) {
92-
LOG.warn("Error loading plugin.yml for plugin: " + pluginClass.getName() +": " + e.getMessage(), e);
104+
LOG.warn("Error loading " + filename + " for plugin: " + pluginClass.getName() +": " + e.getMessage(), e);
93105
}
94106
}
95107
}
@@ -114,10 +126,21 @@ public boolean isEnabled(String[] profiles) {
114126

115127
protected Resource readPluginConfiguration(Class<?> pluginClass) {
116128
final URL urlToPluginYml = IOUtils.findResourceRelativeToClass(pluginClass, PLUGIN_YML_PATH);
129+
final URL urlToPluginGroovy = IOUtils.findResourceRelativeToClass(pluginClass, PLUGIN_GROOVY_PATH);
130+
131+
Resource ymlUrlResource = urlToPluginYml != null ? new UrlResource(urlToPluginYml) : null;
132+
Resource groovyUrlResource = urlToPluginGroovy != null ? new UrlResource(urlToPluginGroovy) : null;
133+
134+
Boolean groovyUrlResourceExists = groovyUrlResource != null && groovyUrlResource.exists();
117135

118-
Resource urlResource = urlToPluginYml != null ? new UrlResource(urlToPluginYml) : null;
119-
if(urlResource != null && urlResource.exists()) {
120-
return urlResource;
136+
if(ymlUrlResource != null && ymlUrlResource.exists()) {
137+
if (groovyUrlResourceExists) {
138+
throw new RuntimeException("A plugin may define a plugin.yml or a plugin.groovy, but not both");
139+
}
140+
return ymlUrlResource;
141+
}
142+
if(groovyUrlResourceExists) {
143+
return groovyUrlResource;
121144
}
122145
return null;
123146
}

0 commit comments

Comments
 (0)