hexpm.scm 3.5 KB

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