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
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ For example:

```nix
lib.pipe import-tree [
(i: i.mapWith lib.traceVal) # trace all paths. useful for debugging what is being imported.
(i: i.map lib.traceVal) # trace all paths. useful for debugging what is being imported.
(i: i.filter (lib.hasInfix ".mod.")) # filter nix files by some predicate
(i: i ./modules) # finally, call the configured import-tree with a path
]
Expand All @@ -143,7 +143,7 @@ lib.pipe import-tree [
Here is a simpler but less readable equivalent:

```nix
((import-tree.mapWith lib.traceVal).filter (lib.hasInfix ".mod.")) ./modules
((import-tree.map lib.traceVal).filter (lib.hasInfix ".mod.")) ./modules
```

### `import-tree.filter` and `import-tree.filterNot`
Expand All @@ -159,7 +159,7 @@ Here is a simpler but less readable equivalent:
import-tree.filter (lib.hasInfix ".mod.") ./some-dir
```

`filter` can be applied multiple times, in which case only the files match _all_ filters will be selected:
`filter` can be applied multiple times, in which case only the files matching _all_ filters will be selected:

```nix
lib.pipe import-tree [
Expand All @@ -177,38 +177,38 @@ Or, in a simpler but less readable way:

### `import-tree.match` and `import-tree.matchNot`

`match` takes a regular expression. The regex should match the full path for the path to be selected. match is done with `builtins.match`.
`match` takes a regular expression. The regex should match the full path for the path to be selected. Matching is done with `builtins.match`.

```nix
# import-tree.match : regex -> import-tree

import-tree.match ".*/[a-z]+@(foo|bar)\.nix" ./some-dir
```

`match` can be applied multiple times, in which case only the paths match _all_ regex patterns will be selected, and can be combined with any number of `filter`, in any order.
`match` can be applied multiple times, in which case only the paths matching _all_ regex patterns will be selected, and can be combined with any number of `filter`, in any order.

### `import-tree.mapWith`
### `import-tree.map`

`mapWith` can be used to transform each path by providing a function.
`map` can be used to transform each path by providing a function.

e.g. to convert the path into a module explicitly:

```nix
# import-tree.mapWith : (path -> any) -> import-tree
# import-tree.map : (path -> any) -> import-tree

import-tree.mapWith (path: {
import-tree.map (path: {
imports = [ path ];
# assuming such an option is declared
automaticallyImportedPaths = [ path ];
})
```

`mapWith` can be applied multiple times, composing the transformations:
`map` can be applied multiple times, composing the transformations:

```nix
lib.pipe import-tree [
(i: i.mapWith (lib.removeSuffix ".nix"))
(i: i.mapWith builtins.stringLength)
(i: i.map (lib.removeSuffix ".nix"))
(i: i.map builtins.stringLength)
] ./some-dir
```

Expand All @@ -217,10 +217,10 @@ The above example first removes the `.nix` suffix from all selected paths, then
Or, in a simpler but less readable way:

```nix
((import-tree.mapWith (lib.removeSuffix ".nix")).mapWith builtins.stringLength) ./some-dir
((import-tree.map (lib.removeSuffix ".nix")).map builtins.stringLength) ./some-dir
```

`mapWith` can be combined with any number of `filter` and `match` calls, in any order, but the (composed) transformation is applied _after_ the filters, and only to the paths that match all of them.
`map` can be combined with any number of `filter` and `match` 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`

Expand Down
18 changes: 9 additions & 9 deletions checkmate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ in
expected = [ ];
};

match."test returns files match regex" = {
match."test returns files matching regex" = {
expr = (lit.match ".*/[^/]+_[^/]+\.nix").leafs ./tree;
expected = [
./tree/a/a_b.nix
./tree/a/b/b_a.nix
];
};

matchNot."test returns files not match regex" = {
matchNot."test returns files not matching regex" = {
expr = (lit.matchNot ".*/[^/]+_[^/]+\.nix").leafs ./tree/a/b;
expected = [
./tree/a/b/m.nix
Expand All @@ -76,18 +76,18 @@ in
expected = [ ./tree/a/a_b.nix ];
};

mapWith."test transforms each match file with function" = {
expr = (lit.mapWith import).leafs ./tree/x;
map."test transforms each matching file with function" = {
expr = (lit.map import).leafs ./tree/x;
expected = [ "z" ];
};

mapWith."test `mapWith` composes with `filter`" = {
expr = ((lit.filter (lib.hasInfix "/x")).mapWith import).leafs ./tree;
map."test `map` composes with `filter`" = {
expr = ((lit.filter (lib.hasInfix "/x")).map import).leafs ./tree;
expected = [ "z" ];
};

mapWith."test multiple `mapWith`s compose" = {
expr = ((lit.mapWith import).mapWith builtins.stringLength).leafs ./tree/x;
map."test multiple `map`s compose" = {
expr = ((lit.map import).map builtins.stringLength).leafs ./tree/x;
expected = [ 1 ];
};

Expand Down Expand Up @@ -134,7 +134,7 @@ in
};

pipeTo."test pipes list into a function" = {
expr = (lit.mapWith lib.pathType).pipeTo (lib.length) ./tree/x;
expr = (lit.map lib.pathType).pipeTo (lib.length) ./tree/x;
expected = 1;
};

Expand Down
6 changes: 3 additions & 3 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let
leafs =
lib: root:
let
initialFilter = p: lib.hasSuffix ".nix" p && !lib.hasInfix "/_" p;
initialFilter = andNot (lib.hasInfix "/_") (lib.hasSuffix ".nix");
in
lib.pipe
[ paths root ]
Expand All @@ -47,7 +47,7 @@ let
g: f: x:
g (f x);

# Applies the second function first, to allow partial application when building the configuration.
# Applies the second filter first, to allow partial application when building the configuration.
and =
g: f: x:
f x && g x;
Expand Down Expand Up @@ -92,7 +92,7 @@ let
filterNot = filterf: self (c: mapAttr (f c) "filterf" (andNot filterf));
match = regex: self (c: mapAttr (f c) "filterf" (and (matchesRegex regex)));
matchNot = regex: self (c: mapAttr (f c) "filterf" (andNot (matchesRegex regex)));
mapWith = mapf: self (c: mapAttr (f c) "mapf" (compose mapf));
map = mapf: self (c: mapAttr (f c) "mapf" (compose mapf));
addPath = path: self (c: mapAttr (f c) "paths" (p: p ++ [ path ]));
addAPI = api: self (c: mapAttr (f c) "api" (a: a // builtins.mapAttrs (_: g: g (self f)) api));

Expand Down