From b46138826260f1ed963ffe18fef7ec4ce70b6de8 Mon Sep 17 00:00:00 2001
From: l-d-s <l@sdf.org>
Date: Mon, 12 Nov 2018 16:54:03 +1100
Subject: Function to great ini-style gridss config file.

---
 tools/gridss-configFile.nix | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 tools/gridss-configFile.nix

(limited to 'tools')

diff --git a/tools/gridss-configFile.nix b/tools/gridss-configFile.nix
new file mode 100644
index 0000000..838c292
--- /dev/null
+++ b/tools/gridss-configFile.nix
@@ -0,0 +1,36 @@
+{bionix, nixpkgs}:
+
+with nixpkgs;
+
+let
+    attrsToGridssConfigString = attrsToGridssConfigStringPrepend "";
+
+    attrsToGridssConfigStringPrepend = prepend: attrs:
+        lib.concatStringsSep "\n" (
+            lib.attrValues (
+                lib.mapAttrs
+                    (name: attr: prepend + (iniLine name attr))
+                    attrs));
+
+    iniLine = name: attr:
+        let attrType = builtins.typeOf attr;
+        in
+            if (iniLineByAttrType ? ${attrType})
+            then (iniLineByAttrType.${attrType} name attr)
+            else builtins.throw (
+               "`gridssConfig` cannot convert attribute of type \"" + attrType + "\".");
+
+    iniLineByAttrType = {
+        string = name: attr: name + " = " + attr;
+        int    = name: attr: name + " = " + builtins.toString attr;
+        float  = name: attr: name + " = " + (
+                    builtins.head (
+                        builtins.match "([0-9]+\.0?[1-9]*)0+" (builtins.toString attr)));
+        bool   = name: attr: name + " = " + (if attr == true then "true" else "false");
+        attrs  = name: attr: attrsToGridssConfigStringPrepend (name + ".") attr;
+        # Allows for repeated fields (e.g. for adapters):
+        list   = name: attr: concatStringsSep "\n" (map (x: iniLine name x) attr);
+    };
+in configAttrs: (writeText
+        "gridss.properties.override"
+        (attrsToGridssConfigString configAttrs))
-- 
cgit v1.2.3