Skip to content

Commit

Permalink
cleanup: move some general-purpose functions to deft-shared module
Browse files Browse the repository at this point in the history
  • Loading branch information
cgay committed Jan 1, 2025
1 parent 19170be commit 87d1bce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
8 changes: 3 additions & 5 deletions sources/library.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions sources/shared.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -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 :: <file-locator>) => (config :: <table>)
fs/with-open-file(stream = file, if-does-not-exist: #f)
let object = parse-json(stream, strict?: #f, table-class: <istring-table>);
if (~instance?(object, <table>))
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 :: <locator>) => (text :: false-or(<string>))
block ()
fs/with-open-file(stream = path, if-does-not-exist: #"signal")
read-to-end(stream)
end
exception (fs/<file-does-not-exist-error>)
#f
end
end function;
13 changes: 0 additions & 13 deletions sources/workspaces/registry.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -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 :: <locator>) => (text :: false-or(<string>))
block ()
fs/with-open-file(stream = path, if-does-not-exist: #"signal")
read-to-end(stream)
end
exception (fs/<file-does-not-exist-error>)
#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
Expand Down
10 changes: 0 additions & 10 deletions sources/workspaces/workspaces.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -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 :: <file-locator>) => (config :: <table>)
fs/with-open-file(stream = file, if-does-not-exist: #f)
let object = parse-json(stream, strict?: #f, table-class: <istring-table>);
if (~instance?(object, <table>))
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.
Expand Down

0 comments on commit 87d1bce

Please sign in to comment.