Watch option (#79)
* Update karun.nim * Watch option Add a -w flag to watch for changes in folder and recompile.
This commit is contained in:
parent
b16112613a
commit
16008dbe52
1 changed files with 42 additions and 6 deletions
|
|
@ -1,7 +1,14 @@
|
||||||
## Simple tool to quickly run Karax applications. Generates the HTML
|
## Simple tool to quickly run Karax applications. Generates the HTML
|
||||||
## required to run a Karax app and opens it in a browser.
|
## required to run a Karax app and opens it in a browser.
|
||||||
|
|
||||||
import os, strutils, parseopt, browsers
|
import os,
|
||||||
|
strutils,
|
||||||
|
parseopt,
|
||||||
|
browsers,
|
||||||
|
times,
|
||||||
|
tables
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const
|
const
|
||||||
css = """
|
css = """
|
||||||
|
|
@ -26,12 +33,23 @@ proc exec(cmd: string) =
|
||||||
if os.execShellCmd(cmd) != 0:
|
if os.execShellCmd(cmd) != 0:
|
||||||
quit "External command failed: " & cmd
|
quit "External command failed: " & cmd
|
||||||
|
|
||||||
|
proc build(name: string, rest: string, selectedCss: string, run: bool) =
|
||||||
|
echo("Building...")
|
||||||
|
createDir("nimcache")
|
||||||
|
exec("nim js --out:nimcache/" & name & ".js " & rest)
|
||||||
|
let dest = "nimcache" / name & ".html"
|
||||||
|
writeFile(dest, html % [name, selectedCss])
|
||||||
|
if run: openDefaultBrowser(dest)
|
||||||
|
|
||||||
proc main =
|
proc main =
|
||||||
var op = initOptParser()
|
var op = initOptParser()
|
||||||
var rest = op.cmdLineRest
|
var rest = op.cmdLineRest
|
||||||
var file = ""
|
var file = ""
|
||||||
var run = false
|
var run = false
|
||||||
var selectedCss = ""
|
var selectedCss = ""
|
||||||
|
var watch = false
|
||||||
|
var files: Table[string, Time] = {"path": getLastModificationTime(".")}.toTable
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
op.next()
|
op.next()
|
||||||
case op.kind
|
case op.kind
|
||||||
|
|
@ -48,15 +66,33 @@ proc main =
|
||||||
if op.key == "r":
|
if op.key == "r":
|
||||||
run = true
|
run = true
|
||||||
rest = rest.replace("-r ")
|
rest = rest.replace("-r ")
|
||||||
|
if op.key == "w":
|
||||||
|
watch = true
|
||||||
|
rest = rest.replace("-w ")
|
||||||
of cmdArgument: file = op.key
|
of cmdArgument: file = op.key
|
||||||
of cmdEnd: break
|
of cmdEnd: break
|
||||||
|
|
||||||
if file.len == 0: quit "filename expected"
|
if file.len == 0: quit "filename expected"
|
||||||
let name = file.splitFile.name
|
let name = file.splitFile.name
|
||||||
createDir("nimcache")
|
build(name, rest, selectedCss, run)
|
||||||
exec("nim js --out:nimcache/" & name & ".js " & rest)
|
echo("after build")
|
||||||
let dest = "nimcache" / name & ".html"
|
if watch:
|
||||||
writeFile(dest, html % [name, selectedCss])
|
# TODO: launch http server
|
||||||
if run: openDefaultBrowser(dest)
|
while true:
|
||||||
|
sleep(300)
|
||||||
|
for path in walkDirRec("."):
|
||||||
|
if ".git" in path:
|
||||||
|
continue
|
||||||
|
if files.hasKey(path):
|
||||||
|
if files[path] != getLastModificationTime(path):
|
||||||
|
echo("File changed: " & path)
|
||||||
|
build(name, rest, selectedCss, run)
|
||||||
|
files[path] = getLastModificationTime(path)
|
||||||
|
else:
|
||||||
|
files[path] = getLastModificationTime(path)
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue