home: Add ini file utility function.

Julien LepillerWed Jun 26 23:03:11+0200 2019

d91ecf0

home: Add ini file utility function. * home/utils.scm (make-ini-file): New procedure. * doc/general.md: Document it.

doc/general.md

3434
(symlink-file-home "/data/alice/.mozilla" ".mozilla")
3535
```
3636
37+
**Scheme Procedure**: (make-ini-file name config)
38+
39+
Create an ini file with name _name_ and content defined by _config_.  _config_
40+
is an association list between section names and content.  The content of a
41+
section is an association list between keys and values.  Values can be strings,
42+
integers or booleans.
43+
44+
For instance, the folowing procedure call:
45+
46+
```scheme
47+
(make-ini-file "foo.ini" '(("General" (("foo" 1) ("bar" 2)))
48+
                           ("Init" (("baz" "foobar") ("foo" #t)))))
49+
```
50+
51+
will produce the following ini file:
52+
53+
```ini
54+
[General]
55+
foo=1
56+
bar=2
57+
58+
[Init]
59+
baz=foobar
60+
foo=true
61+
```
62+
3763
Build-side Utilities
3864
--------------------
3965

4470
4571
Return the complete path to the output of a package being defined by adding
4672
"/" between each component of _path_. _outputs_ is the content of `%build-output`
47-
in the package definition. This is not very useful for end users.
47>
480>
\ No newline at end of file
73+
in the package definition. This is not very useful for end users.

home/utils.scm

1818
(define-module (home utils)
1919
  #:use-module (guix build utils)
2020
  #:use-module (guix gexp)
21+
  #:use-module (ice-9 match)
2122
  #:export (simple-file-home
22-
            symlink-file-home))
23+
            symlink-file-home
24+
            make-ini-file))
2325
2426
(define (simple-file-home file-gexp location)
2527
  "Add @var{file-gexp}, a file-like object, to the user home at @var{location}."

4143
        (symlink #$from (string-append #$output "/" #$to)))
4244
    #:options
4345
    '(#:local-build? #t
44-
      #:modules ((guix build utils)))))
44>
450>
\ No newline at end of file
46+
      #:modules ((guix build utils)))))
47+
48+
(define (make-ini-file name config)
49+
  (define (make-ini-section name config)
50+
    (string-append "[" name "]\n"
51+
                   (string-join
52+
                     (map (lambda (conf)
53+
                            (match conf
54+
                              ((key value)
55+
                               (string-append key "="
56+
                                              (match value
57+
                                                ((? string? val) val)
58+
                                                ((? number? val) (number->string val))
59+
                                                (#t "true")
60+
                                                (#f "false"))))))
61+
                          config)
62+
                     "\n")))
63+
  (plain-file name
64+
    (string-join (map (lambda (section)
65+
                        (match section
66+
                          ((name content)
67+
                           (make-ini-section name content))))
68+
                        config) "\n\n")))