import: opam: Factor out source import.

This also ensures a package can be imported even when it does not
specify a URL.

* guix/import/opam.scm (opam->guix-source): New procedure.
(opam->guix-package): Use it.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Julien Lepiller 2022-04-08 15:22:43 +02:00 committed by Ludovic Courtès
parent be7338d400
commit d59abc336d
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5

View file

@ -324,6 +324,20 @@ (define* (opam-fetch name #:optional (repositories-specs '("opam")))
(filter-map get-opam-repository repositories-specs))
(warning (G_ "opam: package '~a' not found~%") name)))
(define (opam->guix-source url-dict)
(let ((source-url (and url-dict
(or (metadata-ref url-dict "src")
(metadata-ref url-dict "archive")))))
(if source-url
(call-with-temporary-output-file
(lambda (temp port)
(and (url-fetch source-url temp)
`(origin
(method url-fetch)
(uri ,source-url)
(sha256 (base32 ,(guix-hash-url temp)))))))
'no-source-information)))
(define* (opam->guix-package name #:key (repo 'opam) version)
"Import OPAM package NAME from REPOSITORY (a directory name) or, if
REPOSITORY is #f, from the official OPAM repository. Return a 'package' sexp
@ -332,9 +346,7 @@ (define* (opam->guix-package name #:key (repo 'opam) version)
(opam-file (opam-fetch name with-opam))
(version (assoc-ref opam-file "version"))
(opam-content (assoc-ref opam-file "metadata"))
(url-dict (metadata-ref opam-content "url"))
(source-url (or (metadata-ref url-dict "src")
(metadata-ref url-dict "archive")))
(source (opam->guix-source (metadata-ref opam-content "url")))
(requirements (metadata-ref opam-content "depends"))
(names (dependency-list->names requirements))
(dependencies (filter-dependencies names))
@ -348,41 +360,34 @@ (define* (opam->guix-package name #:key (repo 'opam) version)
(not (member name '("dune" "jbuilder"))))
native-dependencies))))
(let ((use-dune? (member "dune" names)))
(call-with-temporary-output-file
(lambda (temp port)
(and (url-fetch source-url temp)
(values
`(package
(name ,(ocaml-name->guix-name name))
(version ,version)
(source
(origin
(method url-fetch)
(uri ,source-url)
(sha256 (base32 ,(guix-hash-url temp)))))
(build-system ,(if use-dune?
'dune-build-system
'ocaml-build-system))
,@(if (null? inputs)
'()
`((propagated-inputs (list ,@inputs))))
,@(if (null? native-inputs)
'()
`((native-inputs (list ,@native-inputs))))
,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
'()
`((properties
,(list 'quasiquote `((upstream-name . ,name))))))
(home-page ,(metadata-ref opam-content "homepage"))
(synopsis ,(metadata-ref opam-content "synopsis"))
(description ,(beautify-description
(metadata-ref opam-content "description")))
(license ,(spdx-string->license
(metadata-ref opam-content "license"))))
(filter
(lambda (name)
(not (member name '("dune" "jbuilder"))))
dependencies))))))))
(values
`(package
(name ,(ocaml-name->guix-name name))
(version ,version)
(source ,source)
(build-system ,(if use-dune?
'dune-build-system
'ocaml-build-system))
,@(if (null? inputs)
'()
`((propagated-inputs (list ,@inputs))))
,@(if (null? native-inputs)
'()
`((native-inputs (list ,@native-inputs))))
,@(if (equal? name (guix-name->opam-name (ocaml-name->guix-name name)))
'()
`((properties
,(list 'quasiquote `((upstream-name . ,name))))))
(home-page ,(metadata-ref opam-content "homepage"))
(synopsis ,(metadata-ref opam-content "synopsis"))
(description ,(beautify-description
(metadata-ref opam-content "description")))
(license ,(spdx-string->license
(metadata-ref opam-content "license"))))
(filter
(lambda (name)
(not (member name '("dune" "jbuilder"))))
dependencies)))))
(define* (opam-recursive-import package-name #:key repo)
(recursive-import package-name