diff options
author | Matthew Bauer <mjbauer95@gmail.com> | 2018-06-27 15:35:07 -0400 |
---|---|---|
committer | Matthew Bauer <mjbauer95@gmail.com> | 2018-06-27 15:35:07 -0400 |
commit | 3210dd3039798a10884925cae98ef3b2a4c10703 (patch) | |
tree | 7d70dbfc4e1192085f112f9cce5137fb48610459 /lib/generators.nix | |
parent | f194659ddbac889755737e7e2dc09b0bb821396a (diff) | |
download | nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar.gz nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar.bz2 nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar.lz nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar.xz nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.tar.zst nixlib-3210dd3039798a10884925cae98ef3b2a4c10703.zip |
generators: add PLIST handling
/cc @LnL7 @3noch
Diffstat (limited to 'lib/generators.nix')
-rw-r--r-- | lib/generators.nix | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/generators.nix b/lib/generators.nix index c09384c00f57..aab4498f9c64 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -175,4 +175,52 @@ rec { else "<λ:{${showFnas}}>" else abort "toPretty: should never happen (v = ${v})"; + # PLIST handling + + toPLIST = x: '' + <?xml version="1.0" encoding="UTF-8"?> + <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> + <plist version="1.0"> + '' + pprExpr "" x + + "\n</plist>"; + + pprExpr = ind: x: with builtins; + if isNull x then "" else + if isBool x then pprBool ind x else + if isInt x then pprInt ind x else + if isString x then pprStr ind x else + if isList x then pprList ind x else + if isAttrs x then pprAttrs ind x else + throw "invalid plist type"; + + pprLiteral = ind: x: ind + x; + + pprBool = ind: x: pprLiteral ind (if x then "<true/>" else "<false/>"); + pprInt = ind: x: pprLiteral ind "<integer>${toString x}</integer>"; + pprStr = ind: x: pprLiteral ind "<string>${x}</string>"; + pprKey = ind: x: pprLiteral ind "<key>${x}</key>"; + + pprIndent = ind: pprExpr "\t${ind}"; + + pprItem = ind: libStr.concatMapStringsSep "\n" (pprIndent ind); + + pprList = ind: x: libStr.concatStringsSep "\n" [ + (pprLiteral ind "<array>") + (pprItem ind x) + (pprLiteral ind "</array>") + ]; + + pprAttrs = ind: x: libStr.concatStringsSep "\n" [ + (pprLiteral ind "<dict>") + (pprAttr ind x) + (pprLiteral ind "</dict>") + ]; + + attrFilter = name: value: name != "_module" && value != null; + + pprAttr = ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList (name: value: lib.optional (attrFilter name value) [ + (pprKey "\t${ind}" name) + (pprExpr "\t${ind}" value) + ]) x)); + } |