stackage.scm 5.4 KB

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