Skip to content

Commit

Permalink
Clear template from templates table onDelete callback
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Dec 29, 2023
1 parent b6cd961 commit 3a2ecc9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
40 changes: 27 additions & 13 deletions src/tim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/tim
import std/json except `%*`
import std/times

import tim/engine/[meta, parser, compiler, logging]
import pkg/[watchout, kapsis/cli]

Expand All @@ -24,9 +26,12 @@ proc displayErrors(l: Logger) =
display(err)
display(l.filePath)

proc compileCode*(engine: Tim, tpl: TimTemplate) =
proc compileCode*(engine: Tim, tpl: TimTemplate, refreshAst = false) =
# Compiles `tpl` TimTemplate to either `.html` or binary `.ast`
var p: Parser = engine.newParser(tpl)
var tplView: TimTemplate
if tpl.getType == ttView:
tplView = tpl
var p: Parser = engine.newParser(tpl, tplView, refreshAst = refreshAst)
if likely(not p.hasError):
if tpl.jitEnabled():
# when enabled, will save the generated binary ast
Expand Down Expand Up @@ -57,13 +62,15 @@ proc precompile*(engine: Tim, callback: TimCallback = nil,
## are include-only files that can be imported into layouts
## or views via `@import` statement.
##
## By enabling flushing ensures outdated files are deleted.
## Note: Enable `flush` option to delete outdated files
if flush: engine.flush()
when not defined release:
when defined timHotCode:
var watchable: seq[string]
# Define callback procs for pkg/watchout
# Callback `onFound`
proc onFound(file: watchout.File) =
# echo indent(file.getName(), 3)
# Runs when detecting a new template.
let tpl: TimTemplate = engine.getTemplateByPath(file.getPath())
case tpl.getType
of ttView, ttLayout:
Expand All @@ -73,27 +80,34 @@ proc precompile*(engine: Tim, callback: TimCallback = nil,
echo err
# setLen(engine.errors, 0)
else: discard

# Callback `onChange`
proc onChange(file: watchout.File) =
# Runs when detecting changes
echo "✨ Changes detected"
echo indent(file.getName() & "\n", 3)
# echo toUnix(getTime())
let tpl: TimTemplate = engine.getTemplateByPath(file.getPath())
case tpl.getType()
of ttView, ttLayout:
engine.compileCode(tpl)
if engine.errors.len > 0:
for err in engine.errors:
echo err
# setLen(engine.errors, 0)
else:
discard
# echo "getting dependencies"

for path in tpl.getDeps:
let deptpl = engine.getTemplateByPath(path)
# echo indent($(ttView) / deptpl.getName(), 4)
engine.compileCode(deptpl, refreshAst = true)
if engine.errors.len > 0:
for err in engine.errors:
echo err
# Callback `onDelete`
proc onDelete(file: watchout.File) =
discard
# echo "✨ Deleted\n", file.getName()
# echo "✨ Tim Engine - Syncing Templates"
var w = newWatchout(@[engine.getSourcePath() / "*"], onChange, onFound)
# Runs when deleting a file
echo "✨ Deleted\n", file.getName()
engine.clearTemplateByPath(file.getPath())

var w = newWatchout(@[engine.getSourcePath() / "*"], onChange, onFound, onDelete)
w.start(waitThread)
else:
for tpl in engine.getViews():
Expand Down
68 changes: 55 additions & 13 deletions src/tim/engine/meta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/tim

import std/[macros, os, json, strutils, base64, tables]
import std/[macros, os, json, strutils, sequtils, base64, tables]
import pkg/[checksums/md5, supersnappy, flatty]

export getProjectPath
Expand All @@ -16,15 +16,15 @@ when defined timStandalone:

type
TimTemplateType* = enum
ttInvalid
ttLayout = "layouts"
ttView = "views"
ttPartial = "partials"

TemplateSourcePaths = tuple[src, ast, html: string]
TimTemplate* = ref object
# ast*: Ast
templateId: string
jit: bool
templateId: string
templateName: string
case templateType: TimTemplateType
of ttPartial:
Expand All @@ -33,6 +33,7 @@ type
viewIndent: uint
else: discard
sources*: TemplateSourcePaths
dependents: Table[string, string]

TemplateTable = TableRef[string, TimTemplate]

Expand Down Expand Up @@ -96,20 +97,29 @@ proc getAstStoragePath*(engine: Tim): string =
#
# TimTemplate API
#
proc newTemplate(id: string, templateType: TimTemplateType,
proc newTemplate(id: string, tplType: TimTemplateType,
sources: TemplateSourcePaths): TimTemplate =
TimTemplate(templateId: id, templateType: templateType, sources: sources)
TimTemplate(
templateId: id,
templateType: tplType,
templateName: sources.src.extractFilename,
sources: sources
)

proc getType*(t: TimTemplate): TimTemplateType =
## Get template type of `t`
t.templateType

proc getHash*(t: TimTemplate): string =
## Returns the hashed path of `t`
hashid(t.sources.src)

proc getName*(t: TimTemplate): string =
## Get template name of `t`
t.templateName

proc getTemplateId*(t: TimTemplate): string =
## Get template id of `t`
t.templateId

proc setViewIndent*(t: TimTemplate, i: uint) =
Expand All @@ -120,6 +130,16 @@ proc getViewIndent*(t: TimTemplate): uint =
assert t.templateType == ttLayout
t.viewIndent

proc hasDep*(t: TimTemplate, path: string): bool =
t.dependents.hasKey(path)

proc addDep*(t: TimTemplate, path: string) =
## Add a new dependent
t.dependents[path] = path

proc getDeps*(t: TimTemplate): seq[string] =
t.dependents.keys.toSeq()

proc writeHtml*(engine: Tim, tpl: TimTemplate, htmlCode: string) =
## Writes `htmlCode` on disk using `tpl` info
writeFile(tpl.sources.html, htmlCode)
Expand Down Expand Up @@ -194,26 +214,29 @@ proc getTemplateByPath*(engine: Tim, path: string): TimTemplate =
htmlPath = engine.output / "html" / id & ".html"
sources = (src: path, ast: astPath, html: htmlPath)
if engine.src / $ttLayout in path:
result = newTemplate(id, ttLayout, sources)
elif engine.src / $ttView in path:
result = newTemplate(id, ttView, sources)
elif engine.src / $ttPartial in path:
result = newTemplate(id, ttPartial, sources)
engine.layouts[path] = newTemplate(id, ttLayout, sources)
return engine.layouts[path]
if engine.src / $ttView in path:
engine.views[path] = newTemplate(id, ttView, sources)
return engine.views[path]
if engine.src / $ttPartial in path:
engine.partials[path] = newTemplate(id, ttPartial, sources)
return engine.partials[path]

proc hasLayout*(engine: Tim, key: string): bool =
## Determine if `key` exists in `layouts` table
result = engine.layouts.hasKey(engine.getPath(key, ttLayout))

proc getLayout*(engine: Tim, key: string): TimTemplate =
## Returns a `TimTemplate` layout with `layoutName`
## Get a `TimTemplate` from `layouts` by `key`
result = engine.layouts[engine.getPath(key, ttLayout)]

proc hasView*(engine: Tim, key: string): bool =
## Determine if `key` exists in `views` table
result = engine.views.hasKey(engine.getPath(key, ttView))

proc getView*(engine: Tim, key: string): TimTemplate =
## Returns a `TimTemplate` view with `key`
## Get a `TimTemplate` from `views` by `key`
result = engine.views[engine.getPath(key, ttView)]

proc newTim*(src, output, basepath: string,
Expand Down Expand Up @@ -255,6 +278,7 @@ proc newTim*(src, output, basepath: string,
result.views[fpath] = id.newTemplate(ttView, sources)
of ttPartial:
result.partials[fpath] = id.newTemplate(ttPartial, sources)
else: discard

discard existsOrCreateDir(result.output / "ast")
discard existsOrCreateDir(result.output / "html")
Expand All @@ -276,4 +300,22 @@ proc flush*(engine: Tim) =
f.path.removeFile()

proc getSourcePath*(engine: Tim): string =
result = engine.src
result = engine.src

proc getTemplateType*(engine: Tim, path: string): TimTemplateType =
## Returns `TimTemplateType` by `path`
let basepath = engine.getSourcePath()
for xdir in ["layouts", "views", "partials"]:
if path.startsWith(basepath / xdir):
return parseEnum[TimTemplateType](xdir)

proc clearTemplateByPath*(engine: Tim, path: string) =
## Clear a template from `TemplateTable` by `path`
case engine.getTemplateType(path):
of ttLayout:
engine.layouts.del(path)
of ttView:
engine.views.del(path)
of ttPartial:
engine.partials.del(path)
else: discard

0 comments on commit 3a2ecc9

Please sign in to comment.