hackage.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  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 scripts import hackage)
  21. #:use-module (guix ui)
  22. #:use-module (guix utils)
  23. #:use-module (guix packages)
  24. #:use-module (guix scripts)
  25. #:use-module (guix import hackage)
  26. #:use-module (guix scripts import)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (srfi srfi-37)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 format)
  32. #:export (guix-import-hackage))
  33. ;;;
  34. ;;; Command-line options.
  35. ;;;
  36. (define ghc-default-version
  37. (string-append "ghc-" (package-version (@ (gnu packages haskell) ghc))))
  38. (define %default-options
  39. `((include-test-dependencies? . #t)
  40. (read-from-stdin? . #f)
  41. (cabal-environment . ,`(("impl" . ,ghc-default-version)))))
  42. (define (show-help)
  43. (display (G_ "Usage: guix import hackage PACKAGE-NAME
  44. Import and convert the Hackage package for PACKAGE-NAME. If PACKAGE-NAME
  45. includes a suffix constituted by a at-sign followed by a numerical version (as
  46. used with Guix packages), then a definition for the specified version of the
  47. package will be generated. If no version suffix is specified, then the
  48. generated package definition will correspond to the latest available
  49. version.\n"))
  50. (display (G_ "
  51. -e ALIST, --cabal-environment=ALIST
  52. specify environment for Cabal evaluation"))
  53. (display (G_ "
  54. -h, --help display this help and exit"))
  55. (display (G_ "
  56. -r, --recursive import packages recursively"))
  57. (display (G_ "
  58. -s, --stdin read from standard input"))
  59. (display (G_ "
  60. -t, --no-test-dependencies don't include test-only dependencies"))
  61. (display (G_ "
  62. -V, --version display version information and exit"))
  63. (newline)
  64. (show-bug-report-information))
  65. (define %options
  66. ;; Specification of the command-line options.
  67. (cons* (option '(#\h "help") #f #f
  68. (lambda args
  69. (show-help)
  70. (exit 0)))
  71. (option '(#\V "version") #f #f
  72. (lambda args
  73. (show-version-and-exit "guix import hackage")))
  74. (option '(#\t "no-test-dependencies") #f #f
  75. (lambda (opt name arg result)
  76. (alist-cons 'include-test-dependencies? #f
  77. (alist-delete 'include-test-dependencies?
  78. result))))
  79. (option '(#\s "stdin") #f #f
  80. (lambda (opt name arg result)
  81. (alist-cons 'read-from-stdin? #t
  82. (alist-delete 'read-from-stdin?
  83. result))))
  84. (option '(#\e "cabal-environment") #t #f
  85. (lambda (opt name arg result)
  86. (alist-cons 'cabal-environment (read/eval arg)
  87. (alist-delete 'cabal-environment
  88. result))))
  89. (option '(#\r "recursive") #f #f
  90. (lambda (opt name arg result)
  91. (alist-cons 'recursive #t result)))
  92. %standard-import-options))
  93. ;;;
  94. ;;; Entry point.
  95. ;;;
  96. (define (guix-import-hackage . args)
  97. (define (parse-options)
  98. ;; Return the alist of option values.
  99. (parse-command-line args %options (list %default-options)
  100. #:build-options? #f))
  101. (define (run-importer package-name opts error-fn)
  102. (let* ((arguments (list
  103. package-name
  104. #:include-test-dependencies?
  105. (assoc-ref opts 'include-test-dependencies?)
  106. #:port (if (assoc-ref opts 'read-from-stdin?)
  107. (current-input-port)
  108. #f)
  109. #:cabal-environment
  110. (assoc-ref opts 'cabal-environment)))
  111. (sexp (if (assoc-ref opts 'recursive)
  112. ;; Recursive import
  113. (map (match-lambda
  114. ((and ('package ('name name) . rest) pkg)
  115. `(define-public ,(string->symbol name)
  116. ,pkg))
  117. (_ #f))
  118. (apply hackage-recursive-import arguments))
  119. ;; Single import
  120. (apply hackage->guix-package arguments))))
  121. (unless sexp (error-fn))
  122. sexp))
  123. (let* ((opts (parse-options))
  124. (args (filter-map (match-lambda
  125. (('argument . value)
  126. value)
  127. (_ #f))
  128. (reverse opts))))
  129. (if (assoc-ref opts 'read-from-stdin?)
  130. (match args
  131. (()
  132. (run-importer "stdin" opts
  133. (lambda ()
  134. (leave (G_ "failed to import cabal file \
  135. from standard input~%")))))
  136. ((many ...)
  137. (leave (G_ "too many arguments~%"))))
  138. (match args
  139. ((package-name)
  140. (run-importer package-name opts
  141. (lambda ()
  142. (leave (G_ "failed to download cabal file \
  143. for package '~a'~%")
  144. package-name))))
  145. (()
  146. (leave (G_ "too few arguments~%")))
  147. ((many ...)
  148. (leave (G_ "too many arguments~%")))))))