on 19052d8 help sign in
haskell.nix/lib/cabal-project-parser.nix
104 lines 3.7 kB history raw
1{ pkgs }:
2let
3 span = pred: list:
4 let n = pkgs.lib.lists.foldr (x: acc: if pred x then acc + 1 else 0) 0 list;
5 in { fst = pkgs.lib.lists.take n list; snd = pkgs.lib.lists.drop n list; };
7 # Look for a index-state: field in the cabal.project file
8 parseIndexState = rawCabalProject:
9 let
10 indexState = pkgs.lib.lists.concatLists (
11 pkgs.lib.lists.filter (l: l != null)
12 (builtins.map (l: builtins.match "^index-state: *(.*)" l)
13 (pkgs.lib.splitString "\n" rawCabalProject)));
14 in
15 pkgs.lib.lists.head (indexState ++ [ null ]);
17 # Remove the indentation of the first line from all the lines
18 unindent = blockLines: (pkgs.lib.foldl' ({matchString, out}: s:
19 let
20 m = builtins.match matchString s;
21 in
22 if m != null
23 then {
24 matchString = "(${builtins.head m})(.*)";
25 out = out ++ [(builtins.elemAt m 1)];
26 }
27 else {
28 inherit matchString out;
29 }
30 ) { matchString = "( *)(.*)"; out = []; } blockLines).out;
32 # Strip comments except `--sha256`
33 stripComments = blockLines: pkgs.lib.filter (s: s != "") (
34 pkgs.lib.foldl' (out: s:
35 let
36 m = builtins.match "(.*)--.*" s;
37 in
38 if !(pkgs.lib.hasPrefix "--sha256" s) && m != null
39 then out ++ [(builtins.head m)]
40 else out ++ [s]
41 ) [] blockLines
42 );
44 # Parse lines of a source-repository-package block
45 parseBlockLines = blockLines: (pkgs.lib.foldl' ({name, attrs}: s:
46 let
47 # Look for a new attribute name
48 pair = builtins.match "([^ :]*): *(.*)" s;
50 # Function to build the next parse state when the attribute name is known
51 nextState = name: value: {
52 inherit name;
53 # Support multi line attributes by appending the value to the named attribute
54 attrs = attrs // {
55 ${name} =
56 if attrs ? ${name}
57 then attrs.${name} + " " + value
58 else value;
59 };
60 };
61 in
62 if pair != null
63 then
64 # First line of a new attribute
65 nextState (builtins.head pair) (builtins.elemAt pair 1)
66 else
67 if name != null
68 then nextState name s # Append another line to the current attribute
69 else __trace "Expected attribute but found `${s}`" { inherit name attrs; }
70 ) { name = null; attrs = {}; } (stripComments (unindent blockLines))).attrs;
72 # Gets data for all the repositories to fetch
73 # A comment like
74 # --shar256: 003lm3pm0000hbfmii7xcdd9v20000flxf7gdl2pyxia7p014i8z
75 # will be trated like a field and returned here
76 # (used in call-cabal-project-to-nix.nix to create a fixed-output derivation)
77 extractRepoData = cabalProjectFileName: lookupSha256: repo: {
78 url = repo.location;
79 ref = repo.tag;
80 sha256 = repo."--sha256" or (lookupSha256 repo);
81 subdirs = if repo ? subdir
82 then pkgs.lib.filter (x: x != "") (pkgs.lib.splitString " " repo.subdir)
83 else ["."];
84 };
86 # Parse a source-repository-package and return data of `type: git` repositories
87 parseBlock = cabalProjectFileName: lookupSha256: block:
88 let
89 x = span (pkgs.lib.strings.hasPrefix " ") (pkgs.lib.splitString "\n" block);
90 attrs = parseBlockLines x.fst;
91 in
92 if attrs."type" or "" != "git"
93 then {
94 sourceRepo = [];
95 otherText = "\nsource-repository-package\n" + block;
96 }
97 else {
98 sourceRepo = extractRepoData cabalProjectFileName lookupSha256 attrs;
99 otherText = pkgs.lib.strings.concatStringsSep "\n" x.snd;
100 };
102in {
103 inherit parseIndexState parseBlockLines parseBlock;