Skip to content

Commit 7e319fa

Browse files
committed
i18n message bundle reloading in fixes in multi project builds
1 parent b0e5375 commit 7e319fa

File tree

4 files changed

+68
-60
lines changed

4 files changed

+68
-60
lines changed

grails-bootstrap/src/main/groovy/grails/util/BuildSettings.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ class BuildSettings {
297297
*/
298298
public static final String BUILD_CLASSES_PATH = "build/classes/main"
299299

300+
/**
301+
* The path to the build resources directory
302+
*/
303+
public static final String BUILD_RESOURCES_PATH = "build/resources/main"
304+
300305
public static final File SETTINGS_FILE = new File("${System.getProperty('user.home')}/.grails/settings.groovy")
301306

302307
}

grails-core/src/main/groovy/grails/boot/GrailsApp.groovy

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.grails.compiler.injection.GrailsAwareInjectionOperation
2121
import org.grails.core.util.BeanCreationProfilingPostProcessor
2222
import org.grails.io.watch.DirectoryWatcher
2323
import org.grails.io.watch.FileExtensionFileChangeListener
24+
import org.grails.plugins.BinaryGrailsPlugin
2425
import org.grails.plugins.support.WatchPattern
2526
import org.grails.spring.beans.factory.OptimizedAutowireCapableBeanFactory
2627
import org.springframework.beans.factory.support.DefaultListableBeanFactory
@@ -123,53 +124,46 @@ class GrailsApp extends SpringApplication {
123124
}
124125
})
125126

127+
def pluginManager = applicationContext.getBean(GrailsPluginManager)
126128
def pluginManagerListener = createPluginManagerListener(applicationContext)
127129
directoryWatcher.addListener(pluginManagerListener)
128130

129131
File baseDir = new File(location).canonicalFile
130132

