|
2 | 2 | lib, |
3 | 3 | }: |
4 | 4 |
|
5 | | -src: |
6 | 5 | let |
7 | 6 | inherit (lib) |
8 | 7 | flatten |
| 8 | + flip |
9 | 9 | groupBy |
10 | 10 | mapAttrs |
11 | 11 | mapAttrsToList |
| 12 | + optionals |
12 | 13 | ; |
| 14 | + defaultFilter = _: _: true; |
13 | 15 |
|
14 | 16 | # A specialized form of lib.listFilesRecursive except it will only look |
15 | 17 | # for Cargo.toml and config.toml files to keep the intermediate results lean |
16 | 18 | listFilesRecursive = |
17 | | - parentIsDotCargo: dir: |
| 19 | + filter: parentIsDotCargo: dir: |
18 | 20 | flatten ( |
19 | | - mapAttrsToList ( |
| 21 | + flip mapAttrsToList (builtins.readDir dir) ( |
20 | 22 | name: type: |
21 | 23 | let |
22 | | - cur = dir + "/${name}"; |
| 24 | + cur = builtins.unsafeDiscardStringContext ((toString dir) + "/${name}"); |
| 25 | + |
23 | 26 | isConfig = parentIsDotCargo && (name == "config.toml" || name == "config"); |
24 | 27 | isCargoToml = name == "Cargo.toml"; |
25 | 28 | in |
26 | | - if type == "directory" then |
27 | | - listFilesRecursive (name == ".cargo") cur |
28 | | - else if isCargoToml then |
29 | | - [ |
30 | | - { |
31 | | - path = cur; |
32 | | - type = "cargoTomls"; |
33 | | - } |
34 | | - ] |
35 | | - else if isConfig then |
36 | | - [ |
37 | | - { |
38 | | - path = cur; |
39 | | - type = "cargoConfigs"; |
40 | | - } |
41 | | - ] |
42 | | - else |
43 | | - [ ] |
44 | | - ) (builtins.readDir dir) |
| 29 | + # NB: manually apply any cleanSourceWith filtering here to avoid surprises |
| 30 | + # in accidentally finding files that were meant to be excluded |
| 31 | + # https://github.com/ipetkov/crane/issues/985 |
| 32 | + optionals (filter cur type) ( |
| 33 | + if type == "directory" then |
| 34 | + listFilesRecursive filter (name == ".cargo") cur |
| 35 | + else if isCargoToml then |
| 36 | + [ |
| 37 | + { |
| 38 | + path = cur; |
| 39 | + type = "cargoTomls"; |
| 40 | + } |
| 41 | + ] |
| 42 | + else if isConfig then |
| 43 | + [ |
| 44 | + { |
| 45 | + path = cur; |
| 46 | + type = "cargoConfigs"; |
| 47 | + } |
| 48 | + ] |
| 49 | + else |
| 50 | + [ ] |
| 51 | + ) |
| 52 | + ) |
45 | 53 | ); |
46 | 54 |
|
47 | | - foundFiles = listFilesRecursive false src; |
48 | | - grouped = groupBy (x: x.type) foundFiles; |
49 | | - cleaned = mapAttrs (_: map (y: y.path)) grouped; |
50 | | - |
51 | 55 | # Ensure we have a well typed result |
52 | 56 | default = { |
53 | 57 | cargoTomls = [ ]; |
54 | 58 | cargoConfigs = [ ]; |
55 | 59 | }; |
56 | 60 | in |
| 61 | +src: |
| 62 | +let |
| 63 | + isCleanSourceWith = src._isLibCleanSourceWith or false; |
| 64 | + origSrc = if isCleanSourceWith then src.origSrc or src else src; |
| 65 | + filter = if isCleanSourceWith then src.filter or defaultFilter else defaultFilter; |
| 66 | + |
| 67 | + foundFiles = listFilesRecursive filter false origSrc; |
| 68 | + grouped = groupBy (x: x.type) foundFiles; |
| 69 | + cleaned = mapAttrs (_: map (y: y.path)) grouped; |
| 70 | +in |
57 | 71 | default // cleaned |
0 commit comments