From 04d8bb8fbf0ba0358c0563c16103a60cc0c55260 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 22 Nov 2020 12:07:14 +0100 Subject: [PATCH] Fixed compiler watch flag crashing when not used on a subdirectory. Fixes #20 --- compiler/src/prog8/CompilerMain.kt | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 7e2c8aa83..da734cdb2 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -55,6 +55,8 @@ private fun compileMain(args: Array) { exitProcess(1) } + // TODO: make watching work with multiple source files + if(watchMode && moduleFiles.size<=1) { val watchservice = FileSystems.getDefault().newWatchService() @@ -68,15 +70,24 @@ private fun compileMain(args: Array) { for (importedFile in compilationResult.importedFiles) { print(" ") println(importedFile) - importedFile.parent.register(watchservice, StandardWatchEventKinds.ENTRY_MODIFY) + val watchDir = importedFile.parent ?: Path.of(".") + watchDir.register(watchservice, StandardWatchEventKinds.ENTRY_MODIFY) } println("[${LocalDateTime.now().withNano(0)}] Waiting for file changes.") - val event = watchservice.take() - for(changed in event.pollEvents()) { - val changedPath = changed.context() as Path - println(" change detected: $changedPath") + + var recompile=false + while(!recompile) { + val event = watchservice.take() + for (changed in event.pollEvents()) { + val changedPath = changed.context() as Path + if(compilationResult.importedFiles.any { it.fileName == changedPath.fileName }) { + println(" change detected: $changedPath") + recompile = true + } + } + event.reset() } - event.reset() + println("\u001b[H\u001b[2J") // clear the screen } catch (x: Exception) { throw x