-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified and added some custom functions.
- Loading branch information
1 parent
4672257
commit b5f4267
Showing
6 changed files
with
145 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ lib }: let | ||
|
||
inherit (builtins) filter isAttrs; | ||
inherit (lib) filterAttrs mapAttrs' mapAttrsToList concatLists; | ||
|
||
in { | ||
|
||
# Maps the given function to the given attribute set. Then filters the result based on the given predicate. | ||
mapAndFilterAttrs = pred: fn: attrs: filterAttrs pred (mapAttrs' fn attrs); | ||
|
||
# Returns all the keys of the given attribute set. | ||
attrKeys = attrs: mapAttrsToList (key: _: key) attrs; | ||
|
||
# Returns all the keys of the given attribute set, recursively. | ||
attrKeysRecursive = attrs: let | ||
|
||
getKeys = prefix: attrs: concatLists (mapAttrsToList (key: value: let | ||
|
||
fullKey = if prefix == "" then key else "${prefix}.${key}"; | ||
|
||
# If the current value is another attrset, recurse into it. | ||
recursedKeys = if isAttrs value | ||
then [fullKey] ++ getKeys fullKey value | ||
else [fullKey]; | ||
|
||
in recursedKeys) attrs); | ||
|
||
in getKeys "" attrs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,75 @@ | ||
{ lib }: let | ||
|
||
inherit (builtins) readDir; | ||
inherit (lib) filterAttrs mapAttrsToList; | ||
inherit (builtins) readDir filter pathExists baseNameOf; | ||
inherit (lib) mapAttrsToList concatLists filterAttrs hasPrefix hasSuffix; | ||
|
||
in { | ||
# Read the content of a directory recursively and return a list of files and directories. | ||
readDirRec = dirPath: filterFn: maxDepth: let | ||
|
||
# Retrieves all the folder names from a given directory. | ||
getSubfolders = dirPath: let | ||
readRecursive = fullPath: prefix: currentDepth: let | ||
|
||
# Read the directory and filter out anything that is not a directory. | ||
dirContent = readDir dirPath; | ||
subfolders = filterAttrs (_: value: value == "directory") dirContent; | ||
# Read the content of the current directory | ||
dirContent = if currentDepth > 0 || maxDepth == -1 then (readDir fullPath) else { }; | ||
filteredDirs = filterAttrs filterFn dirContent; | ||
|
||
# Keep the key only, as it contains the folder name. | ||
subfolderList = mapAttrsToList (key: _: key) subfolders; | ||
recurse = (name: type: let | ||
|
||
in subfolderList; | ||
# Full path to recursively read other dirs. | ||
nextFullPath = "${fullPath}/${name}"; | ||
|
||
# Reads and parses a JSON file at the given path. | ||
readJSON = path: let | ||
# Relative path to store in the output. | ||
relativePath = if prefix == "" then name else "${prefix}/${name}"; | ||
current = [{ inherit type; path = relativePath; }]; | ||
|
||
# Read the file and parse it as JSON. | ||
fileContent = builtins.readFile path; | ||
parsedJSON = builtins.fromJSON fileContent; | ||
# If the current directory is a directory, then recursively read it. | ||
output = if type == "directory" | ||
then current ++ readRecursive nextFullPath relativePath (currentDepth - 1) | ||
else current; | ||
|
||
in parsedJSON; | ||
in output); | ||
|
||
in concatLists (mapAttrsToList recurse filteredDirs); | ||
|
||
in readRecursive dirPath "" maxDepth; | ||
|
||
in rec { | ||
|
||
# Reads the contents of the given directory recursively. Allows to speciify a max depth and whether to include hidden files and directories. | ||
readDirRecursive' = dirPath: { maxDepth ? -1, includeHidden ? false }: let | ||
|
||
# Filter function to exclude hidden files and directories. | ||
filterFn = if includeHidden then (_: _: true) else (name: _: (! hasPrefix "." name)); | ||
contents = readDirRec dirPath filterFn maxDepth; | ||
|
||
in contents; | ||
|
||
# Reads the contents of the entire given directory recursively. | ||
readDirRecursive = dirPath: (readDirRecursive' dirPath { }); | ||
|
||
# Retrieves all the paths of non 'default.nix' files in the given folder. | ||
getModulesRecursive = dirPath: let | ||
|
||
# Read the content of the directory recursively. | ||
contents = readDirRecursive dirPath; | ||
|
||
# Filter out files that are not modules. | ||
modules = filter ({ type, path }: let | ||
|
||
# It's a file, it's not a default.nix and it has a .nix extension. | ||
isModule = type == "regular" && (baseNameOf path) != "default.nix" && hasSuffix ".nix" path; | ||
|
||
in isModule) contents; | ||
|
||
in map ({ type, path }: path) modules; | ||
|
||
# Maps the given function to all the modules in the given directory. | ||
mapModulesRecursive = dirPath: fn: let | ||
|
||
# Get all the modules in the given directory. | ||
modules = getModulesRecursive dirPath; | ||
|
||
# Map the given function to each module. | ||
mapped = map (path: fn path) modules; | ||
|
||
in mapped; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ lib }: let | ||
|
||
inherit (builtins) isPath readFile fromJSON; | ||
|
||
in { | ||
|
||
# Same as 'fromJSON' but allows paths as an input. | ||
fromJSON' = source: let | ||
|
||
# Check if it's a file, if so, read it. | ||
isFilepath = isPath path; | ||
jsonContent = if isFilepath then (readFile source) else (source); | ||
|
||
# Parse the json contents. | ||
parsedJSON = fromJSON jsonContent; | ||
|
||
in parsedJSON; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters