import: gem: Support importing a specific version of a gem.

* guix/import/gem.scm: (rubygems-fetch, gem->guix-package)
(gem-recursive-import): Fix to fetch the specified version of the gem.
* guix/scripts/import/gem.scm (show-help): Update the help message.
(guix-import-gem): Modify so the version number to be passed to subsequent
procedures.
* tests/gem.scm: Add tests.
* doc/guix.texi (Invoking guix import): Document.

Signed-off-by: Christopher Baines <mail@cbaines.net>
This commit is contained in:
Taiju HIGASHI 2022-09-09 22:47:36 +09:00 committed by Christopher Baines
parent c54ef97c80
commit 5d22261db3
No known key found for this signature in database
GPG Key ID: 5E28A33B0B84F577
4 changed files with 154 additions and 23 deletions

@ -13219,6 +13219,12 @@ The command below imports metadata for the @code{rails} Ruby package:
guix import gem rails guix import gem rails
@end example @end example
You can also ask for a specific version:
@example
guix import gem @@7.0.4
@end example
@table @code @table @code
@item --recursive @item --recursive
@itemx -r @itemx -r

@ -5,6 +5,7 @@
;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net> ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev> ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -81,10 +82,12 @@
(requirements gem-dependency-requirements)) ;string (requirements gem-dependency-requirements)) ;string
(define (rubygems-fetch name) (define* (rubygems-fetch name #:optional version)
"Return a <gem> record for the package NAME, or #f on failure." "Return a <gem> record for the package NAME and VERSION, or #f on failure. If VERSION is #f or missing, return the latest version gem."
(and=> (json-fetch (and=> (json-fetch
(string-append "https://rubygems.org/api/v1/gems/" name ".json")) (if version
(string-append "https://rubygems.org/api/v2/rubygems/" name "/versions/" version ".json")
(string-append "https://rubygems.org/api/v1/gems/" name ".json")))
json->gem)) json->gem))
(define (ruby-package-name name) (define (ruby-package-name name)
@ -122,8 +125,11 @@ VERSION, HASH, HOME-PAGE, DESCRIPTION, DEPENDENCIES, and LICENSES."
(define* (gem->guix-package package-name #:key (repo 'rubygems) version) (define* (gem->guix-package package-name #:key (repo 'rubygems) version)
"Fetch the metadata for PACKAGE-NAME from rubygems.org, and return the "Fetch the metadata for PACKAGE-NAME from rubygems.org, and return the
`package' s-expression corresponding to that package, or #f on failure." `package' s-expression corresponding to that package, or #f on failure.
(let ((gem (rubygems-fetch package-name))) Optionally include a VERSION string to fetch a specific version gem."
(let ((gem (if version
(rubygems-fetch package-name version)
(rubygems-fetch package-name))))
(if gem (if gem
(let* ((dependencies-names (map gem-dependency-name (let* ((dependencies-names (map gem-dependency-name
(gem-dependencies-runtime (gem-dependencies-runtime
@ -189,4 +195,5 @@ package on RubyGems."
(recursive-import package-name (recursive-import package-name
#:repo '() #:repo '()
#:repo->guix-package gem->guix-package #:repo->guix-package gem->guix-package
#:guix-name ruby-package-name)) #:guix-name ruby-package-name
#:version version))

@ -4,6 +4,7 @@
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev> ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re> ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com> ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -31,6 +32,7 @@
#:use-module (srfi srfi-37) #:use-module (srfi srfi-37)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (ice-9 format) #:use-module (ice-9 format)
#:use-module (ice-9 receive)
#:export (guix-import-gem)) #:export (guix-import-gem))
@ -42,8 +44,9 @@
'()) '())
(define (show-help) (define (show-help)
(display (G_ "Usage: guix import gem PACKAGE-NAME (display (G_ "Usage: guix import gem PACKAGE-NAME[@VERSION] Import and
Import and convert the RubyGems package for PACKAGE-NAME.\n")) convert the RubyGems package for PACKAGE-NAME. Optionally, a version can be
specified after the at-sign (@) character.\n"))
(display (G_ " (display (G_ "
-h, --help display this help and exit")) -h, --help display this help and exit"))
(display (G_ " (display (G_ "
@ -86,21 +89,23 @@ Import and convert the RubyGems package for PACKAGE-NAME.\n"))
(_ #f)) (_ #f))
(reverse opts)))) (reverse opts))))
(match args (match args
((package-name) ((spec)
(let ((code (if (assoc-ref opts 'recursive) (receive (package-name package-version)
(map (match-lambda (package-name->name+version spec)
((and ('package ('name name) . rest) pkg) (let ((code (if (assoc-ref opts 'recursive)
`(define-public ,(string->symbol name) (map (match-lambda
,pkg)) ((and ('package ('name name) . rest) pkg)
(_ #f)) `(define-public ,(string->symbol name)
(gem-recursive-import package-name 'rubygems)) ,pkg))
(let ((sexp (gem->guix-package package-name))) (_ #f))
(if sexp sexp #f))))) (gem-recursive-import package-name package-version))
(match code (let ((sexp (gem->guix-package package-name #:version package-version)))
((or #f '(#f)) (if sexp sexp #f)))))
(leave (G_ "failed to download meta-data for package '~a'~%") (match code
package-name)) ((or #f '(#f))
(_ code)))) (leave (G_ "failed to download meta-data for package '~a'~%")
package-name))
(_ code)))))
(() (()
(leave (G_ "too few arguments~%"))) (leave (G_ "too few arguments~%")))
((many ...) ((many ...)

@ -3,6 +3,7 @@
;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net> ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com> ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev> ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -44,6 +45,22 @@
\"licenses\": [\"MIT\", \"Apache 2.0\"] \"licenses\": [\"MIT\", \"Apache 2.0\"]
}") }")
(define test-foo-v2-json
"{
\"name\": \"foo\",
\"version\": \"2.0.0\",
\"sha\": \"f3676eafca9987cb5fe263df1edf2538bf6dafc712b30e17be3543a9680547a8\",
\"info\": \"A cool gem\",
\"homepage_uri\": \"https://example.com\",
\"dependencies\": {
\"runtime\": [
{ \"name\": \"bundler\" },
{ \"name\": \"bar\" }
]
},
\"licenses\": [\"MIT\", \"Apache 2.0\"]
}")
(define test-bar-json (define test-bar-json
"{ "{
\"name\": \"bar\", \"name\": \"bar\",
@ -103,6 +120,35 @@
(x (x
(pk 'fail x #f))))) (pk 'fail x #f)))))
(test-assert "gem->guix-package with a specific version"
;; Replace network resources with sample data.
(mock ((guix http-client) http-fetch
(lambda (url . rest)
(match url
("https://rubygems.org/api/v2/rubygems/foo/versions/2.0.0.json"
(values (open-input-string test-foo-v2-json)
(string-length test-foo-v2-json)))
(_ (error "Unexpected URL: " url)))))
(match (gem->guix-package "foo" #:version "2.0.0")
(('package
('name "ruby-foo")
('version "2.0.0")
('source ('origin
('method 'url-fetch)
('uri ('rubygems-uri "foo" 'version))
('sha256
('base32
"1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
('build-system 'ruby-build-system)
('propagated-inputs ('list 'bundler 'ruby-bar))
('synopsis "A cool gem")
('description "This package provides a cool gem")
('home-page "https://example.com")
('license ('list 'license:expat 'license:asl2.0)))
#t)
(x
(pk 'fail x #f)))))
(test-assert "gem-recursive-import" (test-assert "gem-recursive-import"
;; Replace network resources with sample data. ;; Replace network resources with sample data.
(mock ((guix http-client) http-fetch (mock ((guix http-client) http-fetch
@ -170,4 +216,71 @@
(x (x
(pk 'fail x #f))))) (pk 'fail x #f)))))
(test-assert "gem-recursive-import with a specific version"
;; Replace network resources with sample data.
(mock ((guix http-client) http-fetch
(lambda (url . rest)
(match url
("https://rubygems.org/api/v2/rubygems/foo/versions/2.0.0.json"
(values (open-input-string test-foo-v2-json)
(string-length test-foo-v2-json)))
("https://rubygems.org/api/v1/gems/bar.json"
(values (open-input-string test-bar-json)
(string-length test-bar-json)))
("https://rubygems.org/api/v1/gems/bundler.json"
(values (open-input-string test-bundler-json)
(string-length test-bundler-json)))
(_ (error "Unexpected URL: " url)))))
(match (gem-recursive-import "foo" "2.0.0")
((('package
('name "ruby-bar")
('version "1.0.0")
('source
('origin
('method 'url-fetch)
('uri ('rubygems-uri "bar" 'version))
('sha256
('base32
"1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
('build-system 'ruby-build-system)
('propagated-inputs ('list 'bundler))
('synopsis "Another cool gem")
('description "Another cool gem")
('home-page "https://example.com")
('license #f)) ;no licensing info
('package
('name "ruby-bundler")
('version "1.14.2")
('source
('origin
('method 'url-fetch)
('uri ('rubygems-uri "bundler" 'version))
('sha256
('base32
"1446xiz7zg0bz7kgx9jv84y0s4hpsg61dj5l3qb0i00avc1kxd9v"))))
('build-system 'ruby-build-system)
('synopsis "Ruby gem bundler")
('description "Ruby gem bundler")
('home-page "https://bundler.io/")
('license 'license:expat))
('package
('name "ruby-foo")
('version "2.0.0")
('source
('origin
('method 'url-fetch)
('uri ('rubygems-uri "foo" 'version))
('sha256
('base32
"1a270mlajhrmpqbhxcqjqypnvgrq4pgixpv3w9gwp1wrrapnwrzk"))))
('build-system 'ruby-build-system)
('propagated-inputs ('list 'bundler 'ruby-bar))
('synopsis "A cool gem")
('description "This package provides a cool gem")
('home-page "https://example.com")
('license ('list 'license:expat 'license:asl2.0))))
#t)
(x
(pk 'fail x #f)))))
(test-end "gem") (test-end "gem")