schemac: tool for processing schemas
This commit is contained in:
parent
ded072c76c
commit
6d5acf324b
|
@ -1,13 +1,12 @@
|
|||
# Package
|
||||
|
||||
version = "20221120"
|
||||
version = "20221204"
|
||||
author = "Emery Hemingway"
|
||||
description = "data model and serialization format"
|
||||
license = "Unlicense"
|
||||
srcDir = "src"
|
||||
|
||||
# bin = @["preserves/preserves_schema_nim", "preserves/private/preserves_encode"]
|
||||
# Nimble can't build these, because it sucks
|
||||
bin = @["preserves/preserves_schema_nim", "preserves/private/preserves_encode", "preserves/schemac"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# SPDX-FileCopyrightText: 2022 ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[hashes, options, os, parseopt, streams, strutils, sets, 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[void]
|
||||
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)
|
Loading…
Reference in New Issue