Skip to content

Commit

Permalink
Add template change watcher for livereload (#398)
Browse files Browse the repository at this point in the history
Include Spring Boot DevTools dependency for improved development experience. Implement a file system watcher to detect template changes and refresh the application context automatically in development mode.
  • Loading branch information
tschuehly authored Oct 26, 2024
1 parent c3c3e46 commit 84954de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jte-spring-boot-starter-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<artifactId>spring-webflux</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import gg.jte.resolve.DirectoryCodeResolver;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.devtools.filewatch.FileSystemWatcher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;

import java.nio.file.FileSystems;
Expand All @@ -24,6 +31,9 @@ public JteAutoConfiguration(JteProperties jteProperties) {
this.jteProperties = jteProperties;
}


Logger logger = LoggerFactory.getLogger(JteAutoConfiguration.class);

@Bean
@ConditionalOnMissingBean(JteViewResolver.class)
public JteViewResolver jteViewResolver(TemplateEngine templateEngine) {
Expand All @@ -49,4 +59,21 @@ public TemplateEngine jteTemplateEngine() {
}
throw new JteConfigurationException("You need to either set gg.jte.usePrecompiledTemplates or gg.jte.developmentMode to true ");
}

@Bean
@ConditionalOnProperty(name = "gg.jte.developmentMode", havingValue = "true")
FileSystemWatcher viewComponentFileSystemWatcher(ApplicationContext applicationContext, JteProperties jteProperties) {
var fileSystemWatcher = new FileSystemWatcher();
File jteDir = new File(jteProperties.getTemplateLocation());
fileSystemWatcher.addSourceDirectory(jteDir);
logger.info("Watching for template changes at: {}", jteDir.getAbsoluteFile().getPath());
fileSystemWatcher.addListener(
changeSet -> {
logger.debug("Detected change to template: {}", changeSet);
applicationContext.publishEvent(new ContextRefreshedEvent(applicationContext));
}
);
fileSystemWatcher.start();
return fileSystemWatcher;
}
}

0 comments on commit 84954de

Please sign in to comment.