hexpm.scm 13 KB

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