Created
September 8, 2021 13:29
-
-
Save farism/ef7838b10c396d4e690a0619bd55ff13 to your computer and use it in GitHub Desktop.
watcher.nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## create a nimble dev task | |
## | |
## task dev, "run watcher": | |
## exec "nim c -r watcher.nim <src> <binary>" | |
import osproc, os, tables, times | |
let | |
dir = if paramCount() > 0: paramStr(1) else: "src" | |
bin = if paramCount() > 1: paramStr(2) else: "client" | |
proc start(): Process = | |
discard execCmd "nimble build" | |
startProcess("./" & bin, "", [], nil, {poEvalCommand, poStdErrToStdOut, | |
poParentStreams}) | |
proc getLastWrite(): Table[string, Time] = | |
for f in walkDirRec(dir): | |
result[f] = getFileInfo(f).lastWriteTime | |
var | |
process = start() | |
lastWrite = getLastWrite() | |
proc restart() = | |
if not process.isNil: | |
process.kill() | |
process.close() | |
process = start() | |
while true: | |
let t = getLastWrite() | |
if t != lastWrite: | |
echo "Restarting" | |
lastWrite = t | |
restart() | |
sleep(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could probably parse nimble config to get a better default binary name