nix-helpers: a2e4c2c0ace7368d90f5d75a1151adc42ec629a8

     1: # Read the contents of a directory, building up an attrset of the paths. For
     2: # example, given:
     3: #
     4: #   foo/
     5: #     bar.html
     6: #     baz/
     7: #       quux.mp3
     8: #
     9: # We will get:
    10: #
    11: #   {
    12: #     foo = {
    13: #       "bar.html" = /path/to/foo/bar.html;
    14: #       baz        = {
    15: #         "quux.mp3" = /path/to/foo/baz/quux.mp3;
    16: #       };
    17: #     };
    18: #
    19: { attrsToDirs', die, isPath, lib, runCommand }:
    20: 
    21: with builtins;
    22: with lib;
    23: with rec {
    24:   go = dir:
    25:     mapAttrs (n: v:
    26:       if v == "regular" || v == "symlink" then
    27:         dir + "/${n}"
    28:       else
    29:         go (dir + "/${n}")) (readDir dir);
    30: };
    31: 
    32: # Check that we can access some known files/directories
    33: with {
    34:   test = go (runCommand "dirsToAttrs-test-data" { } ''
    35:     mkdir -p "$out/foo"
    36:     echo "baz" > "$out/foo/bar"
    37:   '');
    38: };
    39: assert isAttrs test || die {
    40:   error = "test isn't attrset";
    41:   type = typeOf test;
    42: };
    43: assert test ? foo || die {
    44:   error = "No 'foo' in test";
    45:   names = attrNames test;
    46: };
    47: assert isAttrs test.foo || {
    48:   error = "test.foo isn't attrset";
    49:   type = typeOf test.foo;
    50: };
    51: assert test.foo ? bar || {
    52:   error = "No 'bar' in test.foo";
    53:   names = attrNames test.foo;
    54: };
    55: assert isPath test.foo.bar || {
    56:   error = "test.foo.bar isn't path";
    57:   type = typeOf test.foo.bar;
    58: };
    59: go

Generated by git2html.