Skip to content

Instantly share code, notes, and snippets.

@farism
Created September 8, 2021 13:29
Show Gist options
  • Save farism/ef7838b10c396d4e690a0619bd55ff13 to your computer and use it in GitHub Desktop.
Save farism/ef7838b10c396d4e690a0619bd55ff13 to your computer and use it in GitHub Desktop.
watcher.nim
## 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)
@farism
Copy link
Author

farism commented Sep 8, 2021

could probably parse nimble config to get a better default binary name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment