Skip to content

Commit

Permalink
Add locateDominatingFile lib function
Browse files Browse the repository at this point in the history
(cherry picked from commit 56e71f6)
  • Loading branch information
shlevy committed Mar 4, 2017
1 parent 5de841b commit 8e72857
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/default.nix
Expand Up @@ -34,14 +34,17 @@ let
sandbox = import ./sandbox.nix;
fetchers = import ./fetchers.nix;

# Eval-time filesystem handling
filesystem = import ./filesystem.nix;

in
{ inherit trivial
attrsets lists strings stringsWithDeps
customisation maintainers meta sources
modules options types
licenses platforms systems
debug generators misc
sandbox fetchers;
sandbox fetchers filesystem;
}
# !!! don't include everything at top-level; perhaps only the most
# commonly used functions.
Expand Down
26 changes: 26 additions & 0 deletions lib/filesystem.nix
@@ -0,0 +1,26 @@
{ # locateDominatingFile : RegExp
# -> Path
# -> Nullable { path : Path;
# matches : [ MatchResults ];
# }
# Find the first directory containing a file matching 'pattern'
# upward from a given 'file'.
# Returns 'null' if no directories contain a file matching 'pattern'.
locateDominatingFile = pattern: file:
let go = path:
let files = builtins.attrNames (builtins.readDir path);
matches = builtins.filter (match: match != null)
(map (builtins.match pattern) files);
in
if builtins.length matches != 0
then { inherit path matches; }
else if path == /.
then null
else go (dirOf path);
parent = dirOf file;
isDir =
let base = baseNameOf file;
type = (builtins.readDir parent).${base} or null;
in file == /. || type == "directory";
in go (if isDir then file else parent);
}

0 comments on commit 8e72857

Please sign in to comment.