snallygaster: update license
[NixPkgs.git] / doc / doc-support / lib-function-locations.nix
blobfeab0bce0b245bb4e6c37094efbe8cb5d434d9fd
1 { nixpkgsPath, revision, libsetsJSON }:
2 let
3   lib = import (nixpkgsPath + "/lib");
4   libsets = builtins.fromJSON libsetsJSON;
6   libDefPos = prefix: set:
7     builtins.concatMap
8       (name: [{
9         name = builtins.concatStringsSep "." (prefix ++ [name]);
10         location = builtins.unsafeGetAttrPos name set;
11       }] ++ lib.optionals
12         (builtins.length prefix == 0 && builtins.isAttrs set.${name})
13         (libDefPos (prefix ++ [name]) set.${name})
14       ) (builtins.attrNames set);
16   libset = toplib:
17     builtins.map
18       (subsetname: {
19         subsetname = subsetname;
20         functions = libDefPos [] toplib.${subsetname};
21       })
22       (builtins.map (x: x.name) libsets);
24   flattenedLibSubset = { subsetname, functions }:
25   builtins.map
26     (fn: {
27       name = "lib.${subsetname}.${fn.name}";
28       value = fn.location;
29     })
30     functions;
32   locatedlibsets = libs: builtins.map flattenedLibSubset (libset libs);
33   removeFilenamePrefix = prefix: filename:
34     let
35     prefixLen = (builtins.stringLength prefix) + 1; # +1 to remove the leading /
36       filenameLen = builtins.stringLength filename;
37       substr = builtins.substring prefixLen filenameLen filename;
38       in substr;
40   removeNixpkgs = removeFilenamePrefix (builtins.toString nixpkgsPath);
42   liblocations =
43     builtins.filter
44       (elem: elem.value != null)
45       (lib.lists.flatten
46         (locatedlibsets lib));
48   fnLocationRelative = { name, value }:
49     {
50       inherit name;
51       value = value // { file = removeNixpkgs value.file; };
52     };
54   relativeLocs = (builtins.map fnLocationRelative liblocations);
55   sanitizeId = builtins.replaceStrings
56     [ "'"      ]
57     [ "-prime" ];
59   urlPrefix = "https://github.com/NixOS/nixpkgs/blob/${revision}";
60   jsonLocs = builtins.listToAttrs
61     (builtins.map
62       ({ name, value }: {
63         name = sanitizeId name;
64         value =
65           let
66             text = "${value.file}:${builtins.toString value.line}";
67             target = "${urlPrefix}/${value.file}#L${builtins.toString value.line}";
68           in
69             "[${text}](${target}) in `<nixpkgs>`";
70       })
71     relativeLocs);
74 jsonLocs