maarten ha
03/30/2026, 8:49 PMmaarten ha
03/30/2026, 8:49 PMclass HotReloadListener(
private val project: Project,
): BulkFileListener {
private val projectBasePath: String? get() = project.basePath
override fun after(events: ListVFileEvent) {
val basePath = projectBasePath ?: return
for (event in events) {
val file = event.file ?: continue
val path = file.path
if (event.requestor == this) continue
if (path.contains("src/main/resources")) {
val serverFilePath = path.substringAfter("src/main/resources/")
syncFile(Path(path), Path("$basePath/server/mods/test/$serverFilePath"))
printInfo("shit changed in here")
}
if (path.contains("server/mods/test/")) {
val resourceFilePath = path.substringAfter("server/mods/test/")
syncFile(Path(path), Path("$basePath/src/main/resources/$resourceFilePath"))
printInfo("server mods")
}
if (path.contains("src/main/java") || path.contains("src/main/kotlin")) {
printInfo("java changed")
}
printInfo("We are getting closer")
}
}
private fun syncFile(source: Path, target: Path) {
try{
val sourceFile = source.toFile()
if (!sourceFile.exists()) return
val targetFile = target.toFile()
if (!targetFile.parentFile.exists()) {
targetFile.parentFile.mkdirs()
//target.createParentDirectories()
}
source.copyTo(target, overwrite = true)
}catch(e: Exception){
com.intellij.openapi.diagnostic.Logger.getInstance(HotReloadListener::class.java).error(e)
}
}
}
It is messy code and yet I want to have it working first before refactoringmaarten ha
03/30/2026, 8:52 PMclass HotReloadListenerTest: BasePlatformTestCase() {
fun testHotReload() {
// 1. Manually register your listener for the test
val listener = HotReloadListener(project)
project.messageBus.connect(testRootDisposable).subscribe(VirtualFileManager.VFS_CHANGES, listener)
// 2. Create a "source" file in the mock project
// Use a path that includes "src/main/resources"
val relPath = "src/main/resources/config.json"
// Ensure the file is actually on disk so nio.Path can see it
val basePath = myFixture.project.basePath!!
val sourceFile = Path(basePath, relPath)
sourceFile.parent.toFile().mkdirs()
sourceFile.toFile().writeText("{ 'key': 'value' }")
// Refresh VFS so IntelliJ knows about it
val virtualFile = VfsTestUtil.findFileByCaseSensitivePath(sourceFile.toString())!!
// 3. Simulate a VFS Change Event
VfsTestUtil.createFile(virtualFile.parent, "config.json", "{ 'key': 'new_value' }")
// 4. Assert the file exists in the "server" folder
val expectedServerFile = Path(basePath, "server/mods/test/config.json")
assertTrue("File should have been synced to server folder: $expectedServerFile", expectedServerFile.exists())
assertEquals("{ 'key': 'new_value' }", expectedServerFile.readText())
}
fun testReverseHotReload() {
val listener = HotReloadListener(project)
project.messageBus.connect(testRootDisposable).subscribe(VirtualFileManager.VFS_CHANGES, listener)
val basePath = myFixture.project.basePath!!
val relPath = "server/mods/test/mod-config.json"
val sourceFile = Path(basePath, relPath)
sourceFile.parent.toFile().mkdirs()
sourceFile.toFile().writeText("{ 'mod': 'data' }")
val virtualFile = VfsTestUtil.findFileByCaseSensitivePath(sourceFile.toString())!!
VfsTestUtil.createFile(virtualFile.parent, "mod-config.json", "{ 'mod': 'updated_data' }")
val expectedResourceFile = Path(basePath, "src/main/resources/mod-config.json")
assertTrue("File should have been synced back to resources folder: $expectedResourceFile", expectedResourceFile.exists())
assertEquals("{ 'mod': 'updated_data' }", expectedResourceFile.readText())
}
}