liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

parse-options.fnl (2838B)


      1 (local yaml (require :lyaml))
      2 
      3 (local { : view } (require :fennel))
      4 
      5 (fn headline [name]
      6   (let [(_ _ basename) (string.find name ".*/([^/].*)")
      7         len (basename:len)]
      8     (print basename)
      9     (print (string.rep "=" len))))
     10 
     11 (fn read-preamble [pathname]
     12   (if (= (pathname:sub 1 1) "/")
     13       (let [pathname (if (string.match pathname ".nix$")
     14                          pathname
     15                          (.. pathname "/default.nix"))]
     16         (with-open [f (assert (io.open pathname :r))]
     17           (accumulate [lines ""
     18                        l (f:lines)
     19                        :until (not (= (string.sub l 1 2) "##"))]
     20             (.. lines (string.gsub l "^## *" "") "\n"))))))
     21 
     22 (fn strip-newlines [text]
     23   (and text
     24        (-> text
     25            (string.gsub "\n([^\n])" " %1")
     26            (string.gsub "\n\n+" "\n"))))
     27 
     28 (fn indent [n text]
     29   (let [margin (string.rep " " n)]
     30     (.. margin (string.gsub text "\n +" (.. "\n" margin )))))
     31 
     32 (fn extract-text [description]
     33   (and description
     34 ;       (do (print (view description)) true)
     35        (-> (match description
     36              { :type "literalExpression" : text } text
     37              {} nil
     38              nil nil
     39              t description)
     40            strip-newlines)))
     41 
     42 (fn print-option [o offset]
     43   (let [i (or offset 0)]
     44     (print (indent i (.. " * option ``" o.name "``")))
     45     (print (indent (+ 4 i)
     46                    (or (extract-text o.description) "(no description)")))
     47     (print)
     48     (print (indent (+ 4 i) (.. "**type** " o.type "\n")))
     49     (print (indent (+ 4 i)
     50                    (.. "**default** "
     51                        (or (extract-text (?. o :default)) "(none)")
     52                        "\n"
     53                        )))
     54     (print )))
     55 
     56 (fn print-service [o]
     57   (print (.. " * service ``" o.name "``"))
     58   (print (indent 4 (or (extract-text o.description) "(no description)")))
     59   (print)
     60   (print (indent 4 "**Service parameters**\n"))
     61   (each [_ param (ipairs o.parameters)]
     62     (print-option param 4)))
     63 
     64 (fn sort-options [module]
     65   (table.sort module (fn [a b] (< a.name b.name)))
     66   module)
     67 
     68 (let [raw (yaml.load (io.read "*a"))
     69       modules {}]
     70   (each [_ option (ipairs raw)]
     71     (each [_ path (ipairs option.declarations)]
     72       (let [e (or (. modules path) [])]
     73         (table.insert e option)
     74         (tset modules path e))))
     75   (each [name module (pairs modules)]
     76     (print (or (read-preamble name) (headline name)))
     77     (let [options (sort-options module)]
     78       (each [_ o (ipairs options)]
     79         (if (= o.type "parametrisable s6-rc service definition")
     80             (print-service o)
     81             (print-option o))))))
     82 
     83 ;; for each element el, add to table modules keyed on
     84 ;; el.declarations
     85 
     86 ;; for each value in modules
     87 ;;   print title
     88 ;;   elements = (sort elements on el.name)
     89 ;;   for each el in elements
     90 ;;     is option or service? print whichever
     91 ;;