I’m trying to contribute to a plugin for hytale ye...
# intellij-plugins
m
I’m trying to contribute to a plugin for hytale yet I am struggling quite a bit with file syncing stuff. This is my first time developing with the plugin and would love to have some help. The goal is to have 2 folder in sync both ways and eventually also when it notices changes in the java or kotlin it trigger a build and reload on the hytale server. The code will be in the thread yet I would love some advice or ways I could figure it out. The biggest problem I see is currently that there are missing folders which it should create while syncing if it doesn’t exist. Sadly I thought I had test for it to cover it yet it seems it does not cover it. (Small note, I’m a bottom-up thinker. If I miss details I have difficulties seeing the solution)
Copy code
class 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 refactoring
Copy code
class 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())
    }
}