gnu: signify: Update to 26.
[guix.git] / guix / import / crate.scm
blob52c5cb1c3069a82001bfab9eda136cc90fb8ad6f
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 David Craven <david@craven.ch>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix import crate)
20   #:use-module (guix base32)
21   #:use-module (guix build-system cargo)
22   #:use-module ((guix download) #:prefix download:)
23   #:use-module (gcrypt hash)
24   #:use-module (guix http-client)
25   #:use-module (guix import json)
26   #:use-module (guix import utils)
27   #:use-module ((guix licenses) #:prefix license:)
28   #:use-module (guix monads)
29   #:use-module (guix packages)
30   #:use-module (guix upstream)
31   #:use-module (guix utils)
32   #:use-module (ice-9 match)
33   #:use-module (ice-9 pretty-print) ; recursive
34   #:use-module (json)
35   #:use-module (srfi srfi-1)
36   #:use-module (srfi srfi-2)
37   #:use-module (srfi srfi-26)
38   #:export (crate->guix-package
39             guix-package->crate-name
40             %crate-updater))
42 (define (crate-fetch crate-name callback)
43   "Fetch the metadata for CRATE-NAME from crates.io and call the callback."
45   (define (crates->inputs crates)
46     (sort (map (cut assoc-ref <> "crate_id") crates) string-ci<?))
48   (define (string->license string)
49     (map spdx-string->license (string-split string #\/)))
51   (define (crate-kind-predicate kind)
52     (lambda (dep) (string=? (assoc-ref dep "kind") kind)))
54   (and-let* ((crate-json (json-fetch (string-append crate-url crate-name)))
55              (crate (assoc-ref crate-json "crate"))
56              (name (assoc-ref crate "name"))
57              (version (assoc-ref crate "max_version"))
58              (homepage (assoc-ref crate "homepage"))
59              (repository (assoc-ref crate "repository"))
60              (synopsis (assoc-ref crate "description"))
61              (description (assoc-ref crate "description"))
62              (license (or (and=> (assoc-ref crate "license")
63                                  string->license)
64                           '()))                   ;missing license info
65              (path (string-append "/" version "/dependencies"))
66              (deps-json (json-fetch (string-append crate-url name path)))
67              (deps (vector->list (assoc-ref deps-json "dependencies")))
68              (dep-crates (filter (crate-kind-predicate "normal") deps))
69              (dev-dep-crates
70               (filter (lambda (dep)
71                         (not ((crate-kind-predicate "normal") dep))) deps))
72              (cargo-inputs (crates->inputs dep-crates))
73              (cargo-development-inputs (crates->inputs dev-dep-crates))
74              (home-page (match homepage
75                           (() repository)
76                           (_ homepage))))
77     (callback #:name name #:version version
78               #:cargo-inputs cargo-inputs
79               #:cargo-development-inputs cargo-development-inputs
80               #:home-page home-page #:synopsis synopsis
81               #:description description #:license license)))
83 (define (maybe-cargo-inputs package-names)
84   (match (package-names->package-inputs package-names)
85     (()
86      '())
87     ((package-inputs ...)
88      `(#:cargo-inputs ,package-inputs))))
90 (define (maybe-cargo-development-inputs package-names)
91   (match (package-names->package-inputs package-names)
92     (()
93      '())
94     ((package-inputs ...)
95      `(#:cargo-development-inputs ,package-inputs))))
97 (define (maybe-arguments arguments)
98   (match arguments
99     (()
100      '())
101     ((args ...)
102      `((arguments (,'quasiquote ,args))))))
104 (define* (make-crate-sexp #:key name version cargo-inputs cargo-development-inputs
105                           home-page synopsis description license
106                           #:allow-other-keys)
107   "Return the `package' s-expression for a rust package with the given NAME,
108 VERSION, CARGO-INPUTS, CARGO-DEVELOPMENT-INPUTS, HOME-PAGE, SYNOPSIS, DESCRIPTION,
109 and LICENSE."
110   (let* ((port (http-fetch (crate-uri name version)))
111          (guix-name (crate-name->package-name name))
112          (cargo-inputs (map crate-name->package-name cargo-inputs))
113          (cargo-development-inputs (map crate-name->package-name
114                                         cargo-development-inputs))
115          (pkg `(package
116                    (name ,guix-name)
117                    (version ,version)
118                    (source (origin
119                              (method url-fetch)
120                              (uri (crate-uri ,name version))
121                              (file-name (string-append name "-" version ".tar.gz"))
122                              (sha256
123                               (base32
124                                ,(bytevector->nix-base32-string (port-sha256 port))))))
125                    (build-system cargo-build-system)
126                    ,@(maybe-arguments (append (maybe-cargo-inputs cargo-inputs)
127                                               (maybe-cargo-development-inputs
128                                                 cargo-development-inputs)))
129                    (home-page ,(match home-page
130                                  (() "")
131                                  (_ home-page)))
132                    (synopsis ,synopsis)
133                    (description ,(beautify-description description))
134                    (license ,(match license
135                                (() #f)
136                                ((license) license)
137                                (_ `(list ,@license)))))))
138          (close-port port)
139          pkg))
141 (define (crate->guix-package crate-name)
142   "Fetch the metadata for CRATE-NAME from crates.io, and return the
143 `package' s-expression corresponding to that package, or #f on failure."
144   (crate-fetch crate-name make-crate-sexp))
146 (define (guix-package->crate-name package)
147   "Return the crate name of PACKAGE."
148   (and-let* ((origin (package-source package))
149              (uri (origin-uri origin))
150              (crate-url? uri)
151              (len (string-length crate-url))
152              (path (xsubstring uri len))
153              (parts (string-split path #\/)))
154     (match parts
155       ((name _ ...) name))))
157 (define (crate-name->package-name name)
158   (string-append "rust-" (string-join (string-split name #\_) "-")))
161 ;;; Updater
164 (define (crate-package? package)
165   "Return true if PACKAGE is a Rust crate from crates.io."
166   (let ((source-url (and=> (package-source package) origin-uri))
167         (fetch-method (and=> (package-source package) origin-method)))
168     (and (eq? fetch-method download:url-fetch)
169          (match source-url
170            ((? string?)
171             (crate-url? source-url))
172            ((source-url ...)
173             (any crate-url? source-url))))))
175 (define (latest-release package)
176   "Return an <upstream-source> for the latest release of PACKAGE."
177   (let* ((crate-name (guix-package->crate-name package))
178          (callback (lambda* (#:key version #:allow-other-keys) version))
179          (version (crate-fetch crate-name callback))
180          (url (crate-uri crate-name version)))
181     (upstream-source
182      (package (package-name package))
183      (version version)
184      (urls (list url)))))
186 (define %crate-updater
187   (upstream-updater
188    (name 'crates)
189    (description "Updater for crates.io packages")
190    (pred crate-package?)
191    (latest latest-release)))