home: Add ini file utility function. * home/utils.scm (make-ini-file): New procedure. * doc/general.md: Document it.
doc/general.md
34 | 34 | (symlink-file-home "/data/alice/.mozilla" ".mozilla") | |
35 | 35 | ``` | |
36 | 36 | ||
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 | + | ||
37 | 63 | Build-side Utilities | |
38 | 64 | -------------------- | |
39 | 65 | ||
… | |||
44 | 70 | ||
45 | 71 | Return the complete path to the output of a package being defined by adding | |
46 | 72 | "/" 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 | > | ||
48 | 0 | > | \ No newline at end of file |
73 | + | in the package definition. This is not very useful for end users. |
home/utils.scm
18 | 18 | (define-module (home utils) | |
19 | 19 | #:use-module (guix build utils) | |
20 | 20 | #:use-module (guix gexp) | |
21 | + | #:use-module (ice-9 match) | |
21 | 22 | #:export (simple-file-home | |
22 | - | symlink-file-home)) | |
23 | + | symlink-file-home | |
24 | + | make-ini-file)) | |
23 | 25 | ||
24 | 26 | (define (simple-file-home file-gexp location) | |
25 | 27 | "Add @var{file-gexp}, a file-like object, to the user home at @var{location}." | |
… | |||
41 | 43 | (symlink #$from (string-append #$output "/" #$to))) | |
42 | 44 | #:options | |
43 | 45 | '(#:local-build? #t | |
44 | - | #:modules ((guix build utils))))) | |
44 | > | ||
45 | 0 | > | \ 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"))) |