diff options
author | Munyoki Kilyungi | 2024-10-21 13:53:56 +0300 |
---|---|---|
committer | Munyoki Kilyungi | 2024-10-21 13:53:56 +0300 |
commit | 2e954105f3cdd1a93c41f8e3168677ac0355e94f (patch) | |
tree | df8f0a5a1e1c246a3dccf0ffabc8107680d1a611 | |
parent | c0936aea74122ea225966b2a353a59a174b2a7b7 (diff) | |
download | gn-transform-databases-2e954105f3cdd1a93c41f8e3168677ac0355e94f.tar.gz |
Use (guix build utils) to generate, validate, and version TTL files.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rwxr-xr-x | generate-ttl-files.scm | 108 |
1 files changed, 57 insertions, 51 deletions
diff --git a/generate-ttl-files.scm b/generate-ttl-files.scm index cf9d2a0..2f14b25 100755 --- a/generate-ttl-files.scm +++ b/generate-ttl-files.scm @@ -1,61 +1,67 @@ -#! ./pre-inst-env +#! /usr/bin/env guile !# + (use-modules (ice-9 format) - (ice-9 futures) (ice-9 getopt-long) - (ice-9 ftw)) + (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)) - (output (single-char #\o) (value #t)) - (documentation (single-char #\d) (value #t)))) + (documentation (single-char #\d) (value #t)) + (output (single-char #\o) (value #t)))) (options (getopt-long (command-line) option-spec)) - (settings (option-ref options 'settings #f)) - (output (option-ref options 'output #f)) - (documentation (option-ref options 'documentation #f))) - (define (enter? name stat result) - stat result ;ignore - ;; Skip version control directories if any. - (not (member (basename name) '(".git" ".svn" "CVS")))) - - (define (leaf name stat result) - stat result ;ignore - (when (string-suffix? ".scm" name) - (let* ((base-file-name (basename name ".scm")) - (cmd (format #f " ~a --settings ~a --output ~a --documentation ~a" - name - settings - (string-append output "/" base-file-name ".ttl") - (string-append documentation "/" base-file-name ".md")))) - (unless (string? base-file-name "dataset-metadata-git") - (touch - (future - (begin - (display (format #f "Running ~a" cmd)) - (display "\n") - (system cmd)))))))) - - (define (down name stat result) - name stat ;ignore - result) - - (define (up name stat result) - name stat ;ignore - result) - - (define (skip name stat result) - name stat ;ignore - result) - - ;; Ignore unreadable files/directories but warn the user. - (define (error name stat errno result) - stat ;ignore - (format (current-error-port) "warning: ~a: ~a~%" - name (strerror errno)) - result) + (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)) - (file-system-fold enter? leaf down up skip error - 0 ;initial counter is zero bytes - "./examples")) + (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")))))) |