From 87d1bce76322a14223be8f32a1a6b3a26bfb9385 Mon Sep 17 00:00:00 2001 From: Carl Gay Date: Tue, 31 Dec 2024 16:03:15 -0500 Subject: [PATCH] cleanup: move some general-purpose functions to deft-shared module --- sources/library.dylan | 8 +++----- sources/shared.dylan | 23 +++++++++++++++++++++++ sources/workspaces/registry.dylan | 13 ------------- sources/workspaces/workspaces.dylan | 10 ---------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/sources/library.dylan b/sources/library.dylan index a3a7b10..8708df7 100644 --- a/sources/library.dylan +++ b/sources/library.dylan @@ -49,11 +49,9 @@ define module deft-shared export *debug?*, *verbose?*, - debug, - note, - verbose, - trace, - warn; + debug, note, verbose, trace, warn, + file-content, + load-json-file; end module; define module pacman diff --git a/sources/shared.dylan b/sources/shared.dylan index b8cf102..4cf7664 100644 --- a/sources/shared.dylan +++ b/sources/shared.dylan @@ -40,3 +40,26 @@ end; define inline function warn (fmt, #rest args) => () apply(note, concat("WARNING: ", fmt), args); end; + +define function load-json-file (file :: ) => (config :: ) + fs/with-open-file(stream = file, if-does-not-exist: #f) + let object = parse-json(stream, strict?: #f, table-class: ); + if (~instance?(object,
)) + error("Invalid JSON file %s, must contain at least {}", file); + end; + object + end +end function; + +// Read the full contents of a file and return it as a string. If the file +// doesn't exist return #f. (I thought if-does-not-exist: #f was supposed to +// accomplish this without the need for block/exception.) +define function file-content (path :: ) => (text :: false-or()) + block () + fs/with-open-file(stream = path, if-does-not-exist: #"signal") + read-to-end(stream) + end + exception (fs/) + #f + end +end function; diff --git a/sources/workspaces/registry.dylan b/sources/workspaces/registry.dylan index 9e5eabe..bc58aea 100644 --- a/sources/workspaces/registry.dylan +++ b/sources/workspaces/registry.dylan @@ -39,19 +39,6 @@ define function write-registry-file end end function; -// Read the full contents of a file and return it as a string. If the file -// doesn't exist return #f. (I thought if-does-not-exist: #f was supposed to -// accomplish this without the need for block/exception.) -define function file-content (path :: ) => (text :: false-or()) - block () - fs/with-open-file(stream = path, if-does-not-exist: #"signal") - read-to-end(stream) - end - exception (fs/) - #f - end -end function; - // Create/update a single registry directory having an entry for each library // in each active package and all transitive dependencies. This traverses // package directories to find .lid files. Note that it assumes that .lid files diff --git a/sources/workspaces/workspaces.dylan b/sources/workspaces/workspaces.dylan index 2d57049..c98c34c 100644 --- a/sources/workspaces/workspaces.dylan +++ b/sources/workspaces/workspaces.dylan @@ -193,16 +193,6 @@ define function load-workspace-config := element(json, $default-library-key, default: #f) | find-default-library(); end function; -define function load-json-file (file :: ) => (config ::
) - fs/with-open-file(stream = file, if-does-not-exist: #f) - let object = parse-json(stream, strict?: #f, table-class: ); - if (~instance?(object,
)) - workspace-error("Invalid JSON file %s, must contain at least {}", file); - end; - object - end -end function; - // Find the workspace directory. The nearest directory containing // workspace.json always takes precedence. Otherwise the nearest directory // containing dylan-package.json.