Skip to content

Commit

Permalink
Grails Boot: Refine GrailsDevelopmentModeWatchApplicationContextIniti…
Browse files Browse the repository at this point in the history
…alizer, use BuildSettings.GRAILS_APP_DIR
  • Loading branch information
rainboyan committed May 24, 2023
1 parent f2d14ec commit 9bd27ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class GrailsDevelopmentModeWatchApplicationContextInitializer implements

private static final Log logger = LogFactory.getLog(GrailsDevelopmentModeWatchApplicationContextInitializer.class);

private static final String SOURCE_MAIN_JAVA = "src/main/java";
private static final String SOURCE_MAIN_GROOVY = "src/main/groovy";

private boolean developmentModeActive = false;
private DirectoryWatcher directoryWatcher;

Expand Down Expand Up @@ -238,11 +241,9 @@ else if (i == 1) {
changedFile = changedFile.getCanonicalFile();
// Groovy files within the 'conf' directory are not compiled
boolean configFileChanged = false;
for (String dir : Arrays.asList("grails-app", "app")) {
String confPath = File.separator + dir + File.separator + "conf" + File.separator;
if (changedFile.getPath().contains(confPath)) {
configFileChanged = true;
}
String confPath = new File(BuildSettings.GRAILS_APP_DIR, "conf").getAbsolutePath();
if (changedFile.getPath().contains(confPath)) {
configFileChanged = true;
}
if (configFileChanged) {
pluginManager.informOfFileChange(changedFile);
Expand Down Expand Up @@ -286,18 +287,25 @@ private void setDevelopmentModeActive(boolean active) {
private void recompile(File changedFile, CompilerConfiguration compilerConfig, String location) {
String changedPath = changedFile.getPath();

String sourceMainGroovy = "src" + File.separator + "main" + File.separator + "groovy";
String grailsAppFullPath = BuildSettings.GRAILS_APP_DIR.getAbsolutePath();
String grailsAppPath = grailsAppFullPath.substring(grailsAppFullPath.lastIndexOf(File.separator) + 1);

File appDir = null;
for (String dir : Arrays.asList("grails-app", "app", sourceMainGroovy)) {
boolean sourceFileChanged = false;
for (String dir : Arrays.asList(grailsAppPath, SOURCE_MAIN_JAVA, SOURCE_MAIN_GROOVY)) {
String changedDir = File.separator + dir;
if (changedPath.contains(changedDir)) {
appDir = new File(changedPath.substring(0, changedPath.indexOf(changedDir)));
sourceFileChanged = true;
break;
}
}

String baseFileLocation = appDir != null ? appDir.getAbsolutePath() : location;
if (!sourceFileChanged) {
return;
}

String baseFileLocation = appDir.getAbsolutePath();
compilerConfig.setTargetDirectory(new File(baseFileLocation, BuildSettings.BUILD_CLASSES_PATH));

logger.debug(String.format("WatchService found file changed%nRecompiling... [%s]%n", changedFile));
Expand Down Expand Up @@ -359,7 +367,9 @@ public void onNew(File file) {
}

private void configureDirectoryWatcher(DirectoryWatcher directoryWatcher, String location) {
for (String dir : Arrays.asList("grails-app", "app", "src/main/groovy", "src/main/java")) {
String grailsAppFullPath = BuildSettings.GRAILS_APP_DIR.getAbsolutePath();
String grailsAppPath = grailsAppFullPath.substring(grailsAppFullPath.lastIndexOf(File.separator) + 1);
for (String dir : Arrays.asList(grailsAppPath, SOURCE_MAIN_JAVA, SOURCE_MAIN_GROOVY)) {
directoryWatcher.addWatchDirectory(new File(location, dir), Arrays.asList("groovy", "java"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,22 @@ class BuildSettings {
RESOURCES_DIR = new File(projectResourceDir)
}
else {
RESOURCES_DIR = new File(TARGET_DIR, 'resources/main')
RESOURCES_DIR = new File(BASE_DIR, 'build/resources/main')
}

String projectClassDir = System.getProperty(PROJECT_CLASSES_DIR)
if (projectClassDir) {
CLASSES_DIR = new File(projectClassDir)
BUILD_CLASSES_PATH = projectClassDir
BUILD_CLASSES_PATH = 'build/classes/groovy/main'
}
else {
File groovyDir = new File(TARGET_DIR, 'classes/groovy/main')
File groovyDir = new File(BASE_DIR, 'build/classes/groovy/main')
if (groovyDir.exists()) {
CLASSES_DIR = groovyDir
BUILD_CLASSES_PATH = 'build/classes/groovy/main'
}
else {
CLASSES_DIR = new File(TARGET_DIR, 'classes/main')
CLASSES_DIR = new File(BASE_DIR, 'build/classes/main')
BUILD_CLASSES_PATH = 'build/classes/main'
}
}
Expand Down

0 comments on commit 9bd27ee

Please sign in to comment.