2021-02-16 20:32:15 +01:00
|
|
|
{ lib ? import <nixpkgs/lib> }:
|
|
|
|
let
|
2021-02-17 14:03:26 +01:00
|
|
|
inherit (builtins) baseNameOf dirOf length genList pathExists;
|
2021-02-16 20:32:15 +01:00
|
|
|
inherit (lib) take splitString concatStringsSep last foldl foldr head
|
|
|
|
tail singleton removePrefix hasSuffix removeSuffix flatten crossLists reverseList;
|
|
|
|
butlast = list: take (length list - 1) list;
|
|
|
|
|
|
|
|
doFileSuffix = "do";
|
|
|
|
|
|
|
|
genPatterns = path: let
|
2021-02-17 14:03:26 +01:00
|
|
|
fn = baseNameOf path;
|
2021-02-16 20:32:15 +01:00
|
|
|
in (map (x: "default." + x)
|
|
|
|
(foldr (a: b: let x = (head b); in [ (a + "." + x) ] ++ b)
|
|
|
|
[ doFileSuffix ]
|
|
|
|
(tail (splitString "." fn))));
|
|
|
|
|
|
|
|
searchPath = path:
|
|
|
|
let
|
|
|
|
pathlist = (splitString "/" path);
|
|
|
|
pat = (last pathlist);
|
|
|
|
doFile = path + "." + doFileSuffix;
|
|
|
|
patlist = genPatterns pat;
|
|
|
|
foldedpaths = foldr (a: b:
|
|
|
|
let
|
|
|
|
h = if length b == 0 then "" else head b;
|
|
|
|
in [ (h + a + "/") ] ++ b) [] (tail (reverseList pathlist));
|
|
|
|
pths = crossLists (p: pat: p + pat) [ foldedpaths patlist ];
|
|
|
|
in [ doFile ] ++ pths;
|
|
|
|
|
|
|
|
cwd = "/home/spacefrogg/tmp";
|
|
|
|
path = cwd + "/foo/bar/baz.def.c";
|
|
|
|
|
|
|
|
whichdo = path: let
|
|
|
|
pathlist = searchPath path;
|
|
|
|
in foldl (res: p: if res == "" then if pathExists p then p else res else res) "" pathlist;
|
|
|
|
|
|
|
|
d1 = path: pat: let
|
2021-02-17 14:03:26 +01:00
|
|
|
dir = (dirOf pat) + "/";
|
2021-02-16 20:32:15 +01:00
|
|
|
in removePrefix dir path;
|
|
|
|
|
|
|
|
d2 = path: pat: let
|
2021-02-17 14:03:26 +01:00
|
|
|
dir = (dirOf pat) + "/";
|
2021-02-16 20:32:15 +01:00
|
|
|
_path = removePrefix dir path;
|
|
|
|
_pat = removeSuffix ".${doFileSuffix}" (removePrefix "${dir}default" pat);
|
|
|
|
_out = removeSuffix _pat _path;
|
|
|
|
in if _out == "" then _path else _out;
|
|
|
|
|
|
|
|
self = {
|
2021-02-17 14:03:26 +01:00
|
|
|
inherit searchPath cwd path doFileSuffix whichdo d1 d2;
|
2021-02-16 20:32:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
in self
|