elm.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Philip McGrath <philip@philipmcgrath.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix import elm)
  19. #:use-module (ice-9 match)
  20. #:use-module (ice-9 vlist)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-26)
  23. #:use-module (srfi srfi-34)
  24. #:use-module (srfi srfi-35)
  25. #:use-module (guix utils)
  26. #:use-module (guix base32)
  27. #:use-module (guix hash)
  28. #:use-module (guix http-client)
  29. #:use-module (guix memoization)
  30. #:use-module (guix diagnostics)
  31. #:use-module (guix i18n)
  32. #:use-module (guix import utils)
  33. #:use-module (guix git)
  34. #:use-module (guix import json)
  35. #:autoload (gcrypt hash) (hash-algorithm sha256)
  36. #:use-module (json)
  37. #:use-module (guix build-system elm)
  38. #:export (elm-recursive-import
  39. %elm-package-registry
  40. %current-elm-checkout
  41. elm->guix-package))
  42. (define %registry-url
  43. ;; It is much nicer to fetch this small (< 40 KB gzipped)
  44. ;; file once than to do many HTTP requests.
  45. "https://package.elm-lang.org/all-packages")
  46. (define %elm-package-registry
  47. ;; This is a parameter to support both testing and memoization.
  48. ;; In pseudo-code, it has the contract:
  49. ;; (parameter/c (-> json/c)
  50. ;; (promise/c (vhash/c string? (listof string?))))
  51. ;; To set the parameter, provide a thunk that returns a value suitable
  52. ;; as an argument to 'json->registry-vhash'. Accessing the parameter
  53. ;; returns a promise wrapping the resulting vhash.
  54. (make-parameter
  55. (lambda ()
  56. (cond
  57. ((json-fetch %registry-url #:http-fetch http-fetch/cached))
  58. (else
  59. (raise (formatted-message
  60. (G_ "error downloading Elm package registry from ~a")
  61. %registry-url)))))
  62. (lambda (thunk)
  63. (delay (json->registry-vhash (thunk))))))
  64. (define (json->registry-vhash jsobject)
  65. "Parse the '(json)' module's representation of the Elm package registry to a
  66. vhash mapping package names to lists of available versions, sorted from latest
  67. to oldest."
  68. (fold (lambda (entry vh)
  69. (match entry
  70. ((name . vec)
  71. (vhash-cons name
  72. (sort (vector->list vec) version>?)
  73. vh))))
  74. vlist-null
  75. jsobject))
  76. (define (json->direct-dependencies jsobject)
  77. "Parse the '(json)' module's representation of an 'elm.json' file's
  78. 'dependencies' or 'test-dependencies' field to a list of strings naming direct
  79. dependencies, handling both the 'package' and 'application' grammars."
  80. (cond
  81. ;; *unspecified*
  82. ((not (pair? jsobject))
  83. '())
  84. ;; {"type":"application"}
  85. ((every (match-lambda
  86. (((or "direct" "indirect") (_ . _) ...)
  87. #t)
  88. (_
  89. #f))
  90. jsobject)
  91. (map car (or (assoc-ref jsobject "direct") '())))
  92. ;; {"type":"package"}
  93. (else
  94. (map car jsobject))))
  95. ;; <project-info> handles both {"type":"package"} and {"type":"application"}
  96. (define-json-mapping <project-info> make-project-info project-info?
  97. json->project-info
  98. (dependencies project-info-dependencies
  99. "dependencies" json->direct-dependencies)
  100. (test-dependencies project-info-test-dependencies
  101. "test-dependencies" json->direct-dependencies)
  102. ;; "synopsis" and "license" may be missing for {"type":"application"}
  103. (synopsis project-info-synopsis
  104. "summary" (lambda (x)
  105. (if (string? x)
  106. x
  107. "")))
  108. (license project-info-license
  109. "license" (lambda (x)
  110. (if (string? x)
  111. (spdx-string->license x)
  112. #f))))
  113. (define %current-elm-checkout
  114. ;; This is a parameter for testing purposes.
  115. (make-parameter
  116. (lambda (name version)
  117. (define-values (checkout _commit _relation)
  118. ;; Elm requires that packages use this very specific format
  119. (update-cached-checkout (string-append "https://github.com/" name)
  120. #:ref `(tag . ,version)))
  121. checkout)))
  122. (define (make-elm-package-sexp name version)
  123. "Return two values: the `package' s-expression for the Elm package with the
  124. given NAME and VERSION, and a list of Elm packages it depends on."
  125. (define checkout
  126. ((%current-elm-checkout) name version))
  127. (define info
  128. (call-with-input-file (string-append checkout "/elm.json")
  129. json->project-info))
  130. (define dependencies
  131. (project-info-dependencies info))
  132. (define test-dependencies
  133. (project-info-test-dependencies info))
  134. (define guix-name
  135. (elm->package-name name))
  136. (values
  137. `(package
  138. (name ,guix-name)
  139. (version ,version)
  140. (source (elm-package-origin
  141. ,name
  142. version ;; no ,
  143. (base32
  144. ,(bytevector->nix-base32-string
  145. (file-hash* checkout
  146. #:algorithm (hash-algorithm sha256)
  147. #:recursive? #t)))))
  148. (build-system elm-build-system)
  149. ,@(maybe-propagated-inputs (map elm->package-name dependencies))
  150. ,@(maybe-inputs (map elm->package-name test-dependencies))
  151. (home-page ,(string-append "https://package.elm-lang.org/packages/"
  152. name "/" version))
  153. (synopsis ,(project-info-synopsis info))
  154. (description
  155. ;; Try to use the first paragraph of README.md (which Elm requires),
  156. ;; or fall back to synopsis otherwise.
  157. ,(beautify-description
  158. (match (chunk-lines (call-with-input-file
  159. (string-append checkout "/README.md")
  160. read-lines))
  161. ((_ par . _)
  162. (string-join par " "))
  163. (_
  164. (project-info-synopsis info)))))
  165. ,@(let ((inferred-name (infer-elm-package-name guix-name)))
  166. (if (equal? inferred-name name)
  167. '()
  168. `((properties '((upstream-name . ,name))))))
  169. (license ,(project-info-license info)))
  170. (append dependencies test-dependencies)))
  171. (define elm->guix-package
  172. (memoize
  173. (lambda* (package-name #:key version #:allow-other-keys)
  174. "Fetch the metadata for PACKAGE-NAME, an Elm package registered at
  175. package.elm.org, and return two values: the `package' s-expression
  176. corresponding to that package (or #f on failure) and a list of Elm
  177. dependencies."
  178. (cond
  179. ((vhash-assoc package-name (force (%elm-package-registry)))
  180. => (match-lambda
  181. ((_found latest . _versions)
  182. (make-elm-package-sexp package-name (or version latest)))))
  183. (else
  184. (values #f '()))))))
  185. (define* (elm-recursive-import package-name #:optional version)
  186. (recursive-import package-name
  187. #:version version
  188. #:repo->guix-package elm->guix-package
  189. #:guix-name elm->package-name))