aboutsummaryrefslogtreecommitdiff
path: root/generate-ttl-files.scm
blob: 2f14b255dc0d39e7f5cfa2e011aeaec551296a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env guile
!#

(use-modules (ice-9 format)
             (ice-9 getopt-long)
             (guix build utils)
             (srfi srfi-26)
             (srfi srfi-34)
             (srfi srfi-35))


(define (call-with-temporary-directory proc)
  (let ((tmp-dir (mkdtemp "/tmp/generate-ttl-files.XXXXXX")))
    (dynamic-wind
      (const #t)
      (cut proc tmp-dir)
      (cut delete-file-recursively tmp-dir))))

(let* ((option-spec
        '((settings (single-char #\s) (value #t))
          (documentation (single-char #\d) (value #t))
          (output (single-char #\o) (value #t))))
       (options (getopt-long (command-line) option-spec))
       (settings (canonicalize-path (option-ref options 'settings #f)))
       (output (canonicalize-path (option-ref options 'output #f)))
       (documentation (canonicalize-path (option-ref options 'documentation #f)))
       (%source-dir (dirname (current-filename))))
  (call-with-temporary-directory
   (lambda (tmpdir)
     (let* ((gn-docs-dir (string-append tmpdir "/gn-docs"))
            (rdf-documentation (string-append gn-docs-dir "/rdf-documentation")))
       (unless (file-exists? output)
         (mkdir-p output))

       (invoke "git" "clone" "--depth" "1"
               documentation gn-docs-dir)
       ;; Delete all the files in the gn-docs/rdf-documentation
       (delete-file-recursively rdf-documentation)
       (mkdir-p rdf-documentation)
       ;; Transform data to RDF
       (for-each (lambda (file)
                   (let* ((base-file-name (basename file ".scm"))
                          (pre-inst-env (canonicalize-path "./pre-inst-env"))
                          (ttl-file (string-append (canonicalize-path output)
                                                   "/" base-file-name ".ttl"))
                          (md-file (format #f "~a/~a.md" rdf-documentation base-file-name)))
                     ;; Ignore dataset-metadata-git.scm because TODO
                     (unless (string=? base-file-name "dataset-metadata-git")
                       (invoke "./pre-inst-env" file "--settings" settings
                               "--output" ttl-file
                               "--documentation" md-file))))
                 (find-files "./examples" ".scm"))
       (copy-recursively "./schema" output)
       ;; Validate transformed turtle files
       (for-each (lambda (file)
                   (invoke "rapper" "--input" "turtle" "--count" file))
                 (append (find-files output ".ttl")
                         (find-files "./schema" ".ttl")))
       ;; Push changes to git when data is correctly valldated
       (guard (c ((invoke-error? c)
                  (invoke "git" "-C" gn-docs-dir "add" rdf-documentation)
                  (invoke "git" "-C" gn-docs-dir "commit" "--no-gpg-sign" "-m"
                          "Update RDF documentation.\n\n* Commit made via the generate-ttl-files.scm script\"")
                  (invoke "git" "-C" gn-docs-dir "push" "origin" "master")))
         (invoke "git" "-C" gn-docs-dir "diff" "--exit-code"))))))