preserves-nim/src/preserves/schemac.nim

48 lines
1.5 KiB
Nim

# SPDX-FileCopyrightText: 2022 ☭ 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)
for kind, key, inputPath in getopt():
case kind
of cmdEnd: discard
of cmdArgument:
quit "arguments must be prefixed by --schema: or --bundle:"
of cmdLongOption:
if inputPath == "":
quit "long command line options require a path argument"
case key
of "schema":
var schema = parsePreservesSchema(readFile(inputPath))
write(outStream, schema.toPreserve)
of "bundle":
var 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)
else: quit("unhandled option " & key)
else: quit("unhandled option " & key)
close(outStream)