Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ Or, in a simpler but less readable way:

`mapWith` can be combined with any number of `filtered` and `matching` calls, in any order, but the (composed) transformation is applied _after_ the filters, and only to the paths that match all of them.

### `import-tree.addPath`

`addPath` can be used to prepend paths to be filtered as a setup for import-tree.
This function can be applied multiple times.

```nix
# import-tree.addPath : (path_or_list_of_paths) -> import-tree

# Both of these result in the same imported files.
# however, the first adds ./vendor as a *pre-configured* path.
# and the final user can supply ./modules or [] empty.
(import-tree.addPath ./vendor) ./modules
import-tree [./vendor ./modules]
```

### `import-tree.withLib`

> \[!NOTE\]
Expand Down Expand Up @@ -274,3 +289,9 @@ The test suite can be found in [`checkmate.nix`](checkmate.nix). To run it local
```sh
nix flake check path:checkmate --override-input target path:.
```

Run the following to format files:

```sh
nix run github:vic/checkmate#fmt
```
14 changes: 14 additions & 0 deletions checkmate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ in
expected = [ 1 ];
};

addPath."test `addPath` prepends a path to filter" = {
expr = (lit.addPath ./tree/x).leafs [ ];
expected = [ ./tree/x/y.nix ];
};

addPath."test `addPath` can be called multiple times" = {
expr = ((lit.addPath ./tree/x).addPath ./tree/a/b).leafs [ ];
expected = [
./tree/x/y.nix
./tree/a/b/b_a.nix
./tree/a/b/m.nix
];
};

pipeTo."test pipes list into a function" = {
expr = (lit.mapWith lib.pathType).pipeTo (lib.length) ./tree/x;
expected = 1;
Expand Down
19 changes: 12 additions & 7 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let
filterf ? null,
mapf ? null,
pipef ? null,
paths ? [ ],
...
}:
path:
Expand All @@ -29,13 +30,15 @@ let
let
initialFilter = p: lib.hasSuffix ".nix" p && !lib.hasInfix "/_" p;
in
lib.pipe root [
(lib.lists.flatten)
(map lib.filesystem.listFilesRecursive)
(lib.lists.flatten)
(builtins.filter (compose (and filterf initialFilter) toString))
(map mapf)
];
lib.pipe
[ paths root ]
[
(lib.lists.flatten)
(map lib.filesystem.listFilesRecursive)
(lib.lists.flatten)
(builtins.filter (compose (and filterf initialFilter) toString))
(map mapf)
];

in
result;
Expand Down Expand Up @@ -63,6 +66,7 @@ let
# Accumulated configuration
mapf = (i: i);
filterf = _: true;
paths = [ ];

__functor = self: f: {
config = (f self);
Expand All @@ -72,6 +76,7 @@ let
filtered = filterf: self (c: mapAttr (f c) "filterf" (and filterf));
matching = regex: self (c: mapAttr (f c) "filterf" (and (matchesRegex regex)));
mapWith = mapf: self (c: mapAttr (f c) "mapf" (compose mapf));
addPath = path: self (c: mapAttr (f c) "paths" (p: p ++ [ path ]));

# Configuration updates (non-accumulating)
withLib = lib: self (c: (f c) // { inherit lib; });
Expand Down