Skip to content

Commit fb6d7c0

Browse files
author
jared
committed
Reimplemented how nix finds the lbf modules with new lbf-list-modules.nix
- For some reason, using the `pkgs.lib.*` functions seem to break CI? Anyhow, this implementation should a bit more efficient (only by constants / without actually measuring) since we don't "append the path prefix, then drop the path prefix" i.e., we directly construct the module names...
1 parent ac244a7 commit fb6d7c0

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{ lib }:
2+
# root is the absolute path to the directory with .lbf files.
3+
# Example
4+
# ~~~~~~
5+
# ```nix
6+
# let
7+
# lbfListModules = pkgs.callPackage (import ./lbf-list-modules.nix) {};
8+
# in lbfListModules ./src
9+
# ```
10+
# evaluates to
11+
# ```
12+
# MySchema
13+
# Directory.MySchema
14+
# ```
15+
# where
16+
# ```text
17+
# $ tree src/
18+
# src/
19+
# ├── Directory
20+
# │   └── MySchema.lbf
21+
# └── MySchema.lbf
22+
# ```
23+
24+
root:
25+
# INVARIANT:
26+
# - dir == dotPrefix except dotPrefix doesn't contain the prefix root, and
27+
# dotPrefix has all `/`s replaced with a singular `.`
28+
# - `dotPrefix` always contains the dot at the end if it needs to be there
29+
let
30+
matchLbfFile = name:
31+
let matches = builtins.match ''(.*)\.lbf$'' name;
32+
in if matches == null
33+
then null
34+
else builtins.elemAt matches 0;
35+
36+
go = dir: dotPrefix:
37+
builtins.concatLists
38+
(lib.mapAttrsToList
39+
(name: value:
40+
if value == "directory"
41+
then go "${dir}/${name}" "${dotPrefix}${name}."
42+
43+
else if value == "regular"
44+
then
45+
# Is this an lbf file?
46+
let lbfFileMatch = matchLbfFile name;
47+
in if lbfFileMatch != null
48+
then [ "${dotPrefix}${lbfFileMatch}" ]
49+
else [ ]
50+
else
51+
builtins.trace ''warning: `${name}` has file type `${value}` which is not a regular file nor directory''
52+
[ ]
53+
)
54+
(builtins.readDir dir)
55+
);
56+
in
57+
go root ""

extras/lbf-nix/lbf-typescript.nix

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,14 @@ let
8585

8686
# TODO(jaredponn): this was (essentially) stolen from the Rust side. This
8787
# should be made a common function.
88-
findModules = root: builtins.map
89-
(path: builtins.replaceStrings [ "/" ] [ "." ]
90-
(pkgs.lib.strings.removePrefix "./" (pkgs.lib.strings.removeSuffix ".lbf"
91-
(pkgs.lib.strings.removePrefix root path))))
92-
(builtins.filter (pkgs.lib.hasSuffix ".lbf")
93-
(pkgs.lib.filesystem.listFilesRecursive root));
88+
lbfListModules = pkgs.callPackage ./lbf-list-modules.nix { };
9489

9590
packageSet =
9691
pkgs.writeTextFile {
9792
name = "lb-typescript-packages";
9893
text =
9994
builtins.toJSON
100-
(builtins.mapAttrs (_name: findModules) imports);
95+
(builtins.mapAttrs (_name: lbfListModules) imports);
10196
};
10297

10398
lbfBuilt = with lbfTypescriptOpts;

0 commit comments

Comments
 (0)