crate.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016 David Craven <david@craven.ch>
  4. ;;; Copyright © 2019, 2020 Martin Becze <mjbecze@riseup.net>
  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 crate)
  21. #:use-module (guix ui)
  22. #:use-module (guix utils)
  23. #:use-module (guix scripts)
  24. #:use-module (guix import crate)
  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-crate))
  32. ;;;
  33. ;;; Command-line options.
  34. ;;;
  35. (define %default-options
  36. '())
  37. (define (show-help)
  38. (display (G_ "Usage: guix import crate PACKAGE-NAME
  39. Import and convert the crates.io package for PACKAGE-NAME.\n"))
  40. (display (G_ "
  41. -r, --recursive import packages recursively"))
  42. (newline)
  43. (display (G_ "
  44. -h, --help display this help and exit"))
  45. (display (G_ "
  46. -V, --version display version information and exit"))
  47. (newline)
  48. (show-bug-report-information))
  49. (define %options
  50. ;; Specification of the command-line options.
  51. (cons* (option '(#\h "help") #f #f
  52. (lambda args
  53. (show-help)
  54. (exit 0)))
  55. (option '(#\V "version") #f #f
  56. (lambda args
  57. (show-version-and-exit "guix import crate")))
  58. (option '(#\r "recursive") #f #f
  59. (lambda (opt name arg result)
  60. (alist-cons 'recursive #t result)))
  61. %standard-import-options))
  62. ;;;
  63. ;;; Entry point.
  64. ;;;
  65. (define (guix-import-crate . args)
  66. (define (parse-options)
  67. ;; Return the alist of option values.
  68. (args-fold* args %options
  69. (lambda (opt name arg result)
  70. (leave (G_ "~A: unrecognized option~%") name))
  71. (lambda (arg result)
  72. (alist-cons 'argument arg result))
  73. %default-options))
  74. (let* ((opts (parse-options))
  75. (args (filter-map (match-lambda
  76. (('argument . value)
  77. value)
  78. (_ #f))
  79. (reverse opts))))
  80. (match args
  81. ((spec)
  82. (define-values (name version)
  83. (package-name->name+version spec))
  84. (if (assoc-ref opts 'recursive)
  85. (crate-recursive-import name #:version version)
  86. (let ((sexp (crate->guix-package name #:version version #:include-dev-deps? #t)))
  87. (unless sexp
  88. (leave (G_ "failed to download meta-data for package '~a'~%")
  89. (if version
  90. (string-append name "@" version)
  91. name)))
  92. (list sexp))))
  93. (()
  94. (leave (G_ "too few arguments~%")))
  95. ((many ...)
  96. (leave (G_ "too many arguments~%"))))))