hackage.scm 6.2 KB

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