preserves-nim/src/preserves/preserves_schemac.nim

59 lines
1.7 KiB
Nim

# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[hashes, options, os, parseopt, streams, strutils, tables]
import ../preserves, ./schema, ./schemaparse
when isMainModule:
let outStream = newFileStream(stdout)
var
inputPath = ""
noBundle = false
for kind, key, arg in getopt():
case kind
of cmdEnd: discard
of cmdArgument:
if inputPath != "":
quit "only a single path may specified"
inputPath = key
of cmdLongOption:
if arg != "":
quit("flag does not take an argument: " & key & " " & arg)
case key
of "no-bundle": noBundle = true
else: quit(key & "flag not recognized")
else: quit(key & "flag not recognized")
if inputPath == "":
quit "input file(s) not specified"
if noBundle:
if not fileExists inputPath:
quit(inputPath & " does not exist or is not a file")
var schema = parsePreservesSchema(readFile(inputPath))
write(outStream, schema.toPreserve)
else:
let bundle = Bundle()
if not dirExists inputPath:
quit "not a directory of schemas: " & inputPath
else:
for filePath in walkDirRec(inputPath, relative = true):
var (dirPath, fileName, fileExt) = splitFile(filePath)
if fileExt == ".prs":
var
scm = parsePreservesSchema(readFile(inputPath / filePath))
path: ModulePath
for e in split(dirPath, '/'):
add(path, Symbol e)
add(path, Symbol fileName)
bundle.modules[path] = scm
if bundle.modules.len == 0:
quit "no schemas parsed"
else:
write(outStream, bundle.toPreserve)
close(outStream)