stackage.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix import stackage)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 regex)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-35)
  27. #:use-module (guix import json)
  28. #:use-module (guix import hackage)
  29. #:use-module (guix import utils)
  30. #:use-module (guix memoization)
  31. #:use-module (guix packages)
  32. #:use-module (guix upstream)
  33. #:export (%stackage-url
  34. stackage->guix-package
  35. stackage-recursive-import
  36. %stackage-updater))
  37. ;;;
  38. ;;; Stackage info fetcher and access functions
  39. ;;;
  40. (define %stackage-url
  41. (make-parameter "https://www.stackage.org"))
  42. ;; Latest LTS version compatible with GHC 8.6.5.
  43. (define %default-lts-version "14.27")
  44. (define (lts-info-packages lts-info)
  45. "Returns the alist of packages contained in LTS-INFO."
  46. (or (assoc-ref lts-info "packages") '()))
  47. (define (leave-with-message fmt . args)
  48. (raise (condition (&message (message (apply format #f fmt args))))))
  49. (define stackage-lts-info-fetch
  50. ;; "Retrieve the information about the LTS Stackage release VERSION."
  51. (memoize
  52. (lambda* (#:optional (version ""))
  53. (let* ((url (string-append (%stackage-url)
  54. "/lts-" (if (string-null? version)
  55. %default-lts-version
  56. version)))
  57. (lts-info (json-fetch url)))
  58. (if lts-info
  59. (reverse lts-info)
  60. (leave-with-message "LTS release version not found: ~a" version))))))
  61. (define (stackage-package-name pkg-info)
  62. (assoc-ref pkg-info "name"))
  63. (define (stackage-package-version pkg-info)
  64. (assoc-ref pkg-info "version"))
  65. (define (lts-package-version pkgs-info name)
  66. "Return the version of the package with upstream NAME included in PKGS-INFO."
  67. (let ((pkg (find (lambda (pkg) (string=? (stackage-package-name pkg) name))
  68. (vector->list pkgs-info))))
  69. (stackage-package-version pkg)))
  70. ;;;
  71. ;;; Importer entry point
  72. ;;;
  73. (define (hackage-name-version name version)
  74. (and version (string-append name "@" version)))
  75. (define stackage->guix-package
  76. (memoize
  77. (lambda* (package-name ; upstream name
  78. #:key
  79. (include-test-dependencies? #t)
  80. (lts-version %default-lts-version)
  81. (packages-info
  82. (lts-info-packages
  83. (stackage-lts-info-fetch lts-version))))
  84. "Fetch Cabal file for PACKAGE-NAME from hackage.haskell.org. The retrieved
  85. version corresponds to the version of PACKAGE-NAME specified in the LTS-VERSION
  86. release at stackage.org. Return the `package' S-expression corresponding to
  87. that package, or #f on failure. PACKAGES-INFO is the alist with the packages
  88. included in the Stackage LTS release."
  89. (let* ((version (lts-package-version packages-info package-name))
  90. (name-version (hackage-name-version package-name version)))
  91. (if name-version
  92. (hackage->guix-package name-version
  93. #:include-test-dependencies?
  94. include-test-dependencies?)
  95. (leave-with-message "~a: Stackage package not found" package-name))))))
  96. (define (stackage-recursive-import package-name . args)
  97. (recursive-import package-name
  98. #:repo->guix-package (lambda* (name #:key repo version)
  99. (apply stackage->guix-package (cons name args)))
  100. #:guix-name hackage-name->package-name))
  101. ;;;
  102. ;;; Updater
  103. ;;;
  104. (define latest-lts-release
  105. (let ((pkgs-info
  106. (mlambda () (lts-info-packages
  107. (stackage-lts-info-fetch %default-lts-version)))))
  108. (lambda* (package)
  109. "Return an <upstream-source> for the latest Stackage LTS release of
  110. PACKAGE or #f if the package is not included in the Stackage LTS release."
  111. (let* ((hackage-name (guix-package->hackage-name package))
  112. (version (lts-package-version (pkgs-info) hackage-name))
  113. (name-version (hackage-name-version hackage-name version)))
  114. (match (and=> name-version hackage-fetch)
  115. (#f (format (current-error-port)
  116. "warning: failed to parse ~a~%"
  117. (hackage-cabal-url hackage-name))
  118. #f)
  119. (_ (let ((url (hackage-source-url hackage-name version)))
  120. (upstream-source
  121. (package (package-name package))
  122. (version version)
  123. (urls (list url))))))))))
  124. (define %stackage-updater
  125. (upstream-updater
  126. (name 'stackage)
  127. (description "Updater for Stackage LTS packages")
  128. (pred hackage-package?)
  129. (latest latest-lts-release)))
  130. ;;; stackage.scm ends here