131133
List<File> watchBaseDirectories = [baseDir]
132-
def parentDir = baseDir.parentFile
133-
File settingsFile = new File(parentDir, "settings.gradle")
134-
135-
if(settingsFile.exists()) {
136-
def cc = new CompilerConfiguration()
137-
cc.scriptBaseClass = SettingsFile.name
138-
def binding = new Binding()
139-
def shell = new GroovyShell(Thread.currentThread().contextClassLoader, binding, cc)
140-
try {
141-
shell.evaluate(settingsFile)
142-
} catch (Throwable e) {
143-
// ignore
144-
}
145-
def projectPaths = binding.getVariables().get('projectPaths')
146-
if(projectPaths) {
147-
for(path in projectPaths) {
148-
if(path) {
149-
150-
def pathStr = path.toString()
151-
if(pathStr.startsWith(':')) {
152-
pathStr = pathStr.substring(1)
153-
}
154-
watchBaseDirectories << new File(parentDir, pathStr)
155-
}
134+
for(GrailsPlugin plugin in pluginManager.allPlugins) {
135+
if(plugin instanceof BinaryGrailsPlugin) {
136+
BinaryGrailsPlugin binaryGrailsPlugin = (BinaryGrailsPlugin)plugin
137+
def pluginDirectory = binaryGrailsPlugin.projectDirectory
138+
if(pluginDirectory != null) {
139+
watchBaseDirectories << pluginDirectory
156140
}
157141
}
158142
}
159143

160144
def locationLength = location.length() + 1
161-
def pluginManager = applicationContext.getBean(GrailsPluginManager)
145+
162146
for(GrailsPlugin plugin in pluginManager.allPlugins) {
163-
for(WatchPattern wp in plugin.watchedResourcePatterns) {
164-
for(watchBase in watchBaseDirectories) {
165-
if(wp.file) {
166-
def resolvedPath = new File(watchBase, wp.file.path.substring(locationLength))
167-
directoryWatcher.addWatchFile(resolvedPath)
168-
}
169-
else if(wp.directory && wp.extension) {
147+
def watchedResourcePatterns = plugin.getWatchedResourcePatterns()
148+
if(watchedResourcePatterns != null) {
149+
150+
for(WatchPattern wp in new ArrayList<WatchPattern>(watchedResourcePatterns)) {
151+
boolean first = true
152+
for(watchBase in watchBaseDirectories) {
153+
if(!first) {
154+
// the base project will already been in the list of watch patterns, but we add any subprojects here
155+
plugin.watchedResourcePatterns.add(new WatchPattern(directory: watchBase, extension: wp.extension))
156+
}
157+
first = false
158+
if(wp.file) {
159+
def resolvedPath = new File(watchBase, wp.file.path.substring(locationLength))
160+
directoryWatcher.addWatchFile(resolvedPath)
161+
}
162+
else if(wp.directory && wp.extension) {
170163

171-
def resolvedPath = new File(watchBase, wp.directory.path.substring(locationLength))
172-
directoryWatcher.addWatchDirectory(resolvedPath, wp.extension)
164+
def resolvedPath = new File(watchBase, wp.directory.path.substring(locationLength))
165+
directoryWatcher.addWatchDirectory(resolvedPath, wp.extension)
166+
}
173167
}
174168
}
175169
}

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,30 +212,32 @@ public Properties getProperties(final Locale locale) {
212212
try {
213213
// first load all properties
214214
Resource[] resources = resolver.getResources('*' + PROPERTIES_EXTENSION);
215-
resources = filterResources(resources, locale);
216-
properties = new Properties();
217-
218-
// message bundles are locale specific. The more underscores the locale has the more specific the locale
219-
// so we order by the number of underscores present so that the most specific appears
220-
Arrays.sort(resources, new Comparator<Resource>() {
221-
@Override
222-
public int compare(Resource o1, Resource o2) {
223-
String f1 = o1.getFilename();
224-
String f2 = o2.getFilename();
225-
226-
int firstUnderscoreCount = StringUtils.countOccurrencesOf(f1, "_");
227-
int secondUnderscoreCount = StringUtils.countOccurrencesOf(f2, "_");
228-
229-
if(firstUnderscoreCount == secondUnderscoreCount) {
230-
return 0;
215+
resources = resources.length > 0 ? filterResources(resources, locale) : resources;
216+
if(resources.length > 0) {
217+
properties = new Properties();
218+
219+
// message bundles are locale specific. The more underscores the locale has the more specific the locale
220+
// so we order by the number of underscores present so that the most specific appears
221+
Arrays.sort(resources, new Comparator<Resource>() {
222+
@Override
223+
public int compare(Resource o1, Resource o2) {
224+
String f1 = o1.getFilename();
225+
String f2 = o2.getFilename();
226+
227+
int firstUnderscoreCount = StringUtils.countOccurrencesOf(f1, "_");
228+
int secondUnderscoreCount = StringUtils.countOccurrencesOf(f2, "_");
229+
230+
if(firstUnderscoreCount == secondUnderscoreCount) {
231+
return 0;
232+
}
233+
else {
234+
return firstUnderscoreCount > secondUnderscoreCount ? 1 : -1;
235+
}
231236
}
232-
else {
233-
return firstUnderscoreCount > secondUnderscoreCount ? 1 : -1;
234-
}
235-
}
236-
});
237+
});
237238

238-
loadFromResources(properties, resources);
239+
loadFromResources(properties, resources);
240+
}
239241
} catch (IOException e) {
240242
return null;
241243
}

grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,20 @@ class I18nGrailsPlugin extends Plugin {
106106

107107
def nativeascii = application.config.getProperty('grails.enable.native2ascii', Boolean, true)
108108
def resourcesDir = BuildSettings.RESOURCES_DIR
109+
def classesDir = BuildSettings.CLASSES_DIR
110+
109111
if (resourcesDir.exists() && event.source instanceof Resource) {
110-
def eventFile = event.source.file.canonicalFile
112+
File eventFile = event.source.file.canonicalFile
111113
def ant = getClass().classLoader.loadClass("groovy.util.AntBuilder").newInstance()
112-
File i18nDir = new File("${Environment.current.reloadLocation}/grails-app/i18n").canonicalFile
114+
File i18nDir = eventFile.parentFile
113115
if (isChildOfFile(eventFile, i18nDir)) {
114-
executeMessageBundleCopy(ant, eventFile, i18nDir, BuildSettings.RESOURCES_DIR, nativeascii)
115-
executeMessageBundleCopy(ant, eventFile, i18nDir, BuildSettings.CLASSES_DIR, nativeascii)
116+
if( i18nDir.name == 'i18n' && i18nDir.parentFile.name == 'grails-app') {
117+
def appDir = i18nDir.parentFile.parentFile
118+
resourcesDir = new File(appDir, BuildSettings.BUILD_RESOURCES_PATH)
119+
classesDir = new File(appDir, BuildSettings.BUILD_CLASSES_PATH)
120+
}
121+
executeMessageBundleCopy(ant, eventFile, i18nDir, resourcesDir, nativeascii)
122+
executeMessageBundleCopy(ant, eventFile, i18nDir, classesDir, nativeascii)
116123
}
117124
}
118125

0 commit comments

Comments
 (0)