Subject: [PATCH v2 05/23] DRAFT import: juliahub: Add support for native-inputs.

From: Simon Tournier <[email-masked]>
Date: Mon, 18 Sep 2023 20:03:12 +0200

From: Nicolas Graves via Guix-patches via <[email-masked]>

Signed-off-by: Simon Tournier <[email-masked]>
---
 guix/import/juliahub.scm | 105 ++++++++++++++++++++++++---------------
 1 file changed, 64 insertions(+), 41 deletions(-)

diff --git a/guix/import/juliahub.scm b/guix/import/juliahub.scm
index efe6abbb2481..4544dee98016 100644
--- a/guix/import/juliahub.scm
+++ b/guix/import/juliahub.scm
@@ -20,13 +20,14 @@ (define-module (guix import juliahub)
   #:use-module (ice-9 textual-ports)
   #:use-module (ice-9 regex)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 streams)
   #:use-module (ice-9 string-fun)
-  #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-9)
+  #:use-module (srfi srfi-11)
   #:use-module (guix http-client)
   #:use-module (guix import utils)
   #:use-module (guix import json)
-  #:use-module (guix base16)
   #:use-module (guix base32)
   #:use-module (guix packages)
   #:use-module (guix upstream)
@@ -66,19 +67,42 @@ (define (json->juliahub-direct-dependencies vector)
                #f)))
        (vector->list vector))))
 
+(define (ini-list->extra-dependencies lst)
+  (match lst
+    (('(extras) ooo ...)
+     (extra-list->extra-dependencies ooo))
+    (((tag) ooo ...)
+     (ini-list->extra-dependencies ooo))
+    ((attribute '= value ooo ...)
+     (ini-list->extra-dependencies ooo))
+    ('()
+     '())))
+
+(define (extra-list->extra-dependencies lst)
+  (match lst
+    ((attribute '= value ooo ...)
+     `(,(symbol->string attribute) ,@(extra-list->extra-dependencies ooo)))
+    (((tag) ooo ...)
+     '())
+    ('()
+     '())))
+
+(define (parse-extra-dependencies directory)
+  (let* ((port (open-input-file (string-append directory "/Project.toml")))
+         (ini-list (stream->list (port->stream port read))))
+    (close-port port)
+    (ini-list->extra-dependencies ini-list)))
+
 ;; Julia package.
 (define-json-mapping <juliahub-package> make-juliahub-package juliahub-package?
   json->juliahub-package
   (homepage juliahub-package-homepage) ;string
   (readme juliahub-package-readme) ;string
-  ;; (slug juliahub-package-slug) ;string
   (version juliahub-package-version) ;string
   (description juliahub-package-description) ;string
-  (dependencies
-   juliahub-package-dependencies "deps"
+  (direct-dependencies
+   juliahub-package-direct-dependencies "deps"
    json->juliahub-direct-dependencies) ;list of <juliahub-dependency>
-   ;; (lambda (vector)
-     ;; (map json->juliahub-dependency (vector->list vector))))
   (url juliahub-package-url) ;string
   (uuid juliahub-package-uuid) ;string
   (license juliahub-package-license)) ;string
@@ -90,7 +114,6 @@ (define-json-mapping <juliahub-dependency>
   (name juliahub-dependency-name) ;string
   (uuid juliahub-dependency-uuid) ;string
   (versions juliahub-dependency-versions "versions" vector->list)) ;list of strings
-  ;; (slug juliahub-dependency-slug) ;string
 
 (define (julia-name->guix-name name)
   (string-append "julia-" (snake-case name)))
@@ -100,27 +123,25 @@ (define* (juliahub-fetch name #:key (version #f))
   (and=> (json-fetch (string-append (juliahub-url name) "pkg.json"))
          json->juliahub-package))
 
-(define (make-julia-sexp name version uri hash home-page synopsis description
-                         dependencies licenses)
+(define (make-julia-sexp name source home-page synopsis description
+                         direct-dependencies test-dependencies-names licenses)
   "Return the `package' s-expression for a Julia package with the given NAME,
-VERSION, URI, HASH, HOME-PAGE, DESCRIPTION, DEPENDENCIES, and LICENSES."
+VERSION, URI, HASH, HOME-PAGE, DESCRIPTION, DEPENDENCIES,
+TEST-DEPENDENCIES-NAMES and LICENSES."
   `(package
      (name ,(julia-name->guix-name name))
      (version ,version)
-     (source (origin
-               (method url-fetch)
-               (uri ,uri)
-               (sha256
-                (base32
-                 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5"
-                 ;; ,(bytevector->nix-base32-string hash)
-                 ))))
+     (source ,source)
      (build-system julia-build-system)
-     ,@(if (null? dependencies)
+     ,@(if (null? direct-dependencies)
            '()
-           `((inputs
+           `((propagated-inputs
               (list ,@(map (compose julia-name->guix-name juliahub-dependency-name)
-                           dependencies)))))
+                           direct-dependencies)))))
+     ,@(if (null? test-dependencies-names)
+           '()
+           `((native-inputs
+              (list ,@(map julia-name->guix-name test-dependencies-names)))))
      (synopsis ,synopsis)
      (description ,description)
      (home-page ,home-page)
@@ -135,26 +156,28 @@ (define* (juliahub->guix-package package-name
 `package' s-expression corresponding to that package, or #f on failure.
 Optionally include a VERSION string to fetch a specific version juliahub."
   (let ((package (if version
-                      (juliahub-fetch package-name version)
-                      (juliahub-fetch package-name))))
+                     (juliahub-fetch package-name version)
+                     (juliahub-fetch package-name))))
     (if package
-        (let* ((dependencies-names
-                (map juliahub-dependency-name
-                     (juliahub-package-dependencies package)))
-               (licenses
-                (map spdx-string->license
-                     (list (juliahub-package-license package)))))
-          (values (make-julia-sexp
-                   package-name
-                   (juliahub-package-version package)
-                   (juliahub-package-url package)
-                   "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5"
-                   (juliahub-package-homepage package)
-                   (juliahub-package-description package)
-                   (beautify-description (juliahub-package-readme package))
-                   (juliahub-package-dependencies package)
-                   licenses)
-                  dependencies-names))
+        (let-values (((source directory)
+                     (git->origin+dir url `(tag-or-commit . ,package-version))))
+          (let* ((dependencies-names
+                  (map juliahub-dependency-name
+                       (juliahub-package-direct-dependencies package)))
+                 (licenses
+                  (map spdx-string->license
+                       (list (juliahub-package-license package))))
+                 (test-dependencies-names (parse-extra-dependencies directory)))
+            (values (make-julia-sexp
+                     package-name
+                     source
+                     (juliahub-package-homepage package)
+                     (juliahub-package-description package)
+                     (beautify-description (juliahub-package-readme package))
+                     (juliahub-package-direct-dependencies package)
+                     test-dependencies-names
+                     licenses)
+                    (append dependencies-names test-dependencies))))
         (values #f '()))))
 
 (define* (import-release package #:key (version #f))
-- 
2.38.1