diff mbox

[3/7] import: crate: Add crate updater.

Message ID 20160928151538.11679-3-david@craven.ch
State New
Headers show

Commit Message

David Craven Sept. 28, 2016, 3:15 p.m. UTC
* guix/import/crate.scm (crate-package?, latest-release,
  %crate-updater): New variables.
* guix/scripts/refresh.scm (%updaters): Add %crate-updater to list of
  updaters.
---
 guix/import/crate.scm    | 36 +++++++++++++++++++++++++++++++++++-
 guix/scripts/refresh.scm |  4 +++-
 2 files changed, 38 insertions(+), 2 deletions(-)

Comments

Ludovic Courtès Oct. 4, 2016, 9:05 a.m. UTC | #1
David Craven <david@craven.ch> skribis:

> * guix/import/crate.scm (crate-package?, latest-release,
>   %crate-updater): New variables.
> * guix/scripts/refresh.scm (%updaters): Add %crate-updater to list of
>   updaters.

Please add a line under “Invoking guix refresh” in guix.texi, like we
did for the other updaters.

OK with this change!

Ludo’.
diff mbox

Patch

diff --git a/guix/import/crate.scm b/guix/import/crate.scm
index db7f119..8fab200 100644
--- a/guix/import/crate.scm
+++ b/guix/import/crate.scm
@@ -31,7 +31,8 @@ 
   #:use-module (json)
   #:use-module (srfi srfi-1)
   #:export (crate->guix-package
-            guix-package->crate-name))
+            guix-package->crate-name
+            %crate-updater))
 
 (define (crate-fetch name)
   "Return an alist representation of the crates.io metadata for the package NAME,
@@ -91,3 +92,36 @@  VERSION, SOURCE-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE."
 
 (define (crate-name->package-name name)
   (string-append "rust-" name))
+
+(define (crate-package? package)
+  "Return true if PACKAGE is a Rust crate from crates.io."
+
+  (define (crate-url? url)
+    (string-prefix? "https://crates.io/" url))
+
+  (let ((source-url (and=> (package-source package) origin-uri))
+        (fetch-method (and=> (package-source package) origin-method)))
+    (and (eq? fetch-method download:url-fetch)
+         (match source-url
+           ((? string?)
+            (crate-url? source-url))
+           ((source-url ...)
+            (any crate-url? source-url))))))
+
+(define (latest-release package)
+  "Return an <upstream-source> for the latest release of PACKAGE."
+  (let* ((crate-name (guix-package->crate-name package))
+         (metadata (crate-fetch crate-name))
+         (version (assoc-ref* metadata "crate" "max_version"))
+         (url (crate-uri crate-name version)))
+    (upstream-source
+     (package (package-name package))
+     (version version)
+     (urls (list url)))))
+
+(define %crate-updater
+  (upstream-updater
+   (name 'crates)
+   (description "Updater for crates.io packages")
+   (pred crate-package?)
+   (latest latest-release)))
diff --git a/guix/scripts/refresh.scm b/guix/scripts/refresh.scm
index 84e2a8f..eda10df 100644
--- a/guix/scripts/refresh.scm
+++ b/guix/scripts/refresh.scm
@@ -38,6 +38,7 @@ 
                           %xorg-updater))
   #:use-module (guix import elpa)
   #:use-module (guix import cran)
+  #:use-module (guix import crate)
   #:use-module (guix import hackage)
   #:use-module (guix gnupg)
   #:use-module (gnu packages)
@@ -206,7 +207,8 @@  unavailable optional dependencies such as Guile-JSON."
                  %hackage-updater
                  ((guix import pypi) => %pypi-updater)
                  ((guix import gem) => %gem-updater)
-                 ((guix import github) => %github-updater)))
+                 ((guix import github) => %github-updater)
+                 ((guix import crate) => %crate-updater)))
 
 (define (lookup-updater name)
   "Return the updater called NAME."