hexpm.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Cyril Roelandt <tipecaml@gmail.com>
  3. ;;; Copyright © 2016 David Craven <david@craven.ch>
  4. ;;; Copyright © 2017, 2019-2021 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2019 Martin Becze <mjbecze@riseup.net>
  6. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  7. ;;; Copyright © 2020-2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix import hexpm)
  24. #:use-module (guix base32)
  25. #:use-module ((guix download) #:prefix download:)
  26. #:use-module (gcrypt hash)
  27. #:use-module (guix http-client)
  28. #:use-module (json)
  29. #:use-module (guix import utils)
  30. #:use-module ((guix import json) #:select (json-fetch))
  31. #:use-module ((guix build utils)
  32. #:select ((package-name->name+version
  33. . hyphen-package-name->name+version)
  34. dump-port))
  35. #:use-module ((guix licenses) #:prefix license:)
  36. #:use-module (guix monads)
  37. #:use-module (guix packages)
  38. #:use-module (guix upstream)
  39. #:use-module (guix utils)
  40. #:use-module (ice-9 match)
  41. #:use-module (ice-9 regex)
  42. #:use-module (ice-9 popen)
  43. #:use-module (srfi srfi-1)
  44. #:use-module (srfi srfi-2)
  45. #:use-module (srfi srfi-26)
  46. #:use-module (guix build-system rebar)
  47. #:export (hexpm->guix-package
  48. guix-package->hexpm-name
  49. strings->licenses ;; why used here?
  50. hexpm-recursive-import
  51. %hexpm-updater))
  52. ;;;
  53. ;;; Interface to https://hex.pm/api, version 2.
  54. ;;; REST-API end-points:
  55. ;;; https://github.com/hexpm/specifications/blob/master/apiary.apib
  56. ;;; Repository end-points:
  57. ;;; https://github.com/hexpm/specifications/blob/master/endpoints.md
  58. ;;;
  59. (define %hexpm-api-url
  60. (make-parameter "https://hex.pm/api"))
  61. (define (package-url name)
  62. (string-append (%hexpm-api-url) "/packages/" name))
  63. ;;
  64. ;; Hexpm Package. /packages/${name}
  65. ;; https://github.com/hexpm/specifications/blob/master/apiary.apib#Package
  66. ;;
  67. ;; Each package can have several "releases", each of which has its own set of
  68. ;; requirements, build-tool, etc. - see <hexpm-release> below.
  69. (define-json-mapping <hexpm-pkgdef> make-hexpm-pkgdef hexpm-pkgdef?
  70. json->hexpm
  71. (name hexpm-name) ; string
  72. (html-url hexpm-html-url "html_url") ; string
  73. (docs-html-url hexpm-docs-html-url "docs_html_url") ; string | 'null
  74. (meta hexpm-meta "meta" json->hexpm-meta)
  75. (versions hexpm-versions "releases" ; list of <hexpm-version>
  76. (lambda (vector)
  77. (map json->hexpm-version
  78. (vector->list vector))))
  79. ;; "latest_version" and "latest_stable_version" are not named in the
  80. ;; specification, butt seen in practice.
  81. (latest-version hexpm-latest-version "latest_version") ; string
  82. (latest-stable hexpm-latest-stable "latest_stable_version")) ; string
  83. ;; Hexpm package metadata.
  84. (define-json-mapping <hexpm-meta> make-hexpm-meta hexpm-meta?
  85. json->hexpm-meta
  86. (description hexpm-meta-description) ;string
  87. (licenses hexpm-meta-licenses "licenses" ;list of strings
  88. (lambda (vector)
  89. (or (and vector (vector->list vector))
  90. #f))))
  91. ;; Hexpm package versions.
  92. (define-json-mapping <hexpm-version> make-hexpm-version hexpm-version?
  93. json->hexpm-version
  94. (number hexpm-version-number "version") ;string
  95. (url hexpm-version-url)) ;string
  96. (define (lookup-hexpm name)
  97. "Look up NAME on hex.pm and return the corresopnding <hexpm> record
  98. or #f if it was not found."
  99. (and=> (json-fetch (package-url name))
  100. json->hexpm))
  101. ;;
  102. ;; Hexpm release. /packages/${name}/releases/${version}
  103. ;; https://github.com/hexpm/specifications/blob/master/apiary.apib#Release
  104. ;;
  105. (define-json-mapping <hexpm-release> make-hexpm-release hexpm-release?
  106. json->hexpm-release
  107. (version hexpm-release-version) ; string
  108. (url hexpm-release-url) ; string
  109. (meta hexpm-release-meta "meta" json->hexpm-release-meta)
  110. ;; Specification names the next fields "dependencies", but in practice it is
  111. ;; "requirements".
  112. (dependencies hexpm-requirements "requirements")) ; list of <hexpm-dependency>
  113. ;; Hexpm release meta.
  114. ;; https://github.com/hexpm/specifications/blob/main/package_metadata.md
  115. (define-json-mapping <hexpm-release-meta>
  116. make-hexpm-release-meta hexpm-release-meta?
  117. json->hexpm-release-meta
  118. (app hexpm-release-meta-app) ; string
  119. (elixir hexpm-release-meta-elixir) ; string
  120. (build-tools hexpm-release-meta-build-tools "build_tools" ; list of strings
  121. (lambda (vector)
  122. (or (and vector (vector->list vector))
  123. (list)))))
  124. ;; Hexpm dependency. Each requirement has information about the required
  125. ;; version, such as "~> 2.1.2" or ">= 2.1.2 and < 2.2.0", see
  126. ;; <https://hexdocs.pm/elixir/Version.html#module-requirements>, and whether
  127. ;; the dependency is optional.
  128. (define-json-mapping <hexpm-dependency> make-hexpm-dependency
  129. hexpm-dependency?
  130. json->hexpm-dependency
  131. (name hexpm-dependency-name "app") ; string
  132. (requirement hexpm-dependency-requirement) ; string
  133. (optional hexpm-dependency-optional)) ; bool
  134. (define (hexpm-release-dependencies release)
  135. "Return the list of dependency names of RELEASE, a <hexpm-release>."
  136. (let ((reqs (or (hexpm-requirements release) '#())))
  137. (map first reqs))) ;; TODO: also return required version
  138. (define (lookup-hexpm-release version*)
  139. "Look up RELEASE on hexpm-version-url and return the corresopnding
  140. <hexpm-release> record or #f if it was not found."
  141. (and=> (json-fetch (hexpm-version-url version*))
  142. json->hexpm-release))
  143. ;;;
  144. ;;; Converting hex.pm packages to Guix packages.
  145. ;;;
  146. (define (maybe-inputs package-inputs input-type)
  147. "Given a list of PACKAGE-INPUTS, tries to generate the 'inputs' field of a
  148. package definition. INPUT-TYPE, a symbol, is used to populate the name of
  149. the input field."
  150. (match package-inputs
  151. (()
  152. '())
  153. ((package-inputs ...)
  154. `((,input-type (list ,@package-inputs))))))
  155. (define (dependencies->package-names names)
  156. "Given a list of hexpm package NAMES, returns a list of guix package names
  157. as symbols."
  158. ;; TODO: Base name on language of dependency.
  159. ;; The language used for implementing the dependency is not know without
  160. ;; recursing the dependencies. So for now assume more packages are based on
  161. ;; Erlang and prefix all dependencies with "erlang-" (the default).
  162. (map string->symbol
  163. (map hexpm-name->package-name
  164. (sort names string-ci<?))))
  165. (define* (make-hexpm-sexp #:key name version tarball-url
  166. home-page synopsis description license
  167. language build-system dependencies
  168. #:allow-other-keys)
  169. "Return the `package' s-expression for a hexpm package with the given NAME,
  170. VERSION, TARBALL-URL, HOME-PAGE, SYNOPSIS, DESCRIPTION, and LICENSE. The
  171. created package's name will stem from LANGUAGE. BUILD-SYSTEM defined the
  172. build-system, and DEPENDENCIES the inputs for the package."
  173. (call-with-temporary-output-file
  174. (lambda (temp port)
  175. (and (url-fetch tarball-url temp)
  176. (values
  177. `(package
  178. (name ,(hexpm-name->package-name name language))
  179. (version ,version)
  180. (source (origin
  181. (method url-fetch)
  182. (uri (hexpm-uri ,name version))
  183. (sha256 (base32 ,(guix-hash-url temp)))))
  184. (build-system ,build-system)
  185. ,@(maybe-inputs (dependencies->package-names dependencies) 'inputs)
  186. (synopsis ,synopsis)
  187. (description ,(beautify-description description))
  188. (home-page ,(match home-page
  189. (() "")
  190. (_ home-page)))
  191. (license ,(match license
  192. (() #f)
  193. ((license) license)
  194. (_ `(list ,@license))))))))))
  195. (define (strings->licenses strings)
  196. "Convert the list of STRINGS into a list of license objects."
  197. (filter-map (lambda (license)
  198. (and (not (string-null? license))
  199. (not (any (lambda (elem) (string=? elem license))
  200. '("AND" "OR" "WITH")))
  201. (or (spdx-string->license license)
  202. license)))
  203. strings))
  204. (define (hexpm-latest-release package)
  205. "Return the version string for the latest stable release of PACKAGE."
  206. ;; Use latest-stable if specified (see comment in hexpm-pkgdef above),
  207. ;; otherwise compare the lists of release versions.
  208. (let ((latest-stable (hexpm-latest-stable package)))
  209. (if (not (unspecified? latest-stable))
  210. latest-stable
  211. (let ((versions (map hexpm-version-number (hexpm-versions package))))
  212. (fold (lambda (a b)
  213. (if (version>? a b) a b)) (car versions) versions)))))
  214. (define* (hexpm->guix-package package-name #:key repo version)
  215. "Fetch the metadata for PACKAGE-NAME from hexpms.io, and return the
  216. `package' s-expression corresponding to that package, or #f on failure.
  217. When VERSION is specified, attempt to fetch that version; otherwise fetch the
  218. latest version of PACKAGE-NAME."
  219. (define package
  220. (lookup-hexpm package-name))
  221. (define version-number
  222. (and package
  223. (or version
  224. (hexpm-latest-release package))))
  225. (define version*
  226. (and package
  227. (find (lambda (version)
  228. (string=? (hexpm-version-number version)
  229. version-number))
  230. (hexpm-versions package))))
  231. (define release
  232. (and package version*
  233. (lookup-hexpm-release version*)))
  234. (define release-meta
  235. (and package version*
  236. (hexpm-release-meta release)))
  237. (define build-system
  238. (and package version*
  239. (let ((build-tools (hexpm-release-meta-build-tools release-meta)))
  240. (cond
  241. ((member "rebar3" build-tools) 'rebar-build-system)
  242. ((member "mix" build-tools) 'mix-build-system)
  243. ((member "make" build-tools) 'gnu-build-system)
  244. (else #f)))))
  245. (define language
  246. (and package version*
  247. (let ((elixir (hexpm-release-meta-elixir release-meta)))
  248. (cond
  249. ((and (string? elixir) (not (string-null? elixir))) "elixir")
  250. (else "erlang")))))
  251. (and package version*
  252. (let ((dependencies (hexpm-release-dependencies release))
  253. (pkg-meta (hexpm-meta package))
  254. (docs-html-url (hexpm-docs-html-url package)))
  255. (values
  256. (make-hexpm-sexp
  257. #:language language
  258. #:build-system build-system
  259. #:name package-name
  260. #:version version-number
  261. #:dependencies dependencies
  262. #:home-page (or (and (not (eq? docs-html-url 'null))
  263. docs-html-url)
  264. ;; TODO: Homepage?
  265. (hexpm-html-url package))
  266. #:synopsis (hexpm-meta-description pkg-meta)
  267. #:description (hexpm-meta-description pkg-meta)
  268. #:license (or (and=> (hexpm-meta-licenses pkg-meta)
  269. strings->licenses))
  270. #:tarball-url (hexpm-uri package-name version-number))
  271. dependencies))))
  272. (define* (hexpm-recursive-import pkg-name #:optional version)
  273. (recursive-import pkg-name
  274. #:version version
  275. #:repo->guix-package hexpm->guix-package
  276. #:guix-name hexpm-name->package-name))
  277. (define (guix-package->hexpm-name package)
  278. "Return the hex.pm name of PACKAGE."
  279. (define (url->hexpm-name url)
  280. (hyphen-package-name->name+version
  281. (basename (file-sans-extension url))))
  282. (match (and=> (package-source package) origin-uri)
  283. ((? string? url)
  284. (url->hexpm-name url))
  285. ((lst ...)
  286. (any url->hexpm-name lst))
  287. (#f #f)))
  288. (define* (hexpm-name->package-name name #:optional (language "erlang"))
  289. (string-append language "-" (string-join (string-split name #\_) "-")))
  290. ;;;
  291. ;;; Updater
  292. ;;;
  293. (define (latest-release package)
  294. "Return an <upstream-source> for the latest release of PACKAGE."
  295. (let* ((hexpm-name (guix-package->hexpm-name package))
  296. (hexpm (lookup-hexpm hexpm-name))
  297. (version (hexpm-latest-release hexpm))
  298. (url (hexpm-uri hexpm-name version)))
  299. (upstream-source
  300. (package (package-name package))
  301. (version version)
  302. (urls (list url)))))
  303. (define %hexpm-updater
  304. (upstream-updater
  305. (name 'hexpm)
  306. (description "Updater for hex.pm packages")
  307. (pred (url-prefix-predicate hexpm-package-url))
  308. (latest latest-release)))