Compilation watcher finishd

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent 20c183910d
commit 6ba5742e8f

View File

@@ -1,18 +1,52 @@
import times, posix, strutils, os import times, posix, strutils, os
import strformat import strformat
import times import times
import libfswatch import std/[streams, osproc, md5, sequtils, terminal]
import std/[streams, osproc]
import libfswatch import fswatch
import libfswatch/fswatch
# import libfswatch
# import libfswatch/fswatch
let file = "/home/floscr/Code/Projects/org-parser/src/js/example/sandbox.nim" let file = "/home/floscr/Code/Projects/org-parser/src/js/example/sandbox.nim"
var ps: seq[Process] type pT = tuple[md5: string, process: Process]
var ps: seq[pT]
var data = ""
type echoT = enum
Error
Success
Info
proc errorEcho(x: string) = styledEcho(styleBright, fgRed, x)
proc successEcho(x: string) = styledEcho(styleBright, fgGreen, x)
proc infoEcho(x: string) = styledEcho(x)
proc timedEcho(str: string, messageType = Info) =
let time = times.now().format("yyyy-MM-dd HH:MM:ss")
let echoFn = case messageType:
of Error: errorEcho
of Success: successEcho
else: infoEcho
echoFn(&"""[{time}]: {str}""")
proc compileEcho(p: Process) {.thread.} =
var line: string
while p.running() and p.outputStream.readLine(line):
echo line
let exitStatus = p.peekExitCode()
case exitStatus:
of 0: timedEcho("Compilation Succeded", Success)
else: timedEcho("COMPILATION ERRORED!!", Error)
p.close()
proc compile(path: string) = proc compile(path: string) =
echo "Compiling..." timedEcho("Compiling...\n")
let p = startProcess( let p = startProcess(
"nim", "nim",
args= [ args= [
@@ -27,28 +61,23 @@ proc compile(path: string) =
], ],
options = {poUsePath, poStdErrToStdOut} options = {poUsePath, poStdErrToStdOut}
) )
ps.add(p) let sha = path.readFile.getMD5()
ps.add((sha, p))
compileEcho(p)
discard p.waitForExit() discard p.waitForExit()
var line: string proc monitorCallback(eg: EventGroup) =
while p.running() and p.outputStream.readLine(line): for e in eg:
echo line if $e.kind == "Updated":
if not ps.anyIt(it.md5 == file.readFile.getMD5()):
for p in ps:
p.process.close()
ps = @[]
echo "Compilation Finished" compile(file)
p.close()
ps = @[]
proc monitorCallback(event: fsw_cevent, event_num: cuint) = var monitor = newMonitor(latency=1)
echo event_num monitor.add(file)
if event_num == 10: monitor.setCallback(monitorCallback)
echo &"""[{times.now().format("yyyy-MM-dd HH:MM:ss")}]: {event.path} was modified. Compiling...""" monitor.start()
for p in ps:
p.close()
ps = @[]
compile(file)
var mon = newMonitor()
mon.addPath(file)
mon.setCallback(monitorCallback)
mon.start()