2015-08-17 03:15:45 +02:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
|
;;; Copyright © 2015 David Thompson <davet@gnu.org>
|
2018-01-10 17:55:37 +01:00
|
|
|
|
;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
|
2018-07-03 22:28:42 +02:00
|
|
|
|
;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
|
2021-01-20 11:35:10 +01:00
|
|
|
|
;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org>
|
2020-02-04 13:18:18 +01:00
|
|
|
|
;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
|
2021-07-15 03:40:35 +02:00
|
|
|
|
;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
|
2022-09-09 15:47:36 +02:00
|
|
|
|
;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
|
2015-08-17 03:15:45 +02:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix import gem)
|
|
|
|
|
#:use-module (ice-9 match)
|
2016-01-01 07:56:07 +01:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2020-09-07 23:12:27 +02:00
|
|
|
|
#:use-module (json)
|
2016-01-01 07:56:07 +01:00
|
|
|
|
#:use-module ((guix download) #:prefix download:)
|
2015-08-17 03:15:45 +02:00
|
|
|
|
#:use-module (guix import utils)
|
|
|
|
|
#:use-module (guix import json)
|
|
|
|
|
#:use-module (guix packages)
|
2016-01-01 07:56:07 +01:00
|
|
|
|
#:use-module (guix upstream)
|
2016-09-22 11:30:17 +02:00
|
|
|
|
#:use-module ((guix licenses) #:prefix license:)
|
2020-02-05 15:52:33 +01:00
|
|
|
|
#:use-module (guix base16)
|
2015-08-17 03:15:45 +02:00
|
|
|
|
#:use-module (guix base32)
|
2020-02-05 15:52:33 +01:00
|
|
|
|
#:use-module ((guix build-system ruby) #:select (rubygems-uri))
|
2016-01-01 07:56:07 +01:00
|
|
|
|
#:export (gem->guix-package
|
2018-07-03 22:28:42 +02:00
|
|
|
|
%gem-updater
|
|
|
|
|
gem-recursive-import))
|
2015-08-17 03:15:45 +02:00
|
|
|
|
|
2020-02-05 15:52:33 +01:00
|
|
|
|
;; Gems as defined by the API at <https://rubygems.org/api/v1/gems>.
|
|
|
|
|
(define-json-mapping <gem> make-gem gem?
|
|
|
|
|
json->gem
|
|
|
|
|
(name gem-name) ;string
|
|
|
|
|
(platform gem-platform) ;string
|
|
|
|
|
(version gem-version) ;string
|
|
|
|
|
(authors gem-authors) ;string
|
|
|
|
|
(licenses gem-licenses "licenses" ;list of strings
|
2020-02-05 16:03:43 +01:00
|
|
|
|
(lambda (licenses)
|
|
|
|
|
;; This is sometimes #nil (the JSON 'null' value). Arrange
|
|
|
|
|
;; to always return a list.
|
|
|
|
|
(cond ((not licenses) '())
|
2021-01-20 11:35:10 +01:00
|
|
|
|
((unspecified? licenses) '())
|
2020-02-05 16:03:43 +01:00
|
|
|
|
((vector? licenses) (vector->list licenses))
|
|
|
|
|
(else '()))))
|
2020-02-05 15:52:33 +01:00
|
|
|
|
(info gem-info)
|
|
|
|
|
(sha256 gem-sha256 "sha" ;bytevector
|
|
|
|
|
base16-string->bytevector)
|
|
|
|
|
(home-page gem-home-page "homepage_uri") ;string
|
|
|
|
|
(dependencies gem-dependencies "dependencies" ;<gem-dependencies>
|
|
|
|
|
json->gem-dependencies))
|
|
|
|
|
|
|
|
|
|
(define-json-mapping <gem-dependencies> make-gem-dependencies
|
|
|
|
|
gem-dependencies?
|
|
|
|
|
json->gem-dependencies
|
|
|
|
|
(development gem-dependencies-development ;list of <gem-dependency>
|
|
|
|
|
"development"
|
|
|
|
|
json->gem-dependency-list)
|
|
|
|
|
(runtime gem-dependencies-runtime ;list of <gem-dependency>
|
|
|
|
|
"runtime"
|
|
|
|
|
json->gem-dependency-list))
|
|
|
|
|
|
|
|
|
|
(define (json->gem-dependency-list vector)
|
2021-01-20 11:35:10 +01:00
|
|
|
|
(if (and vector (not (unspecified? vector)))
|
2020-02-05 15:52:33 +01:00
|
|
|
|
(map json->gem-dependency (vector->list vector))
|
|
|
|
|
'()))
|
|
|
|
|
|
|
|
|
|
(define-json-mapping <gem-dependency> make-gem-dependency gem-dependency?
|
|
|
|
|
json->gem-dependency
|
|
|
|
|
(name gem-dependency-name) ;string
|
|
|
|
|
(requirements gem-dependency-requirements)) ;string
|
|
|
|
|
|
|
|
|
|
|
2022-09-09 15:47:36 +02:00
|
|
|
|
(define* (rubygems-fetch name #:optional version)
|
|
|
|
|
"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."
|
2020-02-05 15:52:33 +01:00
|
|
|
|
(and=> (json-fetch
|
2022-09-09 15:47:36 +02:00
|
|
|
|
(if version
|
|
|
|
|
(string-append "https://rubygems.org/api/v2/rubygems/" name "/versions/" version ".json")
|
|
|
|
|
(string-append "https://rubygems.org/api/v1/gems/" name ".json")))
|
2020-02-05 15:52:33 +01:00
|
|
|
|
json->gem))
|
2015-08-17 03:15:45 +02:00
|
|
|
|
|
|
|
|
|
(define (ruby-package-name name)
|
|
|
|
|
"Given the NAME of a package on RubyGems, return a Guix-compliant name for
|
|
|
|
|
the package."
|
|
|
|
|
(if (string-prefix? "ruby-" name)
|
|
|
|
|
(snake-case name)
|
|
|
|
|
(string-append "ruby-" (snake-case name))))
|
|
|
|
|
|
2016-01-17 00:12:14 +01:00
|
|
|
|
(define (make-gem-sexp name version hash home-page synopsis description
|
2015-08-17 03:15:45 +02:00
|
|
|
|
dependencies licenses)
|
|
|
|
|
"Return the `package' s-expression for a Ruby package with the given NAME,
|
|
|
|
|
VERSION, HASH, HOME-PAGE, DESCRIPTION, DEPENDENCIES, and LICENSES."
|
|
|
|
|
`(package
|
|
|
|
|
(name ,(ruby-package-name name))
|
|
|
|
|
(version ,version)
|
|
|
|
|
(source (origin
|
|
|
|
|
(method url-fetch)
|
|
|
|
|
(uri (rubygems-uri ,name version))
|
|
|
|
|
(sha256
|
|
|
|
|
(base32
|
2020-02-05 15:52:33 +01:00
|
|
|
|
,(bytevector->nix-base32-string hash)))))
|
2015-08-17 03:15:45 +02:00
|
|
|
|
(build-system ruby-build-system)
|
|
|
|
|
,@(if (null? dependencies)
|
|
|
|
|
'()
|
|
|
|
|
`((propagated-inputs
|
2021-07-15 03:40:35 +02:00
|
|
|
|
(list ,@(map string->symbol dependencies)))))
|
2016-01-17 00:12:14 +01:00
|
|
|
|
(synopsis ,synopsis)
|
2015-08-17 03:15:45 +02:00
|
|
|
|
(description ,description)
|
|
|
|
|
(home-page ,home-page)
|
|
|
|
|
(license ,(match licenses
|
2015-09-02 01:57:44 +02:00
|
|
|
|
(() #f)
|
2015-08-17 03:15:45 +02:00
|
|
|
|
((license) (license->symbol license))
|
2015-09-02 01:57:44 +02:00
|
|
|
|
(_ `(list ,@(map license->symbol licenses)))))))
|
2015-08-17 03:15:45 +02:00
|
|
|
|
|
2020-02-04 13:18:18 +01:00
|
|
|
|
(define* (gem->guix-package package-name #:key (repo 'rubygems) version)
|
2015-08-17 03:15:45 +02:00
|
|
|
|
"Fetch the metadata for PACKAGE-NAME from rubygems.org, and return the
|
2022-09-09 15:47:36 +02:00
|
|
|
|
`package' s-expression corresponding to that package, or #f on failure.
|
|
|
|
|
Optionally include a VERSION string to fetch a specific version gem."
|
|
|
|
|
(let ((gem (if version
|
|
|
|
|
(rubygems-fetch package-name version)
|
|
|
|
|
(rubygems-fetch package-name))))
|
2020-02-05 15:52:33 +01:00
|
|
|
|
(if gem
|
|
|
|
|
(let* ((dependencies-names (map gem-dependency-name
|
|
|
|
|
(gem-dependencies-runtime
|
|
|
|
|
(gem-dependencies gem))))
|
|
|
|
|
(dependencies (map (lambda (dep)
|
|
|
|
|
(if (string=? dep "bundler")
|
|
|
|
|
"bundler" ; special case, no prefix
|
|
|
|
|
(ruby-package-name dep)))
|
|
|
|
|
dependencies-names))
|
|
|
|
|
(licenses (map string->license (gem-licenses gem))))
|
|
|
|
|
(values (make-gem-sexp (gem-name gem) (gem-version gem)
|
|
|
|
|
(gem-sha256 gem) (gem-home-page gem)
|
|
|
|
|
(gem-info gem)
|
|
|
|
|
(beautify-description (gem-info gem))
|
|
|
|
|
dependencies
|
|
|
|
|
licenses)
|
|
|
|
|
dependencies-names))
|
|
|
|
|
(values #f '()))))
|
2016-01-01 07:56:07 +01:00
|
|
|
|
|
|
|
|
|
(define (guix-package->gem-name package)
|
|
|
|
|
"Given a PACKAGE built from rubygems.org, return the name of the
|
|
|
|
|
package on RubyGems."
|
|
|
|
|
(let ((source-url (and=> (package-source package) origin-uri)))
|
|
|
|
|
;; The URL has the form:
|
|
|
|
|
;; 'https://rubygems.org/downloads/' +
|
|
|
|
|
;; package name + '-' + version + '.gem'
|
|
|
|
|
;; e.g. "https://rubygems.org/downloads/hashery-2.1.1.gem"
|
|
|
|
|
(substring source-url 31 (string-rindex source-url #\-))))
|
|
|
|
|
|
2016-09-22 11:30:17 +02:00
|
|
|
|
(define (string->license str)
|
|
|
|
|
"Convert the string STR into a license object."
|
|
|
|
|
(match str
|
|
|
|
|
("GNU LGPL" license:lgpl2.0)
|
|
|
|
|
("GPL" license:gpl3)
|
|
|
|
|
((or "BSD" "BSD License") license:bsd-3)
|
|
|
|
|
((or "MIT" "MIT license" "Expat license") license:expat)
|
|
|
|
|
("Public domain" license:public-domain)
|
|
|
|
|
((or "Apache License, Version 2.0" "Apache 2.0") license:asl2.0)
|
|
|
|
|
(_ #f)))
|
|
|
|
|
|
2020-07-03 22:45:21 +02:00
|
|
|
|
(define gem-package?
|
|
|
|
|
(url-prefix-predicate "https://rubygems.org/downloads/"))
|
2016-01-01 07:56:07 +01:00
|
|
|
|
|
2016-04-14 21:40:20 +02:00
|
|
|
|
(define (latest-release package)
|
|
|
|
|
"Return an <upstream-source> for the latest release of PACKAGE."
|
|
|
|
|
(let* ((gem-name (guix-package->gem-name package))
|
2020-02-05 15:52:33 +01:00
|
|
|
|
(gem (rubygems-fetch gem-name))
|
|
|
|
|
(version (gem-version gem))
|
|
|
|
|
(url (rubygems-uri gem-name version)))
|
2016-01-01 07:56:07 +01:00
|
|
|
|
(upstream-source
|
2016-04-14 21:40:20 +02:00
|
|
|
|
(package (package-name package))
|
2016-01-01 07:56:07 +01:00
|
|
|
|
(version version)
|
|
|
|
|
(urls (list url)))))
|
|
|
|
|
|
|
|
|
|
(define %gem-updater
|
|
|
|
|
(upstream-updater
|
|
|
|
|
(name 'gem)
|
|
|
|
|
(description "Updater for RubyGem packages")
|
|
|
|
|
(pred gem-package?)
|
|
|
|
|
(latest latest-release)))
|
2018-07-03 22:28:42 +02:00
|
|
|
|
|
|
|
|
|
(define* (gem-recursive-import package-name #:optional version)
|
2020-02-04 13:18:18 +01:00
|
|
|
|
(recursive-import package-name
|
|
|
|
|
#:repo '()
|
2018-07-03 22:28:42 +02:00
|
|
|
|
#:repo->guix-package gem->guix-package
|
2022-09-09 15:47:36 +02:00
|
|
|
|
#:guix-name ruby-package-name
|
|
|
|
|
#:version version))
|