~kungtotte/dtt

b4f163966436acbd825cd4738c4d4f588eb0f2e3 — Thomas Landin 2 months ago ed49e6d
Fix the recursive config.cfg bug

Now you can be in any directory inside the dtt directory and run `dtt
build` and it will work as expected as long as you have a config.cfg
file somewhere.
2 files changed, 11 insertions(+), 16 deletions(-)

M src/dtt.nim
M src/utils.nim
M src/dtt.nim => src/dtt.nim +8 -13
@@ 91,26 91,21 @@ proc cleanCmd(all: bool = false, force: bool = false) =
    quit(ord(DttError.DirectoryNotFound))

proc buildCmd() =
  # TODO: There's a bug where if you run build outside the main dir it won't
  # find things properly, so we need to start with finding the config.cfg file
  # then setting all the other directories relative to that.
  let (config, working_dir) = try:
    findConfigFile(os.getCurrentDir() / "config.cfg")
  except IOError:
    stderr.writeLine(fmt"{bin}: Could not find a config.cfg file. Is this a dtt directory?")
    quit(ord(DttError.NotADttDirectory))
  let
    wd = os.getCurrentDir()
    condir = wd / content_dir
    tmpldir = wd / template_dir
    outdir = wd / output_dir
    condir = working_dir / content_dir
    tmpldir = working_dir / template_dir
    outdir = working_dir / output_dir
  var
    mcontext = newContext(searchDirs = @[tmpldir])

  if not existsDir(outdir):
    createDir(outdir)

  let config = try:
    findConfigFile(wd / "config.cfg")
  except IOError:
    stderr.writeLine(fmt"{bin}: Could not find a config.cfg file. Is this a dtt directory?")
    quit(ord(DttError.NotADttDirectory))

  loadMetaData(config, mcontext)
  let blogs = findBlogPosts(condir, tmpldir)
  mcontext["links"] = findLinks(condir, excludes=blogs)

M src/utils.nim => src/utils.nim +3 -3
@@ 109,14 109,14 @@ proc findBlogPosts*(directory: string, tmpldir: string): seq[Table[string, strin
        blogContext["date"] = date
        blogContext["blog_link"] = target
        let
          rendered = blogContext.renderTemplate(f, tmpl)
          rendered = blogContext.renderTemplate(f, tmpl, tmpldir)
        let tbl = {"id": name, "post": rendered, "date": date}.toTable
        result.add(tbl)
  sort(result, dateSort, Descending)

proc findConfigFile*(full_path: string): Config =
proc findConfigFile*(full_path: string): tuple[cfg: Config, wd: string] =
  let (path, name) = splitPath(full_path)
  for p in parentDirs(path):
      if existsFile(p / name):
        return loadConfig(p / name)
        return (loadConfig(p / name), p)
  raise new IOError