opam.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Julien Lepiller <julien@lepiller.eu>
  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 scripts import opam)
  19. #:use-module (guix ui)
  20. #:use-module (guix utils)
  21. #:use-module (guix scripts)
  22. #:use-module (guix import opam)
  23. #:use-module (guix scripts import)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-37)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 format)
  29. #:export (guix-import-opam))
  30. ;;;
  31. ;;; Command-line options.
  32. ;;;
  33. (define %default-options
  34. '())
  35. (define (show-help)
  36. (display (G_ "Usage: guix import opam PACKAGE-NAME
  37. Import and convert the opam package for PACKAGE-NAME.\n"))
  38. (display (G_ "
  39. -h, --help display this help and exit"))
  40. (display (G_ "
  41. -r, --recursive import packages recursively"))
  42. (display (G_ "
  43. --repo import packages from this opam repository"))
  44. (display (G_ "
  45. -V, --version display version information and exit"))
  46. (newline)
  47. (show-bug-report-information))
  48. (define %options
  49. ;; Specification of the command-line options.
  50. (cons* (option '(#\h "help") #f #f
  51. (lambda args
  52. (show-help)
  53. (exit 0)))
  54. (option '(#\V "version") #f #f
  55. (lambda args
  56. (show-version-and-exit "guix import opam")))
  57. (option '(#f "repo") #t #f
  58. (lambda (opt name arg result)
  59. (alist-cons 'repo arg result)))
  60. (option '(#\r "recursive") #f #f
  61. (lambda (opt name arg result)
  62. (alist-cons 'recursive #t result)))
  63. %standard-import-options))
  64. ;;;
  65. ;;; Entry point.
  66. ;;;
  67. (define (guix-import-opam . args)
  68. (define (parse-options)
  69. ;; Return the alist of option values.
  70. (args-fold* args %options
  71. (lambda (opt name arg result)
  72. (leave (G_ "~A: unrecognized option~%") name))
  73. (lambda (arg result)
  74. (alist-cons 'argument arg result))
  75. %default-options))
  76. (let* ((opts (parse-options))
  77. (repo (and=> (assoc-ref opts 'repo) string->symbol))
  78. (args (filter-map (match-lambda
  79. (('argument . value)
  80. value)
  81. (_ #f))
  82. (reverse opts))))
  83. (match args
  84. ((package-name)
  85. (if (assoc-ref opts 'recursive)
  86. ;; Recursive import
  87. (map (match-lambda
  88. ((and ('package ('name name) . rest) pkg)
  89. `(define-public ,(string->symbol name)
  90. ,pkg))
  91. (_ #f))
  92. (opam-recursive-import package-name #:repo repo))
  93. ;; Single import
  94. (let ((sexp (opam->guix-package package-name #:repo repo)))
  95. (unless sexp
  96. (leave (G_ "failed to download meta-data for package '~a'~%")
  97. package-name))
  98. sexp)))
  99. (()
  100. (leave (G_ "too few arguments~%")))
  101. ((many ...)
  102. (leave (G_ "too many arguments~%"))))))