From 56e71f62dc49e64fcd37768cc7d1ec219d4990e6 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sat, 4 Mar 2017 13:15:23 -0500 Subject: Add locateDominatingFile lib function --- lib/filesystem.nix | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/filesystem.nix (limited to 'lib/filesystem.nix') diff --git a/lib/filesystem.nix b/lib/filesystem.nix new file mode 100644 index 0000000000000..91b04d81c13be --- /dev/null +++ b/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); +} -- cgit 1.4.